Docker Swarm Series: #5th Troubleshooting
Tags: docker, swarm, stack
Published: 2023-07-12 20:09:36

--- series: Swarm ---
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5dhscpvf2hb3mx334z13.png)
<br>



# Inception <br><br>

Hello everyone, This article is part of The Swarm series, The knowledge in this series is built in sequence, Check-out The Swarm series section above.

At the [last article](https://dev.to/mohamedeleraki/docker-swarm-series-4th-deploy-a-stack-to-a-swarm-cluster-36eg) we covered How to Deploy a Stack to a swarm cluster and the value behind The Stack, And deployed a simple Nignx web-app, and mysql data-base using Docker compose YAML file, Stack deploy command, And [Play-with-docker](https://labs.play-with-docker.com/) lab.

----------------------------------------------------------
<br><br>
# Overview <br><br>

In This article We will complete The Swarm tutorials from the stand-up point Which is the containers stack went through errors and cannot be start, So in this article will go through the way of troubleshooting step-by-step, and find-out how to find the exact issue. in this lab Also will use [Play-with-docker](https://labs.play-with-docker.com/) lab.

----------------------------------------------------------
<br><br>
# Setup the environment<br><br>

To start troubleshooting first we need to redeploy the [last Stack](https://dev.to/mohamedeleraki/docker-swarm-series-4th-deploy-a-stack-to-a-swarm-cluster-36eg), Or simply deploy the YAML file below on [play-with-docker](https://labs.play-with-docker.com/#) lab:<br>

```bash
version: '3'

services:  # services list
  nginx:  # service name
    image: nginx:latest  # specify an image with it's tag
    ports:  # defining ports
      - "8080:80"
    volumes:  # mount volume disk | mount nginx.conf stored on local device to nginx container
      - ./nginx.conf:/etc/nginx/nginx.conf

    # establish connection to mysql container by mention the defined variables at mysql environment below
    environment:
      MYSQL_HOST: mysql
      MYSQL_PORT: 3306
      MYSQL_DATABASE: myapp
      MYSQL_USER: root
      MYSQL_PASSWORD: password

    # The build of this container in depends on mysql
    depends_on:
      - mysql

  mysql:
    image: mysql:latest
    volumes:
      - ./data:/var/lib/mysql

   # Define mysql variables
    environment:
      MYSQL_DATABASE: myapp
      MYSQL_USER: root
      MYSQL_PASSWORD: password
      MYSQL_ROOT_PASSWORD: password
```

<br>
**Deploy**

```bash
docker stack deploy -c docker-stack-file.yaml myapp
```

<br>
**Print-out the deploy service status**

```bash
docker stack services myapp
```<br>


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mkalvp30ih9z05p82gt2.png)



----------------------------------------------------------
<br><br>

<br><br>

# Troubleshooting <br><br>

When print-out the services of **myapp** stack you will find out that the replica table is 0/1 which means that the desired state of replica for this container service is set to 1 however the actual state is 0, That means the container service isn't deployed yet. 

Docker stack will try to deploy these services all the time, however it's the same result, let's figure-out where is the issue.

----------------------------------------------------------
<br><br>
## Down we go<br><br>


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i9xdqyrsilvkjcad0dgo.jpg)<br><br>


- Run the following To get the deployed stacks<br>

```bash
docker stack ls
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mwkk5w0p7zwterai80rq.png)


_we have one stack called **myapp** with two services._

----------------------------------------------------------
<br><br>


- Run the following to print-out the services of that stack<br>

```bash
docker stack services myapp
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pmpgwwokayaab8hxkrl7.png)<br>

As mentioned above, the actual state is 0, let's find out **why**....

----------------------------------------------------------
<br><br>


- Run the following to print-out the services with more info<br>

```bash
docker stack ps myapp
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/20gydrmvkb1sxaafxxn3.png)

----------------------------------------------------------
<br><br><br>



<br><br>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/725vcv1y362u3ayx0jm1.jpg)


Ok..Okay, Now let's explain what we're seeing here.<br>

Let's focus on the highlighted in yellow at red square area, At the first column is for **_container service ID_**, the second for the **_container service name_**, the fourth is for **_Node name_** that host this service, Next to it is **_Desired state_** column with **Ready** status that means the Swarm was trying to deploy on this host while _**rejected**_ at the next column which is **_Current state_**, why is that happened let's view the last column **_Error_**.

----------------------------------------------------------
<br><br>


However the error column **isn't wide enough** let's expand it:

```bash
docker stack ps --no-trunc myapp
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ce26f4se91svhxt1prng.png)

----------------------------------------------------------
<br><br><br><br>


<br><br><br>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nl5wmt9j44j6wjl64ly1.png)

**Yes, But** There are too many services, let's focus on Nginx service by running the following:

```bash
docker service ps --no-trunc myapp_nginx
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4uwwmaf05cmks37m8t5f.png)

Yeah, Now the error is clear enough **_"bind source path does not exist: /root/nginx.conf"_** <br><br>

Actually we didn't create an **_Nginx.conf_ file** to mount it inside the container, Let's fix this out.<br>

- Create a very simple **_nginx.conf_** file at the same path of the YAML file:

```bash
vim nginx.conf

# past the below in it
worker_processes 1;

events {
  worker_connections 1024;
}

http {
  server {
    listen 80;
    server_name example.com;
    root /var/www/html;

    location / {
      try_files $uri $uri/ =404;
    }
  }
}
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qdpk8krxlguidnpfrh1p.png)<br><br>

- Update the stack by running the following:

```bash
docker stack deploy -c docker-stack.yaml myapp
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mt8zkvbq12px0q855m3o.png)<br><br>

- Yeah, Now the container service is running on manager node 2:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ibmq4w51pof20qhf9p43.png)<br>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i36sz3kjqjdrtyat9zm0.png)<br><br>


- _Optional_, ssh on manager 2 and run the following:

```bash
# ssh remote
ssh root@manager2

# print containers list
docker container ls
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3uu7gqhjkv650kn9y5g4.png)<br>

- Let's make a simple somke-test by running curl:

```bash
curl localhost:8080
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8n2eaocfd10hdal0ztdw.png)<br>


----------------------------------------------------------
<br><br><br><br>

# spotlight<br><br>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qotan1bu1wfmtibxppw8.jpeg)<br><br>


Here we did troubleshooting on **myapp_nginx** service, if you do troubleshoot the **myapp_mysql** service will find-out it's the same issue. However it more _complex_ and there's no time to discuses here.<br><br>


The Issue that we faced here it was with failed to **start-up** the service, and we noticed that while **print-out** the service status using `docker service ps --no-trunc myapp_nginx` command, **What if** the container service start successfully however, the app that hosted inside the container service _-Nginx webapp in our case-_ have an issue that obstacle its progress, here you should go to the next **layer of troubleshooting** which is the **container service logs**, fetch the service logs by running the following:

```bash
docker service logs <service-name>

# for a specific task
docker service logs <service-name> --task <task-id>

# for interactive session
docker service logs --follow --tail 100 <service-name>

# or land on the hosted node and run:
docker container logs <container-id>
```

Another way, You can land-on the hosted node and remote on the container using `docker exec` command and troubleshoot the logs.


----------------------------------------------------------
<br><br>


That's it, Very straightforward, very fast🚀. 
Hope this article inspired you and will appreciate your feedback. Thank you.