Get the App
SLTechnology News&Howtos  ›  Servers  › 

Brief introduction of Docker data persistence

Shulou Source: shulou.com Published: 2022-06-02 06:29:14 10月04日 Update

There are two ways to persist data in Docker:

Data volume (Data Volumes)

Mount host directory (Bind mounts)

Data volumes:

Data volumes can be shared and reused between containers

Changes to the data volume will take effect immediately

Updates to data volumes do not affect mirroring

The data volume will always exist by default, even if the container is deleted

$docker volume create my-vol # create a data volume

$docker volume ls # to see which volumes are available

$docker volume inspect my-vol # View volume details

$docker run-d-P-- name web-v my-vol:/wepapp training/webapp python app.py # Mount the data volume to the container's / webapp directory

When using the-P flag, Docker randomly maps a port of 49000 to 49900 to a network port open in the internal container.

$docker inspect web # View container details

$docker rm-f web # forcibly delete a running container

$docker volume rm my-vol # delete volume

$docker volume prune # Clean up useless volumes

Mount the host directory:

$docker run-d-P-- name web-v / src/webapp:/opt/webapp training/webapp python app.py # Mount the host's / src/webapp directory to the container's / opt/webapp directory

$docker run-d-P-- name web-v $HOME/.bash_history:/root/.bash_history training/webapp python app.py # Mount the host file $HOME/.bash_histor to the container

Tags: Data container directory host port details between file mode tag network mirror open force influence update run Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Redmi MySQL macOS Docker