If you want to use a predefined network in Docker Compose, you can do so by specifying the network name and driver in your docker-compos...
If you want to use a predefined network in Docker Compose, you can do so by specifying the network name and driver in your docker-compose.yml
file. Here's an example:
Assuming you already have a Docker network named my-custom-network
created, you can define a service in your docker-compose.yml
file that uses this network:
version: '3'
services:
webapp:
image: nginx:latest
container_name: webapp-container
ports:
- "80:80"
networks:
- my-custom-network
database:
image: mysql:latest
container_name: db-container
environment:
MYSQL_ROOT_PASSWORD: root_password
networks:
- my-custom-network
networks:
my-custom-network:
external: true
In this example:
We define two services:
webapp
anddatabase
.Both services use the
networks
directive to specify themy-custom-network
network.
Ensure that the my-custom-network
is already created before you run the docker-compose
command. You can create a custom network using the following command:
docker network create my-custom-network
Once the network is created, you can start the containers using Docker Compose:
docker-compose up -d
This will create and start the webapp-container
and db-container
containers on the my-custom-network
network, allowing them to communicate using the network name.
By using a predefined network, you can share it among multiple Docker Compose projects or containers, allowing for better network isolation and management.
COMMENTS