How do you create a custom network and configure a Docker Compose service to use this network?
Experience Level: Junior
Tags: Docker Compose
Answer
Add networks block to the root level of the Docker Compose file together with a network name.
Add networks block to the service and add a network-name to it.
The file should look similar to this:
version: "3.4"
services:
my-service:
image: busybox
networks:
- my-network
networks:
my-network:
name: my-network
Related Docker Compose job interview questions
How do you pass environment variables to Docker Compose containers when you are starting services?
Docker Compose JuniorHow do you create a Docker Compose file that is parametrized using environment variables?
Docker Compose JuniorHow do you pass an environment variable to a Docker Compose service container?
Docker Compose JuniorHow do you display images of all running containers that were created based on running Docker Compose?
Docker Compose JuniorHow do you list all running containers that were created based on running Docker Compose?
Docker Compose Junior