The purpose of the this post is to learn how to connect a docker service to the existing or already created docker network. We will be creating a docker network and write the docker-compose.yml file with a service connecting to external network.
Lets create one docker network with overlay driver. For simplicity you may opt not to not use the driver and subnet option that I am using here in network creation.
docker network create --driver overlay --subnet10.0.9.0/24 myexternal-network
Now lets crate a docker docker-compose.yml file and add the external network to it.
version:"3.4" networks: myexternal-network: external: true services: myapp: image: imagename networks: - myexternal-network
So in the networks we usually define a network but here we added a network and represented it as external. adding the network to services are the same like we do for other networks.
As we are using the external network it must be already available before running the docker service else will throw warning message.