Why does my non-volume data in Docker container persist even after restarting the container?
Asked Answered
G

3

14

In some places when I read about Docker containers, I found some people talking that they lose their data (saved inside the container and not a part of volume data) when they restart the container.

I tried to create a simple Ubuntu container like this: docker run -it ubuntu /bin/bash, and created some files inside the container and then restarted it, but my data still there. Why does that actually happen? why do my data still there? Is this something new in the newer versions of Docker or do I have misunderstanding for something?

Goldofpleasure answered 28/11, 2016 at 17:24 Comment(1)
Maybe when people talk about "restarting", they actually mean "recreating".Sale
A
12

The data is lost when the container is removed, not when it's stopped or restarted.

Basically, if you do docker ps, if the containers keeps the same id (the big ugly hexadecimal id), the data is not lost.

It gets complicated when somehow your docker containers are not managed by you, but by some kind of automated-managing method. Tools like these usually start new containers if there is failure. In that case you should mount a volume to store your data on the host.

Austerlitz answered 28/11, 2016 at 17:59 Comment(3)
killed means removed, or?Goldofpleasure
Removed actually. Killing is almost like stopping a container. Stop tries to gracefuly stop the process. Kill doesn't. But they both keep the container in there, so you see them when you type "docker ps". You can restart them, and you will see the files you created. Remove on the other hand will make the container disapear completly, along with all the files inside of it.Austerlitz
great explanation i have posted an answer let me know what you think.Gemma
N
5

You might want to look at the Container Lifecycle: https://github.com/wsargent/docker-cheat-sheet#lifecycle

docker create creates a container but does not start it.

docker rename allows the container to be renamed.

docker run creates and starts a container in one operation.

docker rm deletes a container.

docker update updates a container's resource limits.

If you do docker rm and docker run again your state will not be there anymore.

If you want a transient container, docker run --rm will remove the container after it stops.

Ngocnguyen answered 28/11, 2016 at 17:31 Comment(2)
This is already clear, but I was wondering when they say: "we lost our data when we restarted the container", it is apparently like the comment above, they mean "recreating", not restarting.Goldofpleasure
great explanation i have posted an answer too let me know what you think.Gemma
G
0

So, here is the working of the docker container; docker images build and work in layers when the docker image is built every command adds a layer to the docker image in your case ubuntu is a docker image so when you create a container and when you make changes in container your changes are saved in a writable layer above base image you use which is ubuntu in your case. When you commit the changes along with the new layer it is saved with the new changes as a layer on top of the base ubuntu layer.

Docker image layers are saved with only metadata of the changes made so the changes made are just reflected but the underneath layers still exist this is the reason why when you delete a gaint file from container and form a new image size of the image is not reduced as on ly changes are saved. This is done for space optimization lets say 5 containers use ubuntu as base image so ubuntu is not downloaded 5 times, instead same base image is used but the layers above it have info of the changes so the top most layer of the image is used.

So when you make changes to the ubuntu container the changes are not updated In the image you used to build the container or anywhere in the host system unless volumes configurations are set. The info of the changes are saved in the writable layer of the container and these get deleted when container is removed and are not reflected if a new container is made using the same ubuntu image this is what it means when its said that containers are stateless. If you wish your data to persist you can either commit the changes made after creating the container or you can mount volumes while creating the container for you data to persist.

more info on volumes can be found here:- manage docker data

Gemma answered 19/8, 2023 at 5:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.