4

I'm using docker-compose to mange my container 'abc' in a linux server (centos).

my steps that I want to perofrm:

1) docker compose pull 'abc' in order to pull my latest image

docker-compose --file docker-compose.yml pull

2) docker command to check if my container 'abc' need to be updated (possible???)

3) if my container 'abc' I will perform docker-compose up -d

docker-compose --file docker-compose.yml up -d 

Remark: I know that step 3 is checking and than update the container if necessary but I need to perform pre tasks if step 2 return True

dsaydon
  • 4,421
  • 6
  • 48
  • 52

2 Answers2

3

To restate your question as I read it:

Prior to updating a container, do X

Changes Include:

  • Image changes
  • Environment changes for containers
  • Networking changes
  • ..etc

TLDR; What you want is a dry-run feature of docker-compose. You are not alone: https://github.com/docker/compose/issues/1203

More; I think you are out of luck trying to use docker-compose itself for this purpose until this issue is tackled by the developer community. To be completely safe you would want to perform your pre-flight action when anything affecting the running container is about to change.

Potential Direction: If we were to wish for this, we'd build a shell script that does the examination for us, as simple as a diff on the compose file, or more complex as locating each image e.g., grep image docker-compose.yml | sed "s/^.*\: \(.*\),$/\1/g" ... etc and so forth.

Randy Larson
  • 6,817
  • 2
  • 18
  • 13
3

I'm not entirely sure if this will do exactly what you need, but you can try to check if your container needs updating like this:

docker-compose puts a label with a hash of the current configuration into the container config. You can get the configuration hash like this:

docker inspect my-container -f '{{index .Config.Labels "com.docker.compose.config-hash"}}'

You can then compare this hash to the hash of your current docker compose file:

docker-compose config --hash="my-container"

If those hashes differ, the containers probably needs to be rebuild as the configuration has changed.

This will however probably not work if you're using the latest tag and the image itself needs updating. To account for that, you could additionally try How do I check if my local docker image is outdated, without pushing from somewhere else? or Bash parse docker status to check if local image is up to date

Jan Gassen
  • 3,406
  • 2
  • 26
  • 44