Bird
Raised Fist0
Dockerdevops~30 mins

Creating and managing volumes in Docker - Try It Yourself

Choose your learning style10 modes available
Creating and managing volumes
📖 Scenario: You are working on a Docker project where you need to store data persistently outside of containers. This means even if a container is deleted, the data remains safe. Docker volumes help with this by providing a special storage area on your computer.In this project, you will create a Docker volume, use it in a container, and then check the volume's data to understand how volumes work.
🎯 Goal: Learn how to create a Docker volume, attach it to a container, write data inside the container to the volume, and verify the data persists after the container is removed.
📋 What You'll Learn
Create a Docker volume named mydata
Run a container using the busybox image with the volume mydata mounted at /data
Inside the container, create a file named hello.txt with the content Hello, Docker Volumes!
Verify the file exists in the volume by running another container and reading the file
Remove the first container and confirm the volume still holds the data
💡 Why This Matters
🌍 Real World
Docker volumes are used in real projects to keep databases, logs, and user files safe even when containers are updated or removed.
💼 Career
Understanding Docker volumes is essential for DevOps roles to manage persistent data in containerized applications reliably.
Progress0 / 4 steps
1
Create a Docker volume named mydata
Type the command to create a Docker volume called mydata.
Docker
Hint

Use the docker volume create command followed by the volume name.

2
Run a container with the volume mydata mounted at /data
Run a Docker container using the busybox image with the volume mydata mounted to the container path /data. Name the container voltest and keep it running interactively.
Docker
Hint

Use docker run with -v mydata:/data to mount the volume and --name voltest to name the container.

3
Create a file hello.txt inside the container with specific content
Inside the running voltest container, create a file named /data/hello.txt containing the text Hello, Docker Volumes!. Use the echo command.
Docker
Hint

Use echo "Hello, Docker Volumes!" > /data/hello.txt to write the text into the file inside the mounted volume.

4
Verify the file exists in the volume by reading it from a new container
Exit the voltest container, then run a new busybox container with the volume mydata mounted at /data. Use the cat /data/hello.txt command to display the file content.
Docker
Hint

Use exit to leave the first container, then run docker run --rm -v mydata:/data busybox cat /data/hello.txt to read the file.

Practice

(1/5)
1. What is the main purpose of Docker volumes?
easy
A. To manage network connections between containers
B. To speed up container startup time
C. To create new containers automatically
D. To store data outside of containers so it is not lost when containers stop

Solution

  1. Step 1: Understand what volumes do

    Volumes store data outside the container's writable layer, so data persists even if the container is removed.
  2. Step 2: Compare options

    Only To store data outside of containers so it is not lost when containers stop describes this purpose correctly. Other options describe unrelated features.
  3. Final Answer:

    To store data outside of containers so it is not lost when containers stop -> Option D
  4. Quick Check:

    Volumes keep data safe outside containers = A [OK]
Hint: Volumes keep data safe outside containers [OK]
Common Mistakes:
  • Confusing volumes with container networking
  • Thinking volumes speed up container start
  • Assuming volumes create containers
2. Which command correctly creates a Docker volume named mydata?
easy
A. docker volume new mydata
B. docker volume create mydata
C. docker create volume mydata
D. docker volume add mydata

Solution

  1. Step 1: Recall Docker volume creation syntax

    The correct command to create a volume is docker volume create [name].
  2. Step 2: Match options to syntax

    Only docker volume create mydata matches the correct syntax exactly.
  3. Final Answer:

    docker volume create mydata -> Option B
  4. Quick Check:

    Use 'docker volume create' = C [OK]
Hint: Use 'docker volume create' to make volumes [OK]
Common Mistakes:
  • Using wrong command order like 'docker create volume'
  • Using non-existent commands like 'docker volume new'
  • Confusing 'add' with 'create'
3. What is the output of the command docker volume ls after creating two volumes named vol1 and vol2?
medium
A. Lists volumes including vol1 and vol2
B. Lists all containers running
C. Shows detailed info about vol1 only
D. Deletes volumes vol1 and vol2

Solution

  1. Step 1: Understand what 'docker volume ls' does

    This command lists all Docker volumes available on the system.
  2. Step 2: Interpret the scenario

    After creating volumes vol1 and vol2, the command will list both volumes among others.
  3. Final Answer:

    Lists volumes including vol1 and vol2 -> Option A
  4. Quick Check:

    'docker volume ls' lists volumes = A [OK]
Hint: 'docker volume ls' shows all volumes [OK]
Common Mistakes:
  • Confusing volume list with container list
  • Expecting detailed info instead of list
  • Thinking it deletes volumes
4. You run docker volume rm myvolume but get an error saying the volume is in use. What should you do to fix this?
medium
A. Restart Docker daemon and try removing again
B. Rename the volume before removing it
C. Stop and remove containers using the volume, then remove the volume
D. Use docker volume create again to overwrite it

Solution

  1. Step 1: Understand volume usage error

    Docker prevents removing volumes that are still attached to running or stopped containers.
  2. Step 2: Fix by stopping/removing containers

    You must stop and remove containers using the volume before deleting the volume itself.
  3. Final Answer:

    Stop and remove containers using the volume, then remove the volume -> Option C
  4. Quick Check:

    Remove containers before volumes in use = D [OK]
Hint: Remove containers using volume before deleting volume [OK]
Common Mistakes:
  • Trying to rename volume to delete it
  • Recreating volume instead of removing
  • Restarting Docker daemon without stopping containers
5. You want to create a Docker container that uses a volume named appdata to persist data at /data inside the container. Which command correctly does this?
hard
A. docker run --mount source=appdata,target=/data myimage
B. docker run -v /appdata:/data myimage
C. docker run -v /data appdata myimage
D. docker run --volume /data:appdata myimage

Solution

  1. Step 1: Understand volume mounting syntax

    The recommended syntax for mounting a named volume is docker run --mount source=volume_name,target=container_path image. The shorthand -v volume_name:container_path also works, but paths starting with / indicate bind mounts from the host.
  2. Step 2: Compare options

    docker run -v /appdata:/data myimage performs a bind mount from the host directory /appdata, not the named volume appdata. docker run --mount source=appdata,target=/data myimage correctly mounts the named volume appdata. The other options (-v /data appdata and --volume /data:appdata) have invalid syntax or incorrect source/target order.
  3. Final Answer:

    docker run --mount source=appdata,target=/data myimage -> Option A
  4. Quick Check:

    Use --mount source=appdata,target=/data = B [OK]
Hint: Use --mount source=vol,target=path for clarity [OK]
Common Mistakes:
  • Mixing volume and container paths order
  • Using incorrect flags like --volume with wrong syntax
  • Confusing shorthand and long form options
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard