Checking image version on Docker Swarm

Canberk Erkmen
Koçfinans Tech
Published in
3 min readDec 17, 2020

--

Although we all know about continuous integration and continuous deployment, I will clarify them shortly.

Continuous integration is the process for building and testing new code when there are changes. Continuous deployment is another process for deploying new versions of an application, which passes the continuous integration process, after passing all stages in the pipeline, without any human intervention.

However, we are using Docker Swarm and facing some problems with these processes such as version control. We have to ensure the version which is deployed in environments since we sometimes realize that different versions are running. Before deploying the application to production, there are api tests, automation tests, cdc tests for checking if code change is valid and meet the quality metrics.

To solve this problem, I have wrote a script on Docker Swarm to check if the application is running or not. And also if the application running then which version it is.

First of all, we need only two parameters from the command line. One of them is SERVICE_NAME for the image and the other one is SERVICE_VERSION for which version is wanted to deploy.

We use docker service ls command for find service name in Docker Swarm with image name. If there isn’t any service with given parameter, script exits with error.

When we found it, we have to determine service mode in the cluster, it can be Global or Replicated. It is necessary to know the replica counts for checking how many instances of the service should run when the deployment is finished.

If service mode is global, counting active node number in the cluster, otherwise we determined the replica count for the service.

Determining all of the parameters which we need, it’s time to check service deployment is a success or not. We use docker service ps to find out services which are running in this version.

If the number of running service instances is equal to the number which is found in the previous step, the service is deployed.

If the two numbers are not equal, wait for SLEEP_TIME and then check again until PROCESS_TIME is finished.

At the end, the script exits depending on SUCCESS parameter.

We can also use this script in Jenkins to ensure that new version is deployed in any environment.

Save the file and it can be run as:

./docker-check-status.sh {{SERVICE_NAME}} {{SERVICE_VERSION}}

Before executing the script, you must make the file executable with chmod 755 docker-check-status.sh command.

You can find the script on GitHub as docker-check-status.sh and such more:

This situation was one the most difficult adventure I faced in Docker Swarm, with bash script I can overcame it easily. I am very pleased to share what I know and it was very enjoyable to write these in a post, I hope it helps you.

See you with some other posts in the future!

--

--