Minh Nghĩa
Minh Nghĩa

Reputation: 1053

Why doesn't Docker container save when exit?

Why Docker is designed that way? I can only think of only one reason: to avoid these 2 errors:

This SO question is perhaps one of the most-read answers for a newbie like me about this behavior when a container "exits". However, it doesn't explain the why.

Upvotes: 0

Views: 620

Answers (1)

BMitch
BMitch

Reputation: 263866

If you are running containers correctly, there is nothing inside the container you want to keep. It should be noted that only tmpfs mounts are deleted on exit of a container. Other changes to the container filesystem are deleted when the container is removed. By replacing containers with new instances, going back to the initial image state, we force changes to go into code that builds the image and remove the management of state drift.

Looking at the various types of things that could be lost, here is where you should be saving those changes:

  • Data: any persistent data should be saved in a volume.
  • Application code and dependencies: these should be stored inside the image itself, built with a Dockerfile, and the container replaced with a new container running from the new image.
  • Configuration files: these can be mounted as single file volumes, read only, and managed on the host. Or with swarm mode, secrets and configs can be injected as files in the container using secret and config objects.
  • Temporary files: by their very definition are temporary and should not be saved. You can use a tmpfs mount for these to have them stored in ram.
  • Log files: These should be written to stdout/stderr, and not into the container. Docker will capture these streams with your selected log driver. For persistence, these logs should be centralized with a tool like Elastic.

Upvotes: 2

Related Questions