After docker-compose build the docker-compose up r

2019-05-31 14:27发布

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?

3条回答
狗以群分
2楼-- · 2019-05-31 14:36

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

查看更多
Luminary・发光体
3楼-- · 2019-05-31 14:37

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

查看更多
倾城 Initia
4楼-- · 2019-05-31 14:37

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

查看更多
登录 后发表回答