I enjoy a lot using docker-compose.
Eg. on my server, when I want to update my app with minor changes, I only need to git pull origin master && docker-compose restart
, works perfectly.
But sometimes, I need to rebuild (eg. I added an npm dependency, need to run npm install
again).
In this case, I do docker-compose build --no-cache && docker-compose restart
.
I would expect this to :
- create a new instance of my container
- stop the existing container (after the newer has finished building)
- start the new one
- optionally remove the old one, but this could be done manually
But in practice it seems to restart the former one again.
Is it the expected behavior?
How can I handle a rebuild and start the new one after it is built?
Maybe I missed a specific command? Or would it make sense to have it?
Though the accepted answer shall work to rebuild the container before starting the new one as a replacement, it is ok for simple use case, but the container will still be down during new container initialization process. If this is quite long, it can be an issue.
I managed to achieve rolling updates with
docker-compose
(along with anginx
reverse proxy), and detailed how I built that in this github issue: https://github.com/docker/compose/issues/1786#issuecomment-579794865Hope it can help!
Use the
--build
flag to theup
command, along with the-d
flag to run your containers in the background:This will rebuild all images defined in your compose file, then restart any containers whose images have changed.
-d
assumes that you don't want to keep everything running in your shell foreground. This makes it act more likerestart
, but it's not required.Don't manage your application environment directly. Use deployment tool like Rancher / Kubernetes. Using one you will be able to upgrade your dockerized application without any downtime and even downgrade it should you need to.
Running Rancher is as easy as running another docker container as this tool is available in the Docker Hub.
from the manual docker-compose restart
you should be able to do
The
--no-deps
will not start linked services.The problem is that
restart
will restart your current containers, which is not what you want.As an example, I just did this
docker-compose build
to build the imagesdocker-compose down
1 anddocker-compose up
docker-compose restart
will NOT work heredocker-compose start
instead also does not workTo be honest, i'm not completly sure you need to do a
down
first, but that should be easy to check.1 The bottomline is that you need to callup
. You will see the containers of unchanged images restarting, but for the changed image you'll seerecreating
.The advantage of this over just calling
up --build
is that you can see the building-process first before you restart.1: from the comments; down is not needed, you can just call
up --build
. Down has some "down"-sides, including possible being destructive to your (volume-)data.You can use Swarm. Init swarm first by
docker swarm init
command and usehealthcheck
in docker-compose.yml.Then run below command:
docker stack deploy -c docker-compose.yml project_name
instead of
docker-compose up -d
.When docker-compose.yml file is updated only run this command again:
docker stack deploy -c docker-compose.yml project_name
Docker Swarm will create new version of services and stop old version after that.