3

I am following the Docker tutorial and do it in my own version

version: "3.1"
services:
    web:
        image: registry.gitlab.com/xxxx/xxxx:latest
        deploy:
             replicas: 2
        ports:
             - "8888:80"
    mysql:
        image: mysql:latest
        environment:
           MYSQL_ROOT_PASSWORD: password
           MYSQL_USER: user
           MYSQL_PASS: password
        ports:
             - "8889:3306"
        volumes: 
           - mysql-data:/var/lib/mysql
volumes:
   mysql-data:

Whenever I change some code, I rebuild new docker image and run update.

docker stack deploy --compose-file docker-compose.yml xxxx-learn 

Then I noticed some downtime. They will start new container one at the time and stop old container one at the time. Then problem is that it takes a few minutes to download new image and it takes time for web server to run.

One solution that I was thinking of is to run Nginx load balancing in front of those two web server replicas. But is there any better solution?

Monkey D Luffy
  • 405
  • 1
  • 3
  • 8

2 Answers2

2

You should put restart policy and stop_grace_period on your compose file:

version: "3.1"
services:
    web:
        stop_grace_period: 10s
        deploy:
             replicas: 2
             restart_policy:
               condition: on-failure
dtelaroli
  • 121
  • 3
2

Then I noticed some downtime. They will start new container one at the time and stop old container one at the time. Then problem is that it takes a few minutes to download new image and it takes time for web server to run.

You need to define a healthcheck for your image/container. Without that, docker doesn't know when your application is ready to serve requests and will send requests to the "not yet ready" container, and take down the remaining running container shortly after replacing the first one.

The healthcheck defines a command to run inside your container to identify if your application is healthy. See this documentation for how to configure the healthcheck inside your image.

BMitch
  • 5,966
  • 1
  • 25
  • 32