docker:share_data_between_docker_containers
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
docker:share_data_between_docker_containers [2017/02/22 15:47] – peter | docker:share_data_between_docker_containers [2020/05/13 09:02] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Docker - Share data between docker containers ====== | ||
- | |||
- | Docker Volumes can be created and attached in the same command that creates a container, or they can be created independent of any containers and then attached later. There are a number of different ways to share data between containers. | ||
- | |||
- | ===== Creating an Independent Volume ===== | ||
- | |||
- | Docker' | ||
- | |||
- | <code bash> | ||
- | docker volume create --name DataVolume1 | ||
- | </ | ||
- | |||
- | |||
- | The name is displayed, indicating that the command was successful. | ||
- | |||
- | < | ||
- | Output | ||
- | DataVolume1 | ||
- | </ | ||
- | |||
- | To make use of the volume, we'll create a new container from the Ubuntu image, using the **--rm** flag to automatically delete it when we exit. We'll use **-v** to mount the new volume. | ||
- | |||
- | <code bash> | ||
- | docker run -ti --rm -v DataVolume1:/ | ||
- | </ | ||
- | |||
- | Write some data to the volume: | ||
- | |||
- | <code bash> | ||
- | echo " | ||
- | </ | ||
- | |||
- | Because we used the **--rm** flag, the container will be automatically deleted when we exit. The volume, however, will still be accessible. | ||
- | |||
- | <code bash> | ||
- | exit | ||
- | </ | ||
- | |||
- | Verify the volume is present on the system with docker volume inspect: | ||
- | |||
- | <code bash> | ||
- | docker volume inspect DataVolume1 | ||
- | </ | ||
- | |||
- | Result | ||
- | |||
- | < | ||
- | Output | ||
- | [ | ||
- | { | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | } | ||
- | ] | ||
- | </ | ||
- | |||
- | <WRAP warning> | ||
- | **WARNING**: | ||
- | </ | ||
- | |||
- | |||
- | Start a new container and attach DataVolume1: | ||
- | |||
- | <code bash> | ||
- | docker run --rm -ti -v DataVolume1:/ | ||
- | cat / | ||
- | </ | ||
- | |||
- | Displays: | ||
- | |||
- | < | ||
- | Output | ||
- | Example1 | ||
- | </ | ||
- | |||
- | |||
- | Exit the container. | ||
- | |||
- | <code bash> | ||
- | exit | ||
- | </ | ||
- | |||
- | In this example, we created a volume, attached it to a container, and verified its persistence. | ||
- | |||
- | |||
- | |||
- | ===== Creating a Volume that Persists when the Container is Removed ===== | ||
- | |||
- | Create a volume at the same time as the container, delete the container, then attach the volume to a new container. | ||
- | |||
- | <code bash> | ||
- | docker run -ti --name=Container2 -v DataVolume2:/ | ||
- | </ | ||
- | |||
- | Write some data to the volume: | ||
- | |||
- | <code bash> | ||
- | echo " | ||
- | cat / | ||
- | </ | ||
- | |||
- | Output: | ||
- | |||
- | < | ||
- | Example2 | ||
- | </ | ||
- | |||
- | Exit the container: | ||
- | |||
- | <code bash> | ||
- | exit | ||
- | </ | ||
- | |||
- | |||
- | Restart it - the volume will mount automatically. | ||
- | |||
- | <code bash> | ||
- | docker start -ai Container2 | ||
- | </ | ||
- | |||
- | |||
- | Verify that the volume has indeed mounted and our data is still in place: | ||
- | |||
- | <code bash> | ||
- | cat / | ||
- | </ | ||
- | |||
- | Output | ||
- | |||
- | < | ||
- | Example2 | ||
- | </ | ||
- | |||
- | |||
- | Exit and clean up. | ||
- | |||
- | <code bash> | ||
- | exit | ||
- | </ | ||
- | |||
- | |||
- | Docker won't allow a volume to be removed if it's referenced by a container. | ||
- | |||
- | <code bash> | ||
- | docker volume rm DataVolume2 | ||
- | </ | ||
- | |||
- | The message tells us that the volume is still in use and supplies the long version of the container ID: | ||
- | |||
- | Output: | ||
- | |||
- | < | ||
- | Error response from daemon: Unable to remove volume, | ||
- | volume still in use: remove DataVolume2: | ||
- | [719f98391ecf1d6f0f053ffea1bbd84cd2dc9cf6d31d5a4f348c60d98392804c] | ||
- | </ | ||
- | |||
- | |||
- | Use the ID to remove the container: | ||
- | |||
- | <code bash> | ||
- | docker rm 719f98391ecf1d6f0f053ffea1bbd84cd2dc9cf6d31d5a4f348c60d98392804c | ||
- | </ | ||
- | |||
- | Output: | ||
- | |||
- | < | ||
- | 719f98391ecf1d6f0f053ffea1bbd84cd2dc9cf6d31d5a4f348c60d98392804c | ||
- | </ | ||
- | |||
- | Removing the container won't affect the volume. | ||
- | |||
- | <code bash> | ||
- | docker volume ls | ||
- | </ | ||
- | |||
- | Output: | ||
- | |||
- | < | ||
- | DRIVER | ||
- | local | ||
- | </ | ||
- | |||
- | |||
- | Remove the volume: | ||
- | |||
- | <code bash> | ||
- | docker volume rm DataVolume2 | ||
- | </ | ||
- | |||
- | In this example, we created an empty data volume at the same time that we created a container. | ||
- | |||
- | |||
- | |||
- | ===== Creating a Volume from an Existing Directory with Data ===== | ||
- | |||
- | If a volume is created at the same time a container is create and the path to a directory is provided that contains data in the base image, that data will be copied into the volume. | ||
- | |||
- | Create a container and add the data volume at /var, a directory which contains data in the base image: | ||
- | |||
- | <code bash> | ||
- | docker run -ti --rm -v DataVolume3:/ | ||
- | </ | ||
- | |||
- | All the content from the base image' | ||
- | |||
- | <code bash> | ||
- | docker run --rm -v DataVolume3:/ | ||
- | </ | ||
- | |||
- | DataVolume3 has a copy of the contents of the base image' | ||
- | |||
- | Output: | ||
- | |||
- | < | ||
- | backups | ||
- | cache | ||
- | lib | ||
- | local | ||
- | lock | ||
- | log | ||
- | |||
- | opt | ||
- | run | ||
- | spool | ||
- | tmp | ||
- | </ | ||
- | |||
- | It's unlikely that we would want to mount /var/ in this way, but this can be helpful if we've crafted our own image and want an easy way to preserve data. | ||
- | |||
- | |||
- | ===== Sharing Data between Multiple Docker Containers ===== | ||
- | |||
- | Attaching multiple containers to the same data volume is relatively straightforward, | ||
- | |||
- | ==== Create Container4 and DataVolume4 ==== | ||
- | |||
- | Use **docker run** to create a new container named Container4 with a data volume attached: | ||
- | |||
- | <code bash> | ||
- | docker run -ti --name=Container4 -v DataVolume4:/ | ||
- | </ | ||
- | |||
- | |||
- | Create a file and add some text: | ||
- | |||
- | <code bash> | ||
- | echo "This file is shared between containers" | ||
- | </ | ||
- | |||
- | |||
- | Exit the container. | ||
- | |||
- | <code bash> | ||
- | exit | ||
- | </ | ||
- | |||
- | This returns us to the host command prompt, where we'll make a new container that mounts the data volume from Container4. | ||
- | |||
- | ==== Create Container5 and Mount Volumes from Container4 ==== | ||
- | |||
- | <code bash> | ||
- | docker run -ti --name=Container5 --volumes-from Container4 ubuntu | ||
- | cat / | ||
- | </ | ||
- | |||
- | Output: | ||
- | |||
- | This file is shared between containers. | ||
- | |||
- | Append some text from the second container: | ||
- | |||
- | <code bash> | ||
- | echo "Both containers can write to DataVolume4" | ||
- | </ | ||
- | |||
- | |||
- | Exit the container: | ||
- | |||
- | <code bash> | ||
- | exit | ||
- | </ | ||
- | |||
- | |||
- | Check that the data is still present to Container4. | ||
- | |||
- | |||
- | ==== View Changes Made in Container5 ==== | ||
- | |||
- | Check for the changes that were written to the data volume by Container5 by restarting Container4: | ||
- | |||
- | <code bash> | ||
- | docker start -ai Container4 | ||
- | cat / | ||
- | cat / | ||
- | </ | ||
- | |||
- | Output: | ||
- | |||
- | This file is shared between containers. | ||
- | Both containers can write to DataVolume4 | ||
- | |||
- | |||
- | Now that we've verified that both containers were able to read and write from the data volume, we'll exit the container: | ||
- | |||
- | <code bash> | ||
- | exit | ||
- | </ | ||
- | |||
- | |||
- | Remember that Docker doesn' | ||
- | |||
- | |||
- | ==== Start Container 6 and Mount the Volume Read-Only ==== | ||
- | |||
- | Once a volume has been mounted in a container, rather than unmounting it like we would with a typical Linux file system, we instead create a new container mounted the way we want it and, if needed, remove the previous container. | ||
- | |||
- | <code bash> | ||
- | docker run -ti --name=Container6 --volumes-from Container4: | ||
- | </ | ||
- | |||
- | |||
- | Check the read-only status by trying to remove our example file: | ||
- | |||
- | <code bash> | ||
- | rm / | ||
- | </ | ||
- | |||
- | Output: | ||
- | |||
- | < | ||
- | rm: cannot remove '/ | ||
- | </ | ||
- | |||
- | |||
- | Exit the container and clean up our test containers and volumes: | ||
- | |||
- | <code bash> | ||
- | exit | ||
- | </ | ||
- | |||
- | |||
- | Clean up our containers and volume: | ||
- | |||
- | <code bash> | ||
- | docker rm Container4 Container5 Container6 | ||
- | docker volume rm DataVolume4 | ||
- | </ | ||
- | |||
- | |||
- | In this example, we've shown how to share data between two containers using a data volume and how to mount a data volume as read-only. | ||
docker/share_data_between_docker_containers.1487778427.txt.gz · Last modified: 2020/07/15 09:30 (external edit)