Website available in standalone container, not in swarm

主宰稳场 提交于 2020-01-24 21:06:05

问题


I have Docker CE running on windows server 2016 with 2 images.

when I run these in containers, everything is fine.

docker run --detach --name Website1 --publish 94:94 webimage1
docker run --detach --name Website2 --publish 95:95 webimage2

I can access through browser on other PCs:

  • http://host:94/page1.aspx
  • http://host:95/page1.aspx

Now I want to run them in swarm.

I've gone through docker tutorial and 've set up docker-compose file, with services, port mapping. The setup has Website 1 of 1 replica, Website 2 of 2 replicas.

On docker stack services websites port numbers show up as follows.

Website1: *:94->94/tcp
Website2: *:95->95/tcp

but i can't access any of them with following url's:

  • http://host:94/page1.aspx
  • http://host:95/page1.aspx

I get - This site can't be reached

If I go back to one of my running containers, I see that the port number has a different format.

0.0.0.0:94->94/tcp (WORKING) VS *:94->94/tcp (NOT WORKING)

For initilizing docker swarm I used docker swarm init with IP address of the host on port 2377.

Here is how I deployed docker stack using compose file

docker stack deploy --compose-file docker-stack.yml websites

docker-stack.yml file for reference.

version: "3"
services:

  website1:
    image: website1:latest
    ports:
      - 94:94
    depends_on:
      - website2
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure
        delay: 10s
        max_attempts: 3
        window: 120s
  website2:
    image: website2:latest
    ports:
      - 95:95
    deploy:
      mode: replicated
      replicas: 2
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure
        delay: 10s
        max_attempts: 3
        window: 120s

any guidance would be greatly appreciated.

many thanks


回答1:


From the conversations in the comments the problem is with the stack.

Docker stack supports only images for deployment and replicas. I believe your images are custom ones which are neither in dockerhub nor in any private repo. So when you try to deploy it in stack the service which is getting deployed in the worker node doesn't find such image and is unable to download it from repo. Hence the service won't start in worker node. It works perfectly in manager node because the image already exists there.

So, you have to either set up a local/private registry or push the images to docker registry or else you can even copy the images from manager node to worker node using docker save and docker load and then try using swarm and deploy in stack it will work.

Please note when working with swarm and registries, While deploying stack using docker stack deploy -c composefile.yml test you have to pass --with-registry-auth if you are using authentication for registries as docker stack deploy -c composefile.yml test --with-registry-auth else other nodes may not authenticate with the registry which will result in failure to download images if not found.

Also please note if you set up a local private repo without self signed certificate or with self signed certificate you may need to configure insecure registry. I've given reference of the same.

I recommend setting up a local repo without any authentication and certificates and access it by adding insecure registry in daemon.json file for testing purposes.

Now as per the last comment where you removed swarm and tried running docker service using

docker service create --replicas 2 --name contentlinksapi --publish mode=host,target=94,published=94,protocol=tcp contentlinksapi

It throwed port already in use because it tries to create 2 replicas in the same machine. Where the first replica binds to port 94 because of which, The second replica throws port already in use error.

For your reference.

  1. Deploy a registry server
  2. Test an insecure registry
  3. Docker service mode (check to know why services with two replicas deployed in same host on docker service create)
  4. Docker save
  5. Docker load


来源:https://stackoverflow.com/questions/52349501/website-available-in-standalone-container-not-in-swarm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!