Volume
Understand how to manage volume.
/var/lib/docker/volumes
.Docker volume is used to persist and share the container's data across containers. Folders on your host machine's hard drive are mounted into containers as volumes, which allows the container to write its data into the host volumes.
Volume types
Anonymous and named volumes
Volume Type | Description |
---|---|
Anonymous | The volume will only exist as long as our container exists. Bind mount volumes can prevent some files from being overlapped. |
Named | The volume persists even if you kill or close the container. With this volume, you can't edit the data inside the volume, instead you can use bind mounts for editing |
Bind mounts
The source and the destination must be absolute path
macOS / Linux =
$(pwd):/app
- Windows =
"%cd%":/app
)
The use of bind mounts is good for development, but not for production since production might not have the same folder structure as development. To make managing different environments easier, I recommend the container has the same structure as your local.
Bind mounts allow you to edit or reflect your code changes from local to container. Bind mount volumes can prevent some files from being overlapped. It's great for persistent.
Read-only volume
Read-only volume will prevent the local files from being modified when the files within the container have been modified.
readonly
and :ro
are to indicate that this volume is only for reading