Bird
Raised Fist0
Dockerdevops~5 mins

Volumes for persistent data in Docker - Commands & Configuration

Choose your learning style10 modes available
Introduction
When you run a container, its data disappears if the container stops or is removed. Volumes let you save data outside the container so it stays safe and can be reused.
When you want to save a database's data so it is not lost when the container restarts.
When you need to share files between your host computer and a running container.
When you want to keep logs or configuration files persistent across container updates.
When multiple containers need access to the same data safely.
When you want to back up or migrate container data easily.
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  my-app:
    image: nginx:1.23
    volumes:
      - my-data:/usr/share/nginx/html
volumes:
  my-data:
    driver: local

This file defines a service named my-app using the nginx image.

The volumes section mounts a volume called my-data inside the container at /usr/share/nginx/html.

The volumes key at the bottom creates the named volume my-data using the local driver, which stores data on the host machine.

Commands
This command creates a named volume called 'my-data' on your host machine to store persistent data.
Terminal
docker volume create my-data
Expected OutputExpected
my-data
This runs an nginx container named 'my-nginx' in detached mode and mounts the 'my-data' volume inside the container at the nginx html folder.
Terminal
docker run -d --name my-nginx -v my-data:/usr/share/nginx/html nginx:1.23
Expected OutputExpected
a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6a7b8c9d0e1f2
→
-d - Run container in background (detached mode)
→
--name - Assign a name to the container
→
-v - Mount a volume inside the container
This lists all volumes on your host machine so you can verify 'my-data' exists.
Terminal
docker volume ls
Expected OutputExpected
DRIVER VOLUME NAME local my-data
This shows detailed information about the 'my-data' volume, including its mount point on the host.
Terminal
docker inspect my-data
Expected OutputExpected
[ { "CreatedAt": "2024-06-01T12:00:00Z", "Driver": "local", "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/my-data/_data", "Name": "my-data", "Options": {}, "Scope": "local" } ]
Key Concept

If you remember nothing else from this pattern, remember: volumes keep your container data safe and reusable outside the container lifecycle.

Common Mistakes
Not creating or specifying a volume before running the container.
Data will be stored inside the container and lost when it stops or is removed.
Always create and mount a named volume or bind mount to persist data.
Mounting a volume to the wrong container path.
The application inside the container won't find or save data where expected.
Check the container's documentation for correct paths to mount volumes.
Using anonymous volumes without names for important data.
Anonymous volumes are hard to manage and clean up, risking data loss.
Use named volumes for important persistent data.
Summary
Create a named volume to store data persistently on the host.
Run containers with the volume mounted to the correct path inside the container.
Verify volumes exist and inspect their details to manage data safely.

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

  1. Step 1: Understand container storage

    Containers are temporary and lose data when removed unless data is stored outside.
  2. Step 2: Role of volumes

    Volumes provide a way to save data persistently on the host, independent of container lifecycle.
  3. Final Answer:

    To store data persistently outside the container lifecycle -> Option B
  4. 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

  1. Step 1: Recall Docker volume creation command

    The correct command to create a volume is docker volume create <name>.
  2. Step 2: Match syntax

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

    docker volume create mydata -> Option C
  4. 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

  1. Step 1: Understand named volume behavior

    Named volumes like myvol store data persistently on the host.
  2. Step 2: Effect of container removal

    Removing the container does not delete the named volume or its data.
  3. Final Answer:

    Data remains in the named volume myvol -> Option A
  4. 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

  1. Step 1: Check volume syntax

    The correct volume syntax requires host_path:container_path or volume_name:container_path.
  2. Step 2: Identify missing part

    Here, only one path /data is given, missing container path.
  3. Final Answer:

    Missing container path in volume syntax -> Option A
  4. 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

  1. Step 1: Create the named volume

    Use docker volume create sharedvol to make the volume first.
  2. Step 2: Mount volume in both containers

    Use -v sharedvol:/data to mount the volume at /data inside each container.
  3. Final Answer:

    docker volume create sharedvol
    docker run -v sharedvol:/data container1
    docker run -v sharedvol:/data container2
    -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard