dockerdocker-compose

How to get docker-compose to always re-create containers from fresh images?


My docker images are built on a Jenkins CI server and are pushed to our private Docker Registry. My goal is to provision environments with docker-compose which always start the originally built state of the images.

I am currently using docker-compose 1.3.2 as well as 1.4.0 on different machines but we also used older versions previously.

I always used the docker-compose pull && docker-compose up -d commands to fetch the fresh images from the registry and start them up. I believe my preferred behaviour was working as expected up to a certain point in time, but since then docker-compose up started to re-run previously stopped containers instead of starting the originally built images every time.

Is there a way to get rid of this behaviour? Could that way be one which is wired in the docker-compose.yml configuration file to not depend "not forgetting" something on the command line upon every invocation?

ps. Besides finding a way to achieve my goal, I would also love to know a bit more about the background of this behaviour. I think the basic idea of Docker is to build an immutable infrastructure. The current behaviour of docker-compose just seem to plain clash with this approach.. or do I miss some points here?


Solution

  • docker-compose up --force-recreate is one option, but if you're using it for CI, I would start the build with docker-compose rm -f to stop and remove the containers and volumes (then follow it with pull and up).

    This is what I use:

    docker-compose rm -f
    docker-compose pull
    docker-compose up --build -d
    # Run some tests
    ./tests
    docker-compose stop -t 1
    

    The reason containers are recreated is to preserve any data volumes that might be used (and it also happens to make up a lot faster).

    If you're doing CI you don't want that, so just removing everything should get you want you want.

    Update: use up --build which was added in docker-compose 1.7