WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Docker · Basic · question 12 of 100

How do you share data between containers using Docker volumes?

📕 Buy this interview preparation book: 100 Docker questions & answers — PDF + EPUB for $5

Docker volumes are a mechanism for sharing data between containers or between a container and the host system. Volumes provide a way to store and persist data separately from the container, which makes it easier to manage and share data across multiple containers. Here are the steps to share data between containers using Docker volumes:

Create a new Docker volume using the docker volume create command. The docker volume create command creates a new volume that can be used by one or more containers.

    docker volume create my-data-volume

Start a new Docker container with the –mount or -v option to mount the volume. The –mount or -v option specifies the source and target of the mount point, as well as any additional options.

For example, to start a new Docker container with a volume named my-data-volume mounted at the /data directory inside the container, you would run the following command:

    docker run --name my-container --mount source=my-data-volume,target=/data IMAGE

This command starts a new Docker container based on the specified IMAGE, creates a new mount point at the /data directory inside the container, and mounts the my-data-volume volume to the mount point.

Write data to the mount point inside the container. Any data that is written to the mount point inside the container is stored in the volume and can be accessed by other containers that mount the same volume.

    docker exec -it my-container sh
    echo "Hello, world!" > /data/test.txt
    exit

Start another Docker container and mount the same volume using the same –mount or -v option. Any data that was written to the mount point in the first container can now be accessed from the mount point in the second container.

    docker run --name my-other-container --mount source=my-data-volume,target=/data IMAGE
    docker exec -it my-other-container sh
    cat /data/test.txt

This command starts another Docker container based on the same IMAGE, mounts the my-data-volume volume to the /data directory inside the container, and reads the contents of the test.txt file that was created in the first container.

Here are some additional options that you can use with the –mount or -v option:

:ro: This option specifies that the volume should be mounted in read-only mode, which prevents any changes from being made to the volume inside the container.

:z or :Z: These options specify that the volume should be mounted with the z or Z SELinux context, which is necessary for some systems that use SELinux security.

For example, to mount the my-data-volume volume in read-only mode, you would run the following command:

    docker run --name my-container --mount source=my-data-volume,target=/data:ro IMAGE

This command starts a new Docker container based on the specified IMAGE, creates a new mount point at the /data directory inside the container, and mounts the my-data-volume volume to the mount point in read-only mode.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Docker interview — then scores it.
📞 Practice Docker — free 15 min
📕 Buy this interview preparation book: 100 Docker questions & answers — PDF + EPUB for $5

All 100 Docker questions · All topics