Ram Idavalapati
Ram Idavalapati

Reputation: 714

Unknown mode: host in docker stack deploy

My primary goal is to make Nginx service available from only one machine when I expose ports and do stack deploy in docker swarm (Usually we can access exposed service from any machine in the docker swarm cluster). For that I have seen mode: host option.

But when I use it in docker compose file and do stack deploy then I am getting error service nginx: Unknown mode: host.

nginx.yml file

version: "3.4"
services:
    nginx:
        image: ramidavalapati/nginx:tag1
        deploy:
            mode: host
            restart_policy:
                condition: on-failure
            placement:
                constraints: [node.hostname == ram-ThinkPad-E470]
        ports:
            - 80:80
            - 443:443
        volumes:
            - /home/ram/nginxDocker/nginx.conf:/etc/nginx/nginx.conf
            - /home/ram/nginxDocker/logs/access.log:/var/log/nginx/access.log
            - /home/ram/nginxDocker/logs/error.log:/var/log/nginx/error.log
        networks:
            - all
        command: ["nginx", '-g', 'daemon off;']

networks:
    all:
        external: true

Deploy: $ sudo docker stack deploy -c nginx.yml nginx

Upvotes: 3

Views: 5472

Answers (4)

prasanna
prasanna

Reputation: 152

My primary goal is to make Nginx service available from only one machine when I expose ports and do stack deploy (Usually we can access exposed service from any machine in the docker swarm cluster)

What I understood is, publish and expose ports to only one node and access it only from that node.

When you publish and expose ports for a service (in your case Nginx) then by default you can access that service from any of the nodes which are the part of docker swarm.

But if you want your service should only available from one node then you can use your ports section as from

ports:
  - target: 80
    published: 80
    protocol: tcp
    mode: host
  - target: 443
    published: 443
    protocol: tcp
    mode: host

Use mode: host in ports section

This makes your service should only available from the machines in which its containers are running.

Now if you want your service containers should only run on a specific machine then you can use constraints as @Miguel A.C. mentioned.

Upvotes: 6

Lena Weber
Lena Weber

Reputation: 272

In Docker swarm, there are only two modes of service deployments, global and replicated. The default being replicated mode, in this mode you specify the number of identical tasks to run.

A global service runs one task on every node. So in a nutshell, everytime you add an extra node to the swarm, the orchestrator creates a task and the scheduler assigns the task to the new node.

A replicated service has pre-defined number of identical tasks, for example if you have 4 worker nodes, but you define 3 replicas, one worker node will not have a container on it.

Now based on your deployment scenario, you need to use global service deployment mode if you intend to have a task on every node. So the definition will look like this:

deploy:
      mode: global

For replicated, it would be:

deploy:
      mode: replicated
      replicas: 3

Once you expose ports (HOST:CONTAINER) - it will automatically be reflected on all worker nodes. For further reading you can refer to https://docs.docker.com/engine/swarm/how-swarm-mode-works/services/ and https://www.aquasec.com/wiki/display/containers/Docker+Swarm+101.

Upvotes: 1

Miguel A. C.
Miguel A. C.

Reputation: 613

As Docker informs you, host is not a supported mode. Supported modes are global and replicated. In global mode you'll have a container running on each worker node of your Swarm, in replicated mode you'll have a number of containers selected as the number of replicas and those containers can be deployed in the same or different hosts as decided by the scheduler. If you want to deploy only one container in a specific host you can use mode: replicated, set replicas to 1 and use constraints to specify which node you want to use e.g using node.hostname constraint. See docs for examples that may help you to define your file.

Upvotes: 2

Related Questions