Unable to access web server in Docker swarm from host
I started using Docker on macOS and got stuck trying to complete part 4 of the Getting Started Guide . I created two additional virtual machines ( and ), set up as cluster manager and worker respectively. Then, a stack of 5 Flask web servers was deployed using the steps in Part 3 of this tutorial . The processes seem to start fine and are distributed between the two machines, but I can't access the processes from the host using a browser.myvm1
myvm2
myvm1
myvm2
docker-compose.yml
How should I configure port forwarding/networking to be able to access the web server in the cluster from the virtual machine host running the docker container?
Below is a list of commands I have run, some with resulting output.
$ docker-machine create --driver virtualbox myvm1
$ docker-machine create --driver virtualbox myvm2
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
myvm1 - virtualbox Running tcp://192.168.99.100:2376 v18.09.0
myvm2 - virtualbox Running tcp://192.168.99.101:2376 v18.09.0
$ docker-machine ssh myvm1 "docker swarm init --advertise-addr 192.168.99.100"
$ docker-machine ssh myvm2 "docker swarm join --token <my-token-inserted-here> 192.168.99.100:2377"
$ eval $(docker-machine env myvm1)
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
myvm1 * virtualbox Running tcp://192.168.99.100:2376 v18.09.0
myvm2 - virtualbox Running tcp://192.168.99.101:2376 v18.09.0
$ docker stack deploy -c docker-compose.yml getstartedlab
$ docker stack ps getstartedlab
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
it9asz4zpdmi getstartedlab_web.1 mochr/test_repo:friendly_hello myvm2 Running Preparing 18 seconds ago
645gvtnde7zz getstartedlab_web.2 mochr/test_repo:friendly_hello myvm1 Running Preparing 18 seconds ago
fpq6cvcf3e0e getstartedlab_web.3 mochr/test_repo:friendly_hello myvm2 Running Preparing 18 seconds ago
plkpximnpobf getstartedlab_web.4 mochr/test_repo:friendly_hello myvm1 Running Preparing 18 seconds ago
gr2p8a0asatb getstartedlab_web.5 mochr/test_repo:friendly_hello myvm2 Running Preparing 18 seconds ago
of docker-compose.yml
:
version: "3"
services:
web:
image: mochr/test_repo:friendly_hello
deploy:
replicas: 5
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition: on-failure
ports:
- "4000:80"
networks:
- webnet
networks:
webnet:
It appears this is a known issue with the current version of boot2docker : https://github.com/docker/machine/issues/4608
The workaround is to use a cluster based on machines that don't require boot2docker (e.g. AWS, DigitalOcean, etc.), wait until a new version of boot2docker is released, or use an earlier version of boot2docker as described in that link. To use an earlier version:
export VIRTUALBOX_BOOT2DOCKER_URL=https://github.com/boot2docker/boot2docker/releases/download/v18.06.1-ce/boot2docker.iso
Before creating a virtual machine with docker-machine. (delete the existing virtual machine first, then use that export, then delete docker-machine create myvm1
)
You should then be able to start the stack and access the container at 192.168.99.100:4000 or 192.168.99.101:4000 (or whatever IP address is revealed by docker-machine ls)