📖 Scenario: You are setting up a Docker container to run a simple web server. You want to make sure that the data created by the server is saved even if the container stops or is removed. This is important because you want to keep your website files safe and not lose them.
🎯 Goal: Learn how to create and use Docker volumes to store persistent data outside the container. You will create a volume, attach it to a container, and verify that data is saved persistently.
📋 What You'll Learn
Create a Docker volume named webdata
Run a Docker container named mywebserver using the nginx image
Mount the webdata volume to the container's /usr/share/nginx/html directory
Verify that the volume is used by checking the container's volume configuration
💡 Why This Matters
🌍 Real World
Docker volumes are used to save data generated by containers so it is not lost when containers stop or are deleted. This is important for databases, web servers, and any service that needs to keep files or data.
💼 Career
Understanding Docker volumes is essential for DevOps roles because managing persistent data in containers is a common task in deploying and maintaining applications.
Progress0 / 4 steps
1
Create a Docker volume
Create a Docker volume called webdata using the docker volume create command.
Docker
Hint
Use the command docker volume create webdata to create the volume.
2
Run a container with the volume attached
Run a Docker container named mywebserver using the nginx image. Attach the webdata volume to the container's /usr/share/nginx/html directory using the --mount option.
Docker
Hint
Use docker run -d --name mywebserver --mount source=webdata,target=/usr/share/nginx/html nginx to start the container.
3
Check the volume usage in the container
Use the docker inspect command on the container mywebserver to check that the volume webdata is mounted at /usr/share/nginx/html.
Docker
Hint
Run docker inspect mywebserver and look for the Mounts section showing webdata mounted at /usr/share/nginx/html.
4
Show the volume mount details from inspect output
Run docker inspect mywebserver and print only the Mounts section to confirm the volume webdata is attached to /usr/share/nginx/html.
Docker
Hint
Use docker inspect mywebserver --format '{{json .Mounts}}' to print the mounts in JSON format.
Practice
(1/5)
1. What is the main purpose of using volumes in Docker containers?
easy
A. To limit container CPU usage
B. To store data persistently outside the container lifecycle
C. To speed up container startup time
D. To create network connections between containers
Solution
Step 1: Understand container storage
Containers are temporary and lose data when removed unless data is stored outside.
Step 2: Role of volumes
Volumes provide a way to save data persistently on the host, independent of container lifecycle.
Final Answer:
To store data persistently outside the container lifecycle -> Option B
Quick Check:
Persistent data = volumes [OK]
Hint: Volumes keep data safe even if container is deleted [OK]
Common Mistakes:
Confusing volumes with container CPU limits
Thinking volumes speed up container start
Mixing volumes with networking features
2. Which of the following is the correct Docker CLI syntax to create a named volume called mydata?
easy
A. docker create volume mydata
B. docker volume new mydata
C. docker volume create mydata
D. docker volume make mydata
Solution
Step 1: Recall Docker volume creation command
The correct command to create a volume is docker volume create <name>.
Step 2: Match syntax
docker volume create mydata matches the correct syntax exactly.
Final Answer:
docker volume create mydata -> Option C
Quick Check:
Volume create command = docker volume create [OK]
Hint: Use 'docker volume create' to make volumes [OK]
Common Mistakes:
Using 'create volume' instead of 'volume create'
Using 'new' or 'make' which are invalid commands
Mixing order of words in the command
3. Given the Docker run command: docker run -d -v myvol:/app/data nginx What happens to the data inside /app/data when the container is removed?
medium
A. Data remains in the named volume myvol
B. Data is deleted with the container
C. Data is copied to the host's home directory
D. Data is lost unless backed up manually
Solution
Step 1: Understand named volume behavior
Named volumes like myvol store data persistently on the host.
Step 2: Effect of container removal
Removing the container does not delete the named volume or its data.
Final Answer:
Data remains in the named volume myvol -> Option A
Quick Check:
Named volume data persists after container removal [OK]
Hint: Named volumes keep data safe after container removal [OK]
Common Mistakes:
Assuming data deletes with container
Confusing named volumes with anonymous volumes
Thinking data copies to host home directory automatically
4. You run this command: docker run -v /data nginx But it fails with an error. What is the problem?
medium
A. Missing container path in volume syntax
B. Volume name /data is invalid
C. Nginx image does not support volumes
D. Docker daemon is not running
Solution
Step 1: Check volume syntax
The correct volume syntax requires host_path:container_path or volume_name:container_path.
Step 2: Identify missing part
Here, only one path /data is given, missing container path.
Final Answer:
Missing container path in volume syntax -> Option A
Quick Check:
Volume syntax needs host:container paths [OK]
Hint: Volume syntax needs two paths separated by colon [OK]
Common Mistakes:
Using only one path in -v option
Blaming image for volume errors
Ignoring Docker daemon status
5. You want to share persistent data between two containers using a named volume sharedvol. Which Docker commands correctly achieve this?
hard
A. docker volume create sharedvol docker run -v sharedvol container1 docker run -v sharedvol container2
B. docker run -v sharedvol:/data container1 docker run -v sharedvol:/data container2
C. docker volume create sharedvol docker run -v /data sharedvol container1 docker run -v /data sharedvol container2
D. docker volume create sharedvol docker run -v sharedvol:/data container1 docker run -v sharedvol:/data container2
Solution
Step 1: Create the named volume
Use docker volume create sharedvol to make the volume first.
Step 2: Mount volume in both containers
Use -v sharedvol:/data to mount the volume at /data inside each container.
Final Answer:
docker volume create sharedvol docker run -v sharedvol:/data container1 docker run -v sharedvol:/data container2 -> Option D
Quick Check:
Named volume + mount path in both containers = sharing data [OK]
Hint: Create volume first, then mount with -v volume:path [OK]
Common Mistakes:
Skipping volume creation step
Reversing volume and path in -v option
Omitting container path in volume mount
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.