I've been playing with Docker-Compose for the last few days to see if it would simplify my Docker Container and Network building process.
I'm pretty happy with it, but ran into a problem when I wanted to create a few 'Networks' that didn't get used by any 'Services' (yet).
The reason I want this behavior was to have a Docker-Compose file to create my local fabric of 'Private' network and 'Public' Network. And a separate Docker-Compose for each of my projects that utilize those already created 'external' networks.
I noted that I was able to just specify a dummy container to initialize the creation of the Networks, but it seemed unnecessary. e.g..
version: '2'
services:
# Dummy Service
dummy:
image: busybox
container_name: dummy
hostname: dummy
networks:
private:
networks:
# Private Network for all Services (across Projects)
private:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.18.0.0/24
gateway: 172.18.0.1
Is there a setting/flag that I'm overlooking, or is there currently no way to use Docker-Compose to create Networks without Containers.
Additionally, am I just approaching this incorrectly?
Essentially I'd like to have a network that live on regardless of what containers join/leave it.
By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.
Docker Compose sets up a single network for your application(s) by default, adding each container for a service to the default network. Containers on a single network can reach and discover every other container on the network.
Docker Compose (herein referred to as compose) will use the Dockerfile if you add the build command to your project's docker-compose. yml . Your Docker workflow should be to build a suitable Dockerfile for each image you wish to create, then use compose to assemble the images using the build command.
All my docker images and networks etc.. files I like to run with docker-compose. So while my thought process is on docker-compose I just have docker-compose network files that spin up a hello-world image (many people already have the image, else it is like 2kb) and then it immediately exits the image of coarse but keeps the network.
version: '3.7'
services:
hello_world:
image: hello-world:latest
networks:
main_net:
ipv4_address: 172.20.0.255
networks:
main_net:
name: main_network
ipam:
config:
- subnet: 172.20.0.0/16
This way I plan my network and lay it out nicely in docker-compose file with th minimum overhead...
docker-compose-networks.yml:
version: '3.7'
services:
works:
image: scratch
restart: always
container_name: net_works
deploy:
placement:
constraints:
- "node.hostname==scratch"
networks:
- mynet1
- mynet2
networks:
mynet1:
name: mynet1
driver: overlay
attachable: false
mynet2:
name: mynet2
driver: overlay
attachable: true
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