Home > Enterprise >  How to restart the docker containers automatically when there is a code change?
How to restart the docker containers automatically when there is a code change?

Time:12-19

This is my github workflow code

name: Test
on:
  push:
    branches: [ uat ]

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Build compose containers
      run: |
        cp .envs/.local.example .envs/.local
        docker-compose -f local.yml build web
        docker-compose -f local.yml build postgres
        docker-compose -f local.yml build celeryworker
        docker-compose -f local.yml build redis

I am able to do it manually, but I want to know how to do it automatically.
In my docker compose file I have also given restart=always.

CodePudding user response:

It might be a simple syntax issue.
See "Using activity types and filters with multiple events":

on:
  push:    
    branches: 
      - uat 

The "array [ xx, yy ]" syntax is more for event types: for instance, not for branches:.

CodePudding user response:

Adding “restart=always” in docker compose file means container will get restarted only when container is stopped or exited.

You can try below options which could help in meeting your requirements.

Option 1:Using nodemon

You can make use of nodemon to automatically restart the node server when code is changed . Before that install nodemon in your docker image and make sure it is present. Refer the below URL to install nodemon and then change your CMD in Dockerfile: https://www.npmjs.com/package/nodemon

CMD ["nodemon", "--exec", "npm", "run", "docker-start"]

This is reload your nodejs application whenever codes are changed.

Option 2:Restart Policy in Docker Compose Swarm Mode

The following implementation only works in Docker Compose v3, which introduces the deploy key-value pair in the configuration. Below we can find the different properties to further expand the configuration for restart policies in swarm mode:

  • condition: none, on-failure, or any (default)
  • delay: Duration between restart attempts
  • max_attempts: Maximum number of attempts outside of the restart window
  • window: Duration for determining if a restart is successful

Let's define our restart policies. First, we must make sure we're using Docker Compose v3 by changing the version property:

version: '3'

Once we change the version, we can add the restart_policy property to our services. Similar to the previous section, our message-server container will always restart automatically by providing the any value in the condition:

deploy:
    restart_policy:
        condition: any
        delay: 5s
        max_attempts: 3
        window: 120s

Similarly, we'll add an on-failure restart policy to the product-server:

deploy:
    restart_policy:
        condition: on-failure
        delay: 3s
        max_attempts: 5
        window: 60s

Also, the restart policies in both services include the additional configuration metadata that makes the policies a more robust restart strategy for the containers.

https://www.baeldung.com/ops/docker-compose-restart-policies#restart-policy-in-docker-compose-swarm-mode

  • Related