Skip to main content

Volumes are the preferred mechanism for persisting data generated by and used by Docker containers.

# To create a docker volume:
docker volume create <volume_name>

# To list all created docker volumes:
docker volume ls

# To inspect the details of a created docker volume:
docker volume inspect <volume_name>

# To create a container with a volume attached:
docker container create --name <container_name> --interactive --tty --mount source<volume_name>,target=/<folder_name> <image_name:tag_name>

# To start a container with a volume:
docker run --interactive --tty --name <container_name> --mount source=<volume_name>,target=target=/<folder_name> <image_name:tag_name>

# To start a container using a read-only volume mount:
docker run --interactive --tty --name <container_name> --mount source=<volume_name>,destination=/<folder_name>,readonly <image_name:tag_name>

# To mount volumes from another container:
docker run --interactive --tty --name <container_name> --volumes-from <another_container_name> <image_name:tag_name>

# To delete a docker volume:
docker volume rm <volume_name>

# To automatically remove "anonymous" volumes when the container is removed (NOTE: <volume_name> is a named volume, so it will persisted until manually removed, e.g. `docker volume rm <volume_name>`):
docker run --rm --volume /<anonymous_volume_name> --volume <volume_name>:/<folder_name> <image_name:tag_name>

# To delete all docker volumes at once:
docker volume prune

##
# Bindmounting a Volume
##

# The following command will create a directory called nginxlogs in your current
# user's home directory and bindmount it to /var/log/nginx in the container:
docker run --name=nginx -d -v ~/nginxlogs:/var/log/nginx -p 5000:80 nginx
#
# `--name=nginx` names the container so we can refer to it more easily.
#
# `-d`           detaches the process and runs it in the background. Otherwise,
#                we would just be watching an empty Nginx prompt and wouldn't be
#                able to use this terminal until we killed Nginx.
#
# `-v`           `~/nginxlogs:/var/log/nginx` sets up a bindmount volume that
#                links the `/var/log/nginx` directory from inside the Nginx
#                container to the `~/nginxlogs` directory on the host machine.
#                Docker uses a `:` to split the host's path from the container
#                path, and the host path always comes first.
#
# `-p 5000:80`   sets up a port forward. The Nginx container is listening on
#                port `80` by default. This flag maps the container's port `80`
#                to port `5000` on the host system.
#
# `nginx`        specifies that the container should be built from the Nginx
#                image, which issues the command `nginx -g "daemon off"` to
#                start Nginx.
#
# NOTE: The `-v` flag is very flexible. It can bindmount or name a volume with
# just a slight adjustment in syntax. If the first argument begins with a `/` or
# `~/`, you're creating a bindmount. Remove that, and you're naming the volume.
#
#   -v /path:/path/in/container
#   mounts the host directory, `/path` at the `/path/in/container`
#
#   -v path:/path/in/container
#   creates a volume named path with no relationship to the host.

##
# References
#
# - [Digital Ocean: How To Share Data Between the Docker Container and the Host](https://www.digitalocean.com/community/tutorials/how-to-share-data-between-the-docker-container-and-the-host)
# - [Digital Ocean: How To Share Data between Docker Containers](https://www.digitalocean.com/community/tutorials/how-to-share-data-between-docker-containers)
# - [OSTechNix: Explaining Docker Volumes With Examples](https://www.ostechnix.com/explaining-docker-volumes-with-examples/)
# - [Docker volumes](https://docs.docker.com/storage/volumes/)
###