2

Let's consider following example:

version: '3'
services:
  some_service:
    build: .
    restart: unless-stopped

This docker-compose works properly, however during restart it preserve changes (in filesystem) introduced in previous running (before restart).
How to configure restart-policy to force loosing changes in fileysystem? Is it possible to change restart-policy on running container?

nrz53378
  • 83
  • 6

2 Answers2

3

docker-compose deploys containers with a configuration to be managed by the docker engine. That restart policy is applied to the container, which is handled by the engine. When the engine restarts a container, the previous container state is maintained. The only exception is a tmpfs filesystem mount inside your container which will reset to an empty directory.

However, if your service is managed by swarm mode instead of docker-compose, the default changes to recreate any failed containers instead of just restarting them. You can configure a single node swarm cluster with:

docker swarm init

And then you can deploy your service with:

docker stack deploy -c docker-compose.yml your-app

Note that with swarm mode you do not need to define the restart policy. Swarm mode will correct any difference from the target state, whether the container exits, fails a health check, or gets deleted, the response will be to deploy a new container.

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • Thanks for you answer. Obviously it make sense in case of SWARM. After all, container can be rescheduled to another node - we haven't distributed filesystem. You are welcome here: https://stackoverflow.com/questions/53564063/how-docker-compose-remove-containers – nrz53378 Nov 30 '18 at 19:54
0

When you docker-compose up, you create "containers", they have their own file system. And when you restart a container, it's still the same container with the same file system. So what you have is expected.

If you want a fresh restart, you need to do docker-compose down to remove containers and then up to create new ones.

You can't change restart policy on a running container.

EDIT

@BMitch You can change restart policy with docker update

docker update --restart=on-failure container_id
Siyu
  • 11,187
  • 4
  • 43
  • 55