379

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?

10 Answers 10

410

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

8
  • 1
    Yeah, actually this is what I do in CI as well. Not sure why I didn't mention that... Sep 16, 2015 at 21:13
  • 2
    if you run docker-compose pull before docker-compose rm -f you can save even more time Oct 14, 2016 at 14:43
  • 2
    What is the the -d flag at the end do? Jan 12, 2017 at 15:44
  • 4
    "-d Detached mode: Run containers in the background,"
    – dnephin
    Jan 13, 2017 at 16:47
  • 4
    This did not work in Mac OS. Nov 5, 2021 at 6:55
360

The only solution that worked for me was the --no-cache flag:

docker-compose build --no-cache

This will automatically pull a fresh image from the repo. It also won't use the cached version that is prebuilt with any parameters you've been using before.

5
  • 1
    In addition, under Windows 10 it can help to set the DNS server in the settings from Automatic to Fixed or from Fixed to Automatic.
    – qräbnö
    Apr 15, 2018 at 15:42
  • 3
    Worked for me on OS X building with docker-comopse version 2.
    – RoboBear
    Aug 5, 2018 at 23:19
  • 2
    Worked on OS X docker.
    – SmallChess
    Sep 28, 2019 at 15:13
  • 1
    This worked for me in Mac OS as well. The accepted answer did not. Nov 5, 2021 at 6:55
  • 2
    This does not work in docker-compose 1.29.2.
    – Rob
    Feb 12, 2022 at 15:28
117

By current official documentation there is a shortcut that stops and removes containers, networks, volumes, and images created by up, if they are already stopped or partially removed and so on, then it will do the trick too:

docker-compose down

Then if you have new changes on your images or Dockerfiles use:

docker-compose build --no-cache

Finally:docker-compose up

In one command: docker-compose down && docker-compose build --no-cache && docker-compose up

2
  • 4
    docker-compose build --no-cache is needed only if there are changes on Dockerfiles. Apr 23, 2019 at 13:53
  • Indeed, Victor. Thanks! I thought that it was also necessary after updating a module/application that is executed when the container starts up. For these cases, before running docker-compose up, is necessary to rebuild the services with docker-compose build.
    – ivanleoncz
    Apr 23, 2019 at 14:09
48
docker-compose up --build  # still use image cache

OR

docker-compose build --no-cache  # never use cache
7
  • 18
    When possible, please make an effort to provide additional explanation instead of just code. Such answers tend to be more useful as they help members of the community and especially new developers better understand the reasoning of the solution, and can help prevent the need to address follow-up questions.
    – Rajan
    May 21, 2020 at 9:55
  • 5
    Actually this answer was the most clean and clear to me.
    – Gigino
    Mar 17, 2021 at 8:53
  • 2
    My understanding is that the --build command rebuilds, but uses cached parts, and thus not fully "recreates" the containers. Only the second does what I want.
    – Olindholm
    Jun 11, 2021 at 8:58
  • Doesnt work on docker on centos 7, had to rm -rf /var/lib/docker
    – Nick M
    Jul 9, 2021 at 21:17
  • 6
    Two commands are completely different! docker-compose up --build uses cache images if it is exist. docker-compose build --no-cache never uses cache. Nov 24, 2021 at 10:41
34

You can pass --force-recreate to docker compose up, which should use fresh containers.

I think the reasoning behind reusing containers is to preserve any changes during development. Note that Compose does something similar with volumes, which will also persist between container recreation (a recreated container will attach to its predecessor's volumes). This can be helpful, for example, if you have a Redis container used as a cache and you don't want to lose the cache each time you make a small change. At other times it's just confusing.

I don't believe there is any way you can force this from the Compose file.

Arguably it does clash with immutable infrastructure principles. The counter-argument is probably that you don't use Compose in production (yet). Also, I'm not sure I agree that immutable infra is the basic idea of Docker, although it's certainly a good use case/selling point.

3
  • Thanks for the answer. I think it would be really useful to force it at the configuration level, eg. to enforce it for a database container and disable recreation by default for the app containers.. Sep 17, 2015 at 8:06
  • 16
    --force-recreate doesn't work for me ... Image is not pulled even though a newer version is out there...
    – lisak
    May 24, 2016 at 12:09
  • 4
    @lisak I never said it pulled new images. It doesn't. It just starts new containers using whatever image is locally available. You'll need to run docker pull manually. Aug 20, 2016 at 13:43
6
docker-compose up --build --force-recreate
3

I claimed 3.5gb space in ubuntu AWS through this.

clean docker

docker stop $(docker ps -qa) && docker system prune -af --volumes

build again

docker build .

docker-compose build

docker-compose up

3

Also if the compose has several services and we only want to force build one of those:

docker-compose build --no-cache <service>
1
  • This does not work in docker-compose 1.29.2.
    – Rob
    Feb 12, 2022 at 15:28
1

together with --force-recreate, you might want to consider using this flag too:

 -V, --renew-anon-volumes   Recreate anonymous volumes instead of retrieving
                            data from the previous containers.

I'm not sure from which version this flag is available, so check your docker-compose up --help if you have it or not

-18
$docker-compose build

If there is something new it will be rebuilt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.