×
☰ See All Chapters

Manage data in Docker

Docker containers do not save the data they produce. As soon as the process is finished, the container stops and everything inside of it is removed. The data doesn’t persist when that container no longer exists, and it can be difficult to get the data out of the container if another process needs it. If you want to have persistent data that is stored even after the container stops, you need to enable sharing storage volumes.

Docker has two options for containers to store files in the host machine, so that the files are persisted even after the container stops:

  • volumes 

  • bind mounts 

bind mounts

When you use a bind mount, a file or directory on the host machine is mounted into a container. The file or directory is referenced by its absolute path on the host machine.

docker run -it --mount src='pathToTheFileOrDirectoryOnTheDockerHost', target=/container/directory, type=bind IMAGE[:TAG|@DIGEST] [COMMAND] [arg0 arg1...]

volumes

When you use a volume, a new directory is created within Docker’s storage directory on the host machine, and Docker manages that directory’s contents.

docker run -v /host/directory:/container/directory IMAGE[:TAG|@DIGEST] [COMMAND] [arg0 arg1...]

 


All Chapters
Author