Search code examples
dockerdocker-compose

How to achieve a rolling update with docker-compose?


I have a following setup in docker compose

  • nginx for proxying to frontend, backend and serving static content
  • backend app on port 8080 (spring boot)
  • frontend app on port 4000 (node for SSR)
  • mysql used by backend

Frontend can be updated relatively fast using

docker-compose up -d --no-deps frontend

Unfortunately backend takes about 1 minute to start.

Is there an easy way to achieve lower downtime without having to change the current setup too much? I like how simple it is right now.

I would imagine something like this:

  1. Start a new instance of backend
  2. Wait till it starts (it could be per timer or a healthtest)
  3. Close the previously running instance

Solution

  • Here is the script I've ended up using:

    PREVIOUS_CONTAINER=$(docker ps --format "table {{.ID}}  {{.Names}}  {{.CreatedAt}}" | grep backend | awk -F  "  " '{print $1}')
    docker-compose up -d --no-deps --scale backend=2 --no-recreate backend
    sleep 100
    docker kill -s SIGTERM $PREVIOUS_CONTAINER
    sleep 1
    docker rm -f $PREVIOUS_CONTAINER
    docker-compose up -d --no-deps --scale backend=1 --no-recreate backend
    docker-compose stop http-nginx
    docker-compose up -d --no-deps --build http-nginx