📖 Scenario: You are working on a Docker project where you need to decide how to store data generated by your containers. You want to understand the difference between Docker volumes and bind mounts to choose the best option for your use case.Imagine you have a simple web application container that needs to save logs and configuration files. You want to set up the data storage correctly to keep your data safe and easy to manage.
🎯 Goal: Learn how to create a Docker volume and a bind mount, understand their differences, and decide which one to use by running containers with each method.
📋 What You'll Learn
Create a Docker volume named app_data
Create a bind mount to a local directory /tmp/app_config
Run a container using the volume to store logs
Run a container using the bind mount to access configuration files
Print the contents of the stored files to verify data storage
💡 Why This Matters
🌍 Real World
Developers and system administrators often need to manage persistent data in Docker containers. Choosing between volumes and bind mounts affects data safety, portability, and ease of backup.
💼 Career
Understanding volumes and bind mounts is essential for Docker container management, a key skill in DevOps roles and cloud infrastructure management.
Progress0 / 4 steps
1
Create a Docker volume named app_data
Use the Docker CLI command docker volume create app_data to create a volume called app_data.
Docker
Hint
Use the docker volume create command followed by the volume name.
2
Create a bind mount directory /tmp/app_config
Create a local directory /tmp/app_config on your host machine using the command mkdir -p /tmp/app_config.
Docker
Hint
Use the mkdir -p command to create the directory including parent directories if needed.
3
Run containers using the volume and bind mount
Run two Docker containers: one named log_container that uses the volume app_data mounted at /app/logs, and another named config_container that uses the bind mount /tmp/app_config mounted at /app/config. Use the busybox image and run the command sh -c "echo 'test' > /app/logs/log.txt" in log_container and sh -c "echo 'config' > /app/config/config.txt" in config_container.
Docker
Hint
Use docker run --name [container_name] -v [volume_or_path]:[container_path] to mount volumes or bind mounts.
4
Display the contents of the stored files from both containers
Use docker cp <container_name>:<file_path> - to print the contents of /app/logs/log.txt from log_container and /app/config/config.txt from config_container. Use echo to label each output.
Docker
Hint
Use docker cp [container_name]:[file_path] - to print file contents from stopped containers.
Practice
(1/5)
1. Which Docker storage option should you use if you want Docker to manage persistent data independently of the host system?
easy
A. Use a temporary container filesystem
B. Use a bind mount
C. Use a network file share
D. Use a Docker volume
Solution
Step 1: Understand Docker volume purpose
Docker volumes are designed to store persistent data managed by Docker itself, independent of the host's file system structure.
Step 2: Compare with bind mounts
Bind mounts link host files or folders directly, so Docker does not manage the data lifecycle.
Using temporary container filesystem for persistent data
Assuming network file share is a Docker volume
2. Which of the following is the correct syntax to create a Docker container with a bind mount from host folder /home/user/data to container folder /app/data?
easy
A. docker run -v /home/user/data:/app/data myimage
B. docker run --mount type=bind,source=/app/data,target=/home/user/data myimage
C. docker run --bind /home/user/data:/app/data myimage
D. docker run -v /app/data:/home/user/data myimage
Solution
Step 1: Recall bind mount syntax with -v
The correct syntax for bind mounts with -v is -v host_path:container_path. So -v /home/user/data:/app/data is correct.
Step 2: Check other options for errors
docker run --mount type=bind,source=/app/data,target=/home/user/data myimage reverses source and target paths and uses --mount incorrectly. docker run --bind /home/user/data:/app/data myimage uses invalid flag --bind. docker run -v /app/data:/home/user/data myimage reverses host and container paths.
Final Answer:
docker run -v /home/user/data:/app/data myimage -> Option A
Quick Check:
Bind mount syntax = -v host:container [OK]
Hint: Bind mount syntax: -v host_path:container_path [OK]
Common Mistakes:
Reversing host and container paths
Using invalid flags like --bind
Confusing --mount syntax parameters
3. Given the command docker run -v mydata:/data busybox ls /data, what will be the output if mydata volume is empty?
medium
A. Lists files from host /data directory
B. Shows an empty list (no files)
C. Throws an error: volume not found
D. Lists files from busybox image /data directory
Solution
Step 1: Understand volume behavior
The volume mydata is mounted at /data inside the container. If the volume is empty, the container sees an empty directory at /data.
Step 2: Check command output
The ls /data command lists files in the mounted volume. Since it is empty, the output is empty (no files listed).
Hint: Empty volume means empty folder inside container [OK]
Common Mistakes:
Assuming volume auto-fills with image files
Expecting error if volume is empty
Confusing volume with bind mount behavior
4. You want to edit source code files live on your host and see changes immediately inside the container. You tried docker run -v /app/src busybox but it fails. What is the error and how to fix it?
medium
A. Error: permission denied; fix by running as root
B. Error: volume not found; fix by creating volume first
C. Error: missing container path; fix by adding host:container in -v
D. Error: invalid image; fix by using correct image name
Solution
Step 1: Identify syntax error in -v flag
The command -v /app/src is incomplete. Docker expects -v host_path:container_path or volume_name:container_path.
Step 2: Fix by specifying both paths
To bind mount host folder /app/src into container, use -v /app/src:/app/src or similar.
Final Answer:
Error: missing container path; fix by adding host:container in -v -> Option C
Quick Check:
-v requires host:container paths [OK]
Hint: -v must have host and container paths separated by colon [OK]
Common Mistakes:
Using only one path in -v flag
Confusing volume creation with bind mount syntax
Ignoring error messages about syntax
5. You want to share a configuration file /etc/config.yaml from your host with multiple containers, and you want Docker to manage the data lifecycle. Which approach is best?
hard
A. Create a Docker volume, copy the file into it, then mount the volume in containers
B. Use a bind mount directly from /etc/config.yaml to containers
C. Copy the file into each container image manually
D. Use a network file share mounted inside containers
Solution
Step 1: Understand requirement for Docker-managed data
You want Docker to manage the data lifecycle, so bind mounts (host-managed) are not ideal.
Step 2: Use volume and copy file
Create a Docker volume, copy /etc/config.yaml into it once, then mount this volume into all containers. This centralizes management.
Final Answer:
Create a Docker volume, copy the file into it, then mount the volume in containers -> Option A
Quick Check:
Docker manages data = use volume + copy file [OK]
Hint: For Docker-managed shared data, use volume with copied files [OK]
Common Mistakes:
Using bind mounts when Docker management is needed
Copying files into images instead of using volumes
Assuming network shares are Docker volumes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.