Really basic one but this documentation only seems to mention the docker-compose file itself as opposed to the command line: https://docs.docker.com/compose/networking/#links
When I call:
docker-compose up -d service1 service2
I want those created containers to be on a new network with a custom name. How do I specify this network when using the docker-compose up command?
Even if I add network config to the docker-compose file:
networks:
mynetwork:
service1:
networks:
- mynetwork
The docker-compose command still creates the containers on a default network, rather than use "mynetwork".
Create a external network if it doesn't exist -
$ docker network create mynetwork || true
Define external network to compose file -
.........
ports:
- "8088:8088"
networks:
- mynetwork
networks:
mynetwork:
external: true
Similarly, you can also use the default network create by compose but it will prefix your current directory name to the network name defined. You can also use the host network mode but that's not suggested.
Above two use cases are old now, since compose 3.5
you can give custom names to your compose environment. This lets services in multiple compose files talk to each other without doing much of a configuration.
Preferred
Ex -
version: '3.5'
.........
ports:
- "8088:8088"
networks:
- mynetwork
networks:
mynetwork:
external: true
name: mynetwork
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With