Bird
Raised Fist0
Dockerdevops~5 mins

Named volumes for data sharing in Docker - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes, you want to save data from a container or share data between containers. Named volumes let you store data outside the container so it stays safe even if the container stops or is removed.
When you want to keep a database's data safe even if the database container is deleted.
When you want two or more containers to access the same files or folders.
When you want to back up or inspect data created by a container easily.
When you want to avoid losing important files after container updates or crashes.
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  app:
    image: nginx:1.23
    volumes:
      - shared-data:/usr/share/nginx/html
  backup:
    image: busybox
    command: sleep 3600
    volumes:
      - shared-data:/backup
volumes:
  shared-data:

This file defines two services: app and backup. Both use the same named volume shared-data. The app service uses it to serve files, and the backup service can access the same files for backup. The volumes section declares the named volume shared-data so Docker manages it outside containers.

Commands
This command starts the services defined in the docker-compose.yml file in the background. It creates the named volume and attaches it to both containers so they can share data.
Terminal
docker-compose up -d
Expected OutputExpected
Creating network "default" with the default driver Creating volume "shared-data" with default driver Creating app ... done Creating backup ... done
→
-d - Run containers in detached mode (in the background)
This command lists all Docker volumes on your system, showing that the named volume 'shared-data' was created.
Terminal
docker volume ls
Expected OutputExpected
DRIVER VOLUME NAME local shared-data
This command lists the files inside the shared volume folder in the 'app' container to verify the volume is mounted and accessible.
Terminal
docker-compose exec app ls /usr/share/nginx/html
Expected OutputExpected
index.html
This command stops and removes the containers and network but keeps the named volume so data is not lost.
Terminal
docker-compose down
Expected OutputExpected
Stopping app ... done Stopping backup ... done Removing app ... done Removing backup ... done Removing network default
Key Concept

Named volumes store data outside containers so it persists and can be shared safely between containers.

Common Mistakes
Using bind mounts instead of named volumes for sharing data.
Bind mounts depend on host paths and can cause permission or portability issues.
Use named volumes declared in Docker Compose or Docker CLI for safer, managed data sharing.
Removing containers with 'docker rm -f' without preserving volumes.
This deletes containers but not volumes, which can cause confusion about leftover data.
Use 'docker-compose down' to stop containers but keep volumes, or explicitly remove volumes if needed.
Summary
Named volumes keep data safe outside containers and allow sharing between containers.
Docker Compose can declare and use named volumes easily in the 'volumes' section.
Use 'docker-compose up -d' to start containers with volumes and 'docker-compose down' to stop without losing data.

Practice

(1/5)
1. What is the main purpose of using named volumes in Docker?
easy
A. To isolate network traffic between containers
B. To speed up container startup time
C. To share and persist data between containers safely
D. To limit CPU usage of containers

Solution

  1. Step 1: Understand what named volumes do

    Named volumes store data outside the container's writable layer, allowing data to persist beyond container life.
  2. Step 2: Identify the main benefit

    This persistence and sharing of data between containers is the key purpose of named volumes.
  3. Final Answer:

    To share and persist data between containers safely -> Option C
  4. Quick Check:

    Named volumes = data sharing and persistence [OK]
Hint: Named volumes keep data safe and share it [OK]
Common Mistakes:
  • Confusing volumes with network settings
  • Thinking volumes improve CPU or speed
  • Assuming volumes delete data with containers
2. Which of the following is the correct command to create a named volume called mydata?
easy
A. docker volume create mydata
B. docker create volume mydata
C. docker volume new mydata
D. docker volume make 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 the command with options

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

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

    Volume create command = docker volume create [OK]
Hint: Use 'docker volume create' to make named volumes [OK]
Common Mistakes:
  • Swapping order of words in the command
  • Using 'new' or 'make' instead of 'create'
  • Omitting the 'volume' keyword
3. Given the command:
docker run -d --name app1 -v myvol:/app/data busybox sleep 1000

What happens to the data inside /app/data if the container app1 is removed?
medium
A. Data inside /app/data is saved in the named volume 'myvol'
B. Data inside /app/data is lost unless backed up manually
C. Data inside /app/data is copied to the host's /var/lib/docker folder automatically
D. Data inside /app/data is deleted with the container

Solution

  1. Step 1: Understand volume mounting in the command

    The -v myvol:/app/data option mounts the named volume 'myvol' to the container path '/app/data'.
  2. Step 2: Know volume persistence behavior

    Named volumes keep data even if the container is removed, so data inside '/app/data' is saved in 'myvol'.
  3. Final Answer:

    Data inside /app/data is saved in the named volume 'myvol' -> Option A
  4. Quick Check:

    Named volume keeps data after container removal [OK]
Hint: Named volumes keep data safe after container removal [OK]
Common Mistakes:
  • Thinking data deletes with container
  • Confusing named volumes with anonymous volumes
  • Assuming data copies to host automatically
4. You run this command to share data between two containers:
docker run -d --name c1 -v sharedvol:/data busybox sleep 1000
docker run -d --name c2 -v sharedvol:/data busybox sleep 1000

But container c2 cannot see files created by c1 inside /data. What is the likely problem?
medium
A. The volume mount syntax is incorrect
B. Containers must be on the same network to share volumes
C. The volume 'sharedvol' was not created before running containers
D. Files created by c1 are not saved because the container is stopped

Solution

  1. Step 1: Check volume creation requirement

    Named volumes must exist or be created automatically. If 'sharedvol' was never created, Docker creates an empty volume.
  2. Step 2: Understand volume persistence and sharing

    If 'sharedvol' was not created or data was not saved properly, c2 won't see c1's files. Network does not affect volume sharing.
  3. Final Answer:

    Files created by c1 are not saved because the container is stopped -> Option D
  4. Quick Check:

    Stopped container does not save files to volume [OK]
Hint: Files must be saved and container running to share data [OK]
Common Mistakes:
  • Thinking network affects volume sharing
  • Assuming mount syntax is wrong
  • Believing stopped containers save files automatically
5. You want to share a folder /var/log/app on your host with multiple containers using a named volume. Which sequence of commands correctly creates and uses the named volume to achieve this?
hard
A. docker volume create app_logs docker run -d -v /var/log/app:/app/log busybox sleep 1000
B. docker volume create --opt type=none --opt device=/var/log/app --opt o=bind app_logs docker run -d -v app_logs:/app/log busybox sleep 1000
C. docker volume create app_logs docker run -d -v app_logs:/var/log/app busybox sleep 1000
D. docker run -d -v app_logs:/app/log busybox sleep 1000

Solution

  1. Step 1: Create a named volume that binds to host folder

    Use docker volume create with options type=none, device=/var/log/app, and o=bind to bind the host folder to the volume.
  2. Step 2: Run container mounting the named volume

    Mount the named volume app_logs to the container path /app/log with -v app_logs:/app/log.
  3. Final Answer:

    docker volume create --opt type=none --opt device=/var/log/app --opt o=bind app_logs docker run -d -v app_logs:/app/log busybox sleep 1000 -> Option B
  4. Quick Check:

    Bind host folder with volume create options [OK]
Hint: Use volume create with bind options for host folder sharing [OK]
Common Mistakes:
  • Mounting host folder directly without volume create
  • Mounting volume to wrong container path
  • Skipping volume creation step
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