Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to Share Data Between Docker Containers

Use a Docker volume or a shared host folder so several containers can read and write the same files.

VPS and Cloud Servers3 min read2 steps14 screenshots

There are two main ways to share data:

  1. A Docker volume. Docker manages the storage, and the data stays after containers are removed.
  2. A host directory (bind mount). You mount a folder of the host into the containers.

Method 1: Share data with an independent volume

Step 1: Create the volume

docker volume create shared_vol
Creating a Docker volume

Look at its details:

docker volume inspect shared_vol
Inspecting the volume

Step 2: Start the first container

docker run -it --name cont1 -v shared_vol:/shared_vol ubuntu
Starting the first container

Step 3: Create a file in the shared volume (inside cont1)

echo "Creating shared file" >> /shared_vol/file.txt
Creating a file in the shared volume

Leave the container with exit.

Step 4: Start the second container

docker run -it --name cont2 -v shared_vol:/shared_vol ubuntu
Starting the second container

Step 5: Check the data

cat /shared_vol/file.txt
The file is shared between the containers

The second container sees the file made by the first one.

Method 2: The data stays after containers are removed

Step 1: Remove the containers

docker rm cont1
Removing the first container
docker rm cont2
Removing the second container

Step 2: Start a new container with the same volume

docker run -it --name cont3 -v shared_vol:/shared_vol ubuntu
Starting a container with the existing volume

Run cat /shared_vol/file.txt. The file is still there, because the volume is separate from the containers.

Method 3: Share a host directory

Step 1: Create a folder on the host

mkdir shared_dir
Creating the shared directory

Step 2: Start a container that mounts it

On Linux or macOS use the full path:

docker run -it --name cont-1 -v "$(pwd)/shared_dir":/shared_dir ubuntu

On Windows use a path like this:

docker run -it --name cont-1 -v C:/Users/YourName/Documents/shared_dir:/shared_dir ubuntu
Starting a container with a host directory

Step 3: Create a file in the container

echo "file is created in the shared directory" >> /shared_dir/file.txt
Creating a file in the shared directory

Step 4: Check it on the host

Open the shared_dir folder on the host. The file file.txt is there.

The file in the host folder

Step 5: Start a second container with the same folder

docker run -it --name cont-2 -v "$(pwd)/shared_dir":/shared_dir ubuntu
Starting the second container

Both containers now read and write the same host folder.

Read-only sharing

Add :ro to give a container read-only access:

docker run -it -v shared_vol:/shared_vol:ro ubuntu

Docker Compose

services:
  app1:
    image: ubuntu
    command: sleep infinity
    volumes:
      - shared_vol:/shared_vol
  app2:
    image: ubuntu
    command: sleep infinity
    volumes:
      - shared_vol:/shared_vol

volumes:
  shared_vol:

Which method to use

  • Volume: best for app data, managed by Docker, works on every platform.
  • Host directory: best when you want to edit files from the host, for example source code.
Note: If two containers write the same file at the same time, you need to handle locking in your app.

Quick recap

  • docker volume create and -v volume:/path for volumes.
  • -v /host/path:/path for a host folder.