0
0
Dockerdevops~5 mins

Sharing volumes between containers in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes, you want two or more containers to access the same files. Sharing volumes lets containers read and write the same data, like sharing a folder between friends.
When you want a web server container and a logging container to access the same log files.
When multiple app containers need to read and write shared configuration files.
When you want to persist data created by one container and use it in another container.
When you want to share a database data folder between backup and main database containers.
When you want to share uploaded files between a frontend and backend container.
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  app1:
    image: busybox
    command: sh -c "echo 'Hello from app1' > /shared/message.txt && sleep 3600"
    volumes:
      - shared-data:/shared
  app2:
    image: busybox
    command: sh -c "cat /shared/message.txt && sleep 3600"
    volumes:
      - shared-data:/shared
volumes:
  shared-data:

This file defines two containers, app1 and app2, sharing the same volume called shared-data.

app1 writes a message to a file inside the shared volume.

app2 reads the message from the same file.

The volumes section at the bottom creates the shared volume.

Commands
This command starts both containers in the background, creating the shared volume automatically.
Terminal
docker-compose up -d
Expected OutputExpected
Creating network "default" with the default driver Creating volume "shared-data" with default driver Creating app1 ... done Creating app2 ... done
-d - Run containers in detached (background) mode
This command shows the logs of app2, which should display the message written by app1 to the shared volume.
Terminal
docker-compose logs app2
Expected OutputExpected
app2 | Hello from app1
This command lists all Docker volumes, showing that the shared volume exists.
Terminal
docker volume ls
Expected OutputExpected
DRIVER VOLUME NAME local shared-data
This command stops and removes the containers and deletes the shared volume to clean up.
Terminal
docker-compose down -v
Expected OutputExpected
Stopping app2 ... done Stopping app1 ... done Removing app2 ... done Removing app1 ... done Removing network default Removing volume shared-data
-v - Remove named volumes declared in the compose file
Key Concept

If you remember nothing else from this pattern, remember: Docker volumes let multiple containers share the same files safely and persistently.

Common Mistakes
Mounting the same host folder with different paths inside containers without using a named volume.
This can cause permission issues or inconsistent data because the host folder may not be properly shared or isolated.
Use a named Docker volume declared in the compose file to share data between containers.
Not using the same volume name in all containers that need to share data.
Containers will not share data if they mount different volumes or paths, so the data won't be visible across containers.
Use the exact same volume name in the volumes section for all containers that need to share files.
Not cleaning up volumes after testing, leaving unused volumes consuming disk space.
Unused volumes can accumulate and waste disk space over time.
Use 'docker-compose down -v' to remove volumes when they are no longer needed.
Summary
Define a named volume in docker-compose.yml to share files between containers.
Mount the same named volume path inside each container that needs access.
Use 'docker-compose up -d' to start containers and 'docker-compose logs' to verify shared data.
Clean up with 'docker-compose down -v' to remove containers and volumes.