0

I have a docker-compose image running:

version: '2'
services:
  db:
    image: postgres:commit1
  service:
    image: service:commit1
    ports:
      - "3000:3000"
    depends_on:
      - db

The image has a tag of commit id of git. If anything changes in code, CI/CD pipeline runs and updates the image with latest commit id.

Now let's say I have images as: postgres:commit2 and service:commit2.

What is the best procedure to update the images given the containers are running using commit1 in the compose file?

Do I need to update the images manually in compose and then:

docker-compose restart

And remove the other containers manually?

Is it the best way?

2 Answers 2

2

One way is to templatize your compose file and have a separate CI/CD step to generate a new file with the new image tags on every build.

For ex. -

// docker-compose.yml.template

version: '2'
services:
  db:
    image: postgres:{{ COMMIT_ID }}
  service:
    image: service:{{ COMMIT_ID }}
    ports:
      - "3000:3000"
    depends_on:
      - db

and a using sed or awk script you can replace {{ COMMIT_ID }} with the latest commit id and generate the new file.

// docker-compose.yml

version: '2'
services:
  db:
    image: postgres:commit2
  service:
    image: service:commit2
    ports:
      - "3000:3000"
    depends_on:
      - db

Then you can finally pull and use latest images using docker-compose pull && docker-compose up -d

Sign up to request clarification or add additional context in comments.

Comments

1

I am assuming you are looking for some option to do rolling update with docker-compose and your service support multiple instances. This can be achieved by, first changing the image id in docker-compose file, you can use your CI/CD tool to create new docker-compose file with updated commit hash.

Then you can use below command add new containers(with new new image) behind the services.

docker-compose up --scale db=1 --scale service=1 --no-recreate

Then next step would be to delete the old containers

   docker rm old-container # service

Then the last step would be to scale the services to the number of instances you want.

This is the best I can think of for just docker-compose. If you were using docker swarm or any other system it would have been out of box feature :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.