can not use user-defined bridge in swarm compose yaml file - docker

I learned from docker documentation that I can not use docker DNS to find containers using their hostnames without utilizing user-defined bridge network. I created one using the command:
docker network create --driver=overlay --subnet=172.22.0.0/16 --gateway=172.22.0.1 user_defined_overlay
and tried to deploy a container that uses it. compose file looks like:
version: "3.0"
services:
web1:
image: "test"
ports:
- "12023:22"
hostname: "mytest-web1"
networks:
- test
web2:
image: "test"
ports:
- "12024:22"
hostname: "mytest-web2"
networks:
- test
networks:
test:
external:
name: user_defined_overlay
my docker version is: Docker version 17.06.2-ce, build cec0b72
and I got the following error when I tried deploying the stack:
network "user_defined_bridge" is declared as external, but it is not in the right scope: "local" instead of "swarm"
I was able to create an overlay network and define it in compose file. that worked fine but it didn't for bridge.
result of docker network ls:
NETWORK ID NAME DRIVER SCOPE
cd6c1e05fca1 bridge bridge local
f0df22fb157a docker_gwbridge bridge local
786416ba8d7f host host local
cuhjxyi98x15 ingress overlay swarm
531b858419ba none null local
15f7e38081eb user_defined_overlay overlay swarm
UPDATE
I tried creating two containers running on two different swarm nodes(1st container runs on manager while second runs on worker node) and I specified the user-defined overlay network as shown in stack above. I tried pinging mytest-web2 container from within mytest-web1 container using hostname but I got unknown host mytest-web2

As of 17.06, you can create node local networks with a swarm scope. Do so with the --scope=swarm option, e.g.:
docker network create --scope=swarm --driver=bridge \
--subnet=172.22.0.0/16 --gateway=172.22.0.1 user_defined_bridge
Then you can use this network with services and stacks defined in swarm mode. For more details, you can see PR #32981.
Edit: you appear to have significantly overcomplicated your problem. As long as everything is being done in a single compose file, there's no need to define the network as external. There is a requirement to use an overlay network if you want to communicate container-to-container. DNS discovery is included on bridge and overlay networks with the exception of the default "bridge" network that docker creates. With a compose file, you would never use this network without explicitly configuring it as an external network with that name. So to get container to container networking to work, you can let docker-compose or docker stack deploy create the network for your project/stack automatically with:
version: "3.0"
services:
web1:
image: "test"
ports:
- "12023:22"
web2:
image: "test"
ports:
- "12024:22"
Note that I have also removed the "hostname" setting. It's not needed for DNS resolution. You can communicate directly with a service VIP with the name "web1" or "web2" from either of these containers.
With docker-compose it will create a default bridge network. Swarm mode will create an overlay network. These defaults are ideal to allow DNS discovery and container-to-container communication in each of the scenarios.

The overlay network is the network to be used in swarm. Swarm is meant to be used to manage containers on multiple hosts and overlay networks are docker's multi-host networks https://docs.docker.com/engine/userguide/networking/get-started-overlay/

Related

Can I create a private network that is user independent in docker?

So we have bunch of containers on a system and we want to be able to start/stop them from various User ID (Linux) with docker-compose.
In the docker compose we create a private network so the containers can talk to each other:
networks:
our_app:
driver: bridge
But when we use docker-compose the network is create as "userid_our_app".
Is there not a way to create a network across all users in the compose file?
You could create a network using the docker network create command, then refer to it as default network in the docker-compose manifests, like:
networks:
default:
name: our_app_network
external: true

Problems with network in Docker Swarm

I have been trying to reproduce (in other machine in the same network) the video played by this container on the docker-compose via swarm.
services:
vlc:
image: boydachina/vlc-server
ports:
- 8080:8080
- 8554:8554
networks:
- vlc_net
command:
- cvlc -vvv /opt/vlc-media/python.mp4 --sout '#transcode{vcodec=h264,acodec=mpga,ab=128,channels=2,samplerate=44100}:rtp{sdp=rtsp://:8554/}'
volumes:
- ./media:/opt/vlc-media/
networks:
vlc_net:
But it is as if there was no network from the container of the other machine to my machine. I thought that putting it in bridge mode would solve it, but I saw that you can't put the Docker Swarm in bridge mode. I need to play the video on several machines on the network, does anyone have any solutions?
Before you deploy the stack to the swarm, create a Docker Network with the overlay driver (note that network names must be unique):
docker network create --driver overlay vlc_net
This will create an overlay network that spans the entire swarm.
Then try setting the network options like this:
networks:
vlc_net:
driver: overlay
external: true
It might also help you to look at how Traefik manages its network in a docker swarm and try to replicate it, since all containers in a swarm can connect to Traefik, and that seems like the use case you are trying to solve.

"This node is not a swarm manager" error, but I'm not using docker swarm

For testing our escrow build, I'm attempting to set up a docker network that's isolated from the host and from the outside world.
I've got the following docker-compose.yml (inspired by this forum post):
version: '3'
services:
redis:
image: "redis:2.8.23"
networks:
- isolated
# ... more services (TODO)
networks:
isolated:
driver: overlay
internal: true
When I run docker-compose up -d; it creates the network, but then fails to create the containers, reporting the following:
ERROR: This node is not a swarm manager. Use "docker swarm init" or "docker swarm join" to connect this node to swarm and try again.
But I'm not using docker swarm, nor do I want to.
If I remove the services: stanza from the file, it brings up the network without the error. It warns that the network is unused (obviously).
If I remove the services/redis/networks stanza, it brings up the container correctly.
What am I doing wrong?
I found this answer, which uses driver: bridge for the network, but that still allows access to the host.
Docker version 18.09.3, build 774a1f4
docker-compose version 1.21.2, build a133471
You have specified the network driver to be overlay.
The overlay network driver very much depends on swarm mode and can effectively be considered to be a swarm mode component.
Instead, you should choose a driver that is a local scope driver rather than a swarm scope driver.
The driver you should use is the bridge driver. The bridge network driver is not part of swarm mode and does not depend on swarm mode being active to utilize it.
Since you are using docker-compose, you can just leave the specific driver out entirely, and it will choose the appropriate driver for you. I would recommend removing the driver: overlay line out completely and leaving the rest of the file as-is:
version: '3'
services:
redis:
image: "redis:2.8.23"
networks:
- isolated
# ... more services (TODO)
networks:
isolated:
internal: true

Can't find Docker Compose network entry

I am trying to communicate from one Docker container running on my Win10 laptop with another container also running locally.
I start up the target container, and I see the following network:
docker network ls
NETWORK ID NAME DRIVER SCOPE
...
f85b7c89dc30 w3virtualservicew3id_w3-virtual-service-w3id bridge
I then start up my calling container with docker-compose up. I can then successfully connect my other container to the network via the command line:
docker network connect w3virtualservicew3id_w3-virtual-service-w3id w3vacationatibmservice_rest_1
However, I can't connect to that same network by adding it to the network section of my docker-compose.yml file for the calling container. I was under the impression that they both basically did the same thing:
networks:
- w3_vacation-at-ibm_service
- w3virtualservicew3id_w3-virtual-service-w3id
The error message tells me it can't find the network, which is not true, since I can connect via the command line, so I know it's really there and running:
ERROR: Service "rest" uses an undefined network "w3virtualservicew3id_w3-virtual-service-w3id"
Anyone have any idea what I'm missing?
The network you define under your service is expected to be defined inside the global networks section (same thing for volumes):
version 'X.Y'
services:
calling_container:
networks:
- your_network
networks:
your_network:
external: true
Do you really have to use a separate compose yml for your calling container? If both of your container interacts with each other, you should add them both to one and the same compose yml. In this case, you don't have to specifiy any network, they will automatically be inside the same network.

How to join the default bridge network with docker-compose v2?

I tried to setup an nginx-proxy container to access my other containers via subdomains on port 80 instead of special ports. As you can guess, I could not get it to work.
I'm kind of new to docker itself and found that it's more comfortable for me to write docker-compose.yml files so I don't have to constantly write long docker run ... commands. I thought there's no difference in how you start the containers, either with docker or docker-compose. However, one difference I noticed is that starting the container with docker does not create any new networks, but with docker-compose there will be a xxx_default network afterwards.
I read that containers on different networks cannot access each other and maybe that might be the reason why the nginx-proxy is not forwarding the requests to the other containers. However, I was unable to find a way to configure my docker-compose.yml file to not create any new networks, but instead join the default bridge network like docker run does.
I tried the following, but it resulted in an error saying that I cannot join system networks like this:
networks:
default:
external:
name: bridge
I also tried network_mode: bridge, but that didn't seem to make any difference.
How do I have to write the docker-compose.yml file to not create a new network, or is that not possible at all?
Bonus question: Are there any other differences between docker and docker-compose that I should know of?
Adding network_mode: bridge to each service in your docker-compose.yml will stop compose from creating a network.
If any service is not configured with this bridge (or host), a network will be created.
Tested and confirmed with:
version: "2.1"
services:
app:
image: ubuntu:latest
network_mode: bridge

Resources