Docker Volumes are the mechanism for persiting data.
In thtis article, I would like to share basic knowledge about Docker volume.
π Advantage in Volume
Volumes have some advantages over bind mounts:
- Volumes are asier to back up
- You can easily manage values using Docker CLI commands
- Volumes can be more safely shared among multiple containers
- Volumeβs contents exist outside the lifecycle of a given container
from Use bind mounts | Docker Documentation
π --mount
option
There are -v
, --mount
flag for volume. We should use the --mount
syntax.
Consits of multiple key-value pairs, separated by commas and each consisting of a
tuple.
key | Value |
---|---|
source |
For named vlumes, this is the name of the volume. For anonymous vlumes, this field is omitted. |
destination |
it takes as its value the path where the file or directory will be mouted in the container. |
readonly |
if present, causes the bind mount to be mouted into the container as read-only. |
volume-opt |
it can be specified more than once, takes a key-value pair consisting of the option name and its value |
π Create and manage volume
# Create a volume |
π Start a container with a volume
If you start a container with a volume that does not yet exist, Docker creates the volume. The following example mounts the volume myvol2
into /app/
in the contaienr.
# Start a container with a volume |
Use docker inspect devtest
to verify that the volume was created and mounted correctly. Look for the Mounts
section:
"Mounts": [ |
π€ Start a container with a bind mount
docker run -d \ |
Use docker inspect devtest
to verify that the bind mount was created correctly. Look for the Mounts
section:
"Mounts": [ |
π Volume configuration for docker-compose
version: "3.2" |
π Appendix: Container to host
If you want a Docker container to connect to host machine, please use the follows
- Docker for Mac:
docker.for.mac.localhost
- Docker for Windows:
docker.for.win.localhost
π³ Special Thanks
- https://docs.docker.com/engine/admin/volumes/volumes/#differences-between--v-and---mount-behavior
- https://docs.docker.com/engine/admin/volumes/bind-mounts/#configure-mount-consistency-for-macos
- https://docs.docker.com/compose/compose-file/#volumes
- https://forums.docker.com/t/accessing-host-machine-from-within-docker-container/14248/20
π₯ Recommended VPS Service
VULTR provides high performance cloud compute environment for you.
Vultr has 15 data-centers strategically placed around the globe, you can use a VPS with 512 MB memory for just $ 2.5 / month ($ 0.004 / hour).
In addition, Vultr is up to 4 times faster than the competition, so please check it => Check Benchmark Results!!