8

If Docker is "ephemeral", how is stopping a container different than removing a container? When I stop and I start, that doesn't mean I can save information in that container does it? I thought that was what you didn't want to do (save data in the Container.)

1

3 Answers 3

2

If Docker is "ephemeral", how is stopping a container different than removing a container?

Stopped containers are visible via docker ps -a. If containers are started with the --rm option, they'll remove themselves after stopping.

When I stop and I start, that doesn't mean I can save information in that container does it? I thought that was what you didn't want to do (save data in the Container.)

You're absolutely right. State should never be saved inside a container.

8
  • 2
    If I stop a Docker Container, is State saved? Commented Apr 28, 2017 at 15:33
  • 1
    Well, go look. It depends on how the container was started, which you'd understand if you read my answer. This isn't really a question you should be asking, though, because you should never care about state inside a container. If you do care about it, you're using docker incorrectly. Commented Apr 28, 2017 at 15:34
  • I only care because other than waiting for transactions or the like to complete, I do not see why I couldn't simply remove a running container instead of waiting for it to stop and then remove it Commented Apr 28, 2017 at 15:38
  • 2
    @johnny Please. Just read the documentation. Strive to understand the systems you're working with. Try things. Build up a test system you can play with to try out these things you're asking about. We don't spoon-feed here. If you're wondering if you can skip the "stop" step, just try it! Commented Apr 28, 2017 at 15:46
  • 4
    @EEAA most unuseful answer and subsequent comments. Thanks. Actual answer is here: hhttps://stackoverflow.com/questions/33362856/whats-the-difference-between-docker-stop-and-docker-rm Commented Jun 9, 2020 at 22:32
1

When a container is started, tools like docker create a directory with:

  • the configuration (name, volume mounts, published ports, etc)
  • a container specific filesystem layer and the assembled filesystem from the container layer merged with the image layers
  • log output when using the json or local log drivers

When the container is stopped, the running environment with various namespaces and cgroups is destroyed, but it can be restarted with the previous filesystem state and same configuration.

When the container is removed, you can recreate it, but filesystem changes, logs, and any other configuration data would be lost.

Here's a quick example showing the filesystem persistence:

$ docker run -it --name stop-test busybox /bin/sh / # cat /hello.txt cat: can't open '/hello.txt': No such file or directory / # echo "hello world" > /hello.txt / # cat /hello.txt hello world / # exit $ docker container inspect stop-test --format '{{json .State}}' | jq . { "Status": "exited", "Running": false, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 0, "ExitCode": 0, "Error": "", "StartedAt": "2023-08-16T14:52:49.810997424Z", "FinishedAt": "2023-08-16T14:53:09.900172563Z" } $ docker start stop-test stop-test $ docker attach stop-test / # cat /hello.txt hello world / # exit $ docker rm stop-test stop-test $ docker run -it --name stop-test busybox /bin/sh / # cat /hello.txt cat: can't open '/hello.txt': No such file or directory / # exit $ docker run -it --name stop-test busybox /bin/sh docker: Error response from daemon: Conflict. The container name "/stop-test" is already in use by container "caaad3a5287431dc6a9a924c658723580298d182d4b27bd8ed3fcd6c3690b2ad". You have to remove (or rename) that container to be able to reuse that name. See 'docker run --help'. 

Note that if you want to store data between containers, a volume would be preferred. And if you want to view logs of deleted containers, pushing those logs to an external system, like elastic, would be needed.

0

When stopping a container it is possible to restart the container later again with docker start <ID|name>. If the container was not started with some special options the internal data was retained and one can continue to use it. Can you self test this behavior with the docker registry:

docker run -d -p 5000:5000 --name registry registry:2 # deploy a local docker image curl -X GET http://<youthostname>:5000/v2/_catalog docker stop registry docker start registry curl -X GET http://<youthostname>:5000/v2/_catalog # the content is still there 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.