0

In docker-compose.yml there are two services. Those get the image from a repository.

The first time the services are started with docker-compose up -d. It worked good and after some time the images were stopped because of Docker update and restart.

Then with docker start only one of the images was started. It worked good and after another update and restart(months later), docker-compose up -d is executed. In this case the manually started service previously was recreated and data was lost. First, can this be recovered, and second, is this expected?

Thanks

zhrist
  • 1,169
  • 11
  • 28
  • Although I accepted the only answer, there is a longer explanation. The command 'docker-compose up -d' should not recreate the service if it is not changed, and especially if pull is not done, that gets the changed version of the service. Anyway, it is a complicated situation and I hope you will not get into it, but keep it simple. – zhrist Oct 30 '18 at 19:31
  • you could also share your gathered knowledge and answer your own question – The Fool Nov 01 '18 at 09:39

2 Answers2

2

Well that your data is lost is expected. You need to mount a "volume" to your container and store your data in this volume to keep it. This way it doesn't matter if you destroy your container or use a completely different one. You always can mount your data volume and you are fine. https://docs.docker.com/storage/volumes/ If there is a way to recover? I really don't think so but I'm not 100% sure...

Also maybe check this out for more clarity why the container was rebuild the second time What is the difference between docker-compose up and docker-compose start?

The Fool
  • 16,715
  • 5
  • 52
  • 86
  • You did not notice that I have wrote 'docker start' and not 'docker-compose start'. This means I have used only one of the services in the compose. Then when restart of Docker happened, all services was started with docker-compose up. Maybe last 'docker-compose up' noticed that image of one of the services locally was modified and re-created it, but I was not expecting it to, especially that I was not logged in to the repository. – zhrist Sep 17 '18 at 14:02
  • 1
    @zhrist I did notice it but it doesn't matter with what you started it. As described in the second link. quote: `By default, if there are existing containers for a service, docker-compose up will stop and recreate them (preserving mounted volumes with volumes-from), so that changes in docker-compose.yml are picked up. If you do not want containers stopped and recreated, use docker-compose up --no-recreate. This will still start any stopped containers, if needed.` – The Fool Sep 18 '18 at 19:09
  • This quote is relevant: [docker-compose up](https://docs.docker.com/compose/reference/up/) 'If there are existing containers for a service, and the service’s configuration or image was changed after the container’s creation, docker-compose up picks up the changes by stopping and recreating the containers (preserving mounted volumes). ' This was my initial point, image was not changed. On top of that the image was not changed, docker-compose could not connect to the registry where the image is, so it should not re-create it on two reasons. – zhrist Sep 24 '18 at 14:35
-1

Conclusion here is that with docker-compose up -d you will always loose data inside the container. If the container is stopped, and you use docker-compose start then the data inside container will continue as before. Here by data we mean data inside the container, not on the volume. This can be logs, other application data etc.

So, if data need to be preserved, use volumes as sometimes if container has an recoverable error and can not start/restart, you will loose the data/files inside. You will have to rebuild/docker-comopose up.

zhrist
  • 1,169
  • 11
  • 28