How to use YAML file with docker swarm?

Hello,
I have three servers and I ran the following command on the manager node:

# docker swarm init --advertise-addr 172.20.2.53

I ran the following command on the other two nodes:

# docker swarm join --token SWMTKN-1-0rmswotsbvzaybcujrveigtr7ln5fcywsjtdn02gwdykpt468m-8skbceyft79pho1gz6xh74gx6 172.20.2.53:2377

I got the following message on both nodes:

This node joined a swarm as a worker.

The final result is as follows:

# docker node ls
ID                            HOSTNAME   STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
jd3rf7qz4hnzb9c2z1zh006l1 *   Manager    Ready     Active         Leader           26.1.3
trazxqj2wvg47as0ogr5kv2ow     Node1      Ready     Active                          26.1.3
0rlvf0m07bkuleuwhxf28k9xj     Node2      Ready     Active                          26.1.1

I executed the following command on the manager node:

# docker service create --name Swarm_Mode --replicas 3 -p 80:80 nginx

The result is as follows:

# docker service create --name Swarm_Mode --replicas 3 -p 80:80 nginx
owu0svssay8gue7auwmkga6ch
overall progress: 3 out of 3 tasks 
1/3: running   [==================================================>] 
2/3: running   [==================================================>] 
3/3: running   [==================================================>] 
verify: Service owu0svssay8gue7auwmkga6ch converged 

After this, I checked the running services on all three servers:

On manager:
# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS              PORTS     NAMES
da8c79a563ef   nginx:latest   "/docker-entrypoint.…"   29 seconds ago       Up 22 seconds       80/tcp    Swarm_Mode.2.xp650jms8broke4vg5grjr8bx
8d2cb2e3aed4   nginx:latest   "/docker-entrypoint.…"   About a minute ago   Up About a minute   80/tcp    Swarm_Mode.3.9o3zi2ls2xybqwovti3lq62in

On Node1:
# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS     NAMES
365eca9ef5af   nginx:latest   "/docker-entrypoint.…"   2 minutes ago   Up 2 minutes   80/tcp    Swarm_Mode.1.y4d3h522jhpfv00gq5pdj4d9e

On Node2:
# docker ps
#

I have some questions:

1- Why is there no container running on Node2?

2- I have a YAML file like below:

services:
    nodejs-1:
      container_name: Node-Micro1
      hostname: Node1
      build:
         context: .
         dockerfile: Dockerfile1
      command: npm start
      volumes:
        - ./www:/usr/src/app
        - "/var/run/docker.sock:/var/run/docker.sock"
      expose:
        - "3000"
      ports:
        - '3000:3000'

    nodejs-2:
      container_name: Node-Micro2
      hostname: Node2
      build:
         context: .
         dockerfile: Dockerfile2
      command: npm start
      volumes:
        - ./www:/usr/src/app
        - "/var/run/docker.sock:/var/run/docker.sock"
      expose:
        - "3000"
      ports:
        - '3001:3000'

    nginx:
      image: nginx:latest
      container_name: Nginx-Micro
      ports:
        - '80:80'
      volumes:
        - ./default.conf:/etc/nginx/conf.d/default.conf
        - "/var/run/docker.sock:/var/run/docker.sock"
        - ./www:/usr/share/nginx/html
      depends_on:
        - nodejs-1
        - nodejs-2
      links:
        - nodejs-1
        - nodejs-2

Should the YAML file and project source code be on the manager node?

Thank you.

It looks like the Swarm_Mode service that you created with the docker service ‘create command‘ is currently running only on the manager node and Node1. There could be various reasons why Node2 doesn’t have any containers running:

Review the output of docker service ps Swarm_Mode to check if there are any failed tasks linked to Node2.

Ensure that Node2 has sufficient CPU, memory, and disk space resources available for running containers.

Confirm that Node2 is correctly connected to the Docker swarm and functioning as a worker node. You can verify this by inspecting the output of docker node is on the manager node.

Examine the logs of the Docker daemon on Node2 (usually accessible via journalctl -u docker.service on most Linux distributions) to identify any errors or issues that might be preventing container deployment.

Other than that, more or less, this config looks fine, but remember that the Dockerfiles (Dockerfile1 and Dockerfile2) and any other required files (default.conf) are present and correctly configured in your project dir.

1 Like