3

I use docker-compose and find following problem:

When I change my code and want to rebuild dockers I use

docker-compose stop
docker-compose build

And then I want to run system by:

docker-compose up

But no new version of code/containers are run but old ones. What to do?

Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345

3 Answers3

5

You could use, docker-compose up --build or docker-compose up --build --force-recreate

brunocascio
  • 1,867
  • 3
  • 14
  • 21
  • 1
    NOT WORKING - I test your solutions and both not working. First thing: I saw if I not run: `docker-compose stop` (before each of your proposition, then I will have multiple running containers in background...). Second thing: the running code was not updated (because docker-composer run old containers - so after build we need to use `docker-compose rm -f` before we run conainers with new code). Last: if we use `stop` and `rm` and then your version `up --build` then we exchange containers with large gap (build time) of non working service. – Kamil Kiełczewski Jun 22 '17 at 21:40
  • Worked for me though – Axalix Dec 07 '17 at 19:59
  • May be some thing change in new versions of docker (and `up --build`)? – Kamil Kiełczewski Dec 07 '17 at 20:22
4

I have a helper function to nuke everything so that our Continuous blah, cycle can be tested, erm... continuously. Basically it boils down to the following:

To clear containers:

docker rm -f $(docker ps -a -q)

To clear images:

docker rmi -f $(docker images -a -q)

To clear volumes:

docker volume rm $(docker volume ls -q)

To clear networks:

docker network rm $(docker network ls | tail -n+2 | awk '{if($2 !~ /bridge|none|host/){ print $1 }}')

I generally don't require old containers, volumes and networks, so to clear them all I made a bash script which runs to clean up docker environment before each build. And to rebuild the docker using updated code, I use docker-compose up --build

Credits to marcelmfs and borrowed from Source

Ayushya
  • 9,599
  • 6
  • 41
  • 57
2

In this case first we should remove old containers (by rm -f). So we can deploy new code by:

docker-compose build
docker-compose stop
docker-compose rm -f
docker-compose up

Above sequence is not coincidence - when first instruction build image, the old images running - but when building is finish then old container is stopped, deleted and exchange by new builded one.

I put above commands in handy copy-paste oneliner:

docker-compose build && docker-compose stop && docker-compose rm -f && docker-compose up

Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
  • Nah, same issue. What actually worked is adding `--build` after `up` – Axalix Dec 07 '17 at 19:54
  • This worked for me after nothing else did, but I have no idea why - I had no services or containers running when I ran the one liner – arshbot Jun 15 '20 at 20:39