Skip to content
SumGuy's Ramblings
Go back

Docker Volume Mounts: Essential Flags

Each Docker mount point flag serves specific scenarios that can enhance the functionality, security, and efficiency of containerized applications. By understanding and applying these flags appropriately, developers and system administrators can optimize their Docker environments to suit various operational needs, from simple web hosting to complex, distributed applications in a microservices architecture.

1. Read-Only (:ro)

Scenario: Hosting Static Websites Suppose you are deploying a static website using an Nginx Docker container. The website’s files (HTML, CSS, JavaScript) do not need to be modified by the container once deployed.

Example:

docker run -d -p 80:80 -v /host/webdata:/usr/share/nginx/html:ro nginx

Explanation:

2. Read-Write (:rw)

Scenario: Application Logging Imagine you have an application that needs to write logs persistently so that logs are retained even after the container restarts or is removed.

Example:

docker run -d -v /host/applogs:/app/logs:rw myapp

Explanation:

3. Shared (:shared)

Scenario: Development Environment In a development environment, multiple containers might need to access and modify a common set of files, such as source code or resources.

Example:

docker run -d -v /host/project:/workspace:shared coder1
docker run -d -v /host/project:/workspace:shared coder2

Explanation:

4. RShared (:rshared)

Scenario: Kubernetes Volume Propagation In a Kubernetes cluster, you might need to ensure that any mounts created by a container in a pod are visible to other pods, possibly on different nodes.

Example:

docker run -d -v /host/data:/data:rshared global-data-provider

Explanation:

5. :z and :Z

These flags are used to automatically adjust the SELinux labels of the host files to allow containers to read and write them.

Example:

docker run -v /host/path:/container/path:z nginx

6. --mount Option

While not a flag, the --mount syntax provides more explicit and verbose options compared to the -v or --volume syntax. It supports different types of mounts (like volume, bind, or tmpfs), and allows for additional settings such as readonly.

Example:

docker run --mount type=bind,source=/host/path,target=/container/path,readonly nginx

7. --tmpfs

This option mounts a temporary file storage in the container’s filesystem. It can be used to store temporary application data without persisting it to disk.

Example:

docker run --tmpfs /tmp nginx

8. --volumes-from

This flag allows one container to use the volume of another container. It’s useful for sharing data between containers, especially in complex applications where multiple services need access to the same data.

Example:

docker run --volumes-from other-container nginx

9. :nocopy

This modifier can be used with the --mount option for volumes. It tells Docker not to copy data from a container path to a new volume, which can be useful when the default behavior of copying data is not desired.

Example:

docker run --mount type=volume,source=myvolume,target=/app,data:nocopy myapp

Share this post on:

Previous Post
Docker Networking Essential Guide for All Skill Levels
Next Post
How to Transfer docker Images Without a Repository