User Tools

Site Tools


docker:share_data_between_docker_containers

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
docker:share_data_between_docker_containers [2017/02/22 15:47] – [Sharing Data between Multiple Docker Containers] peterdocker: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's 1.9 onwards allows a volume to be created without it relating to any particular container: 
- 
-<code bash> 
-docker volume create --name DataVolume1 
-</code> 
- 
- 
-The name is displayed, indicating that the command was successful. 
- 
-<code> 
-Output 
-DataVolume1 
-</code> 
- 
-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.  -v requires the name of the volume, a colon, then the absolute path to where the volume should appear inside the container.  If the directories in the path don't exist as part of the image, they'll be created when the command runs.  If they do exist, the mounted volume will hide the existing content. 
- 
-<code bash> 
-docker run -ti --rm -v DataVolume1:/datavolume1 ubuntu 
-</code> 
- 
-Write some data to the volume: 
- 
-<code bash> 
-echo "Example1" > /datavolume1/Example1.txt 
-</code> 
- 
-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 
-</code> 
- 
-Verify the volume is present on the system with docker volume inspect: 
- 
-<code bash> 
-docker volume inspect DataVolume1 
-</code> 
- 
-Result 
- 
-<code> 
-Output 
-[ 
-    { 
-        "Name": "DataVolume1", 
-        "Driver": "local", 
-        "Mountpoint": "/var/lib/docker/volumes/datavolume1/_data", 
-        "Labels": null, 
-        "Scope": "local" 
-    } 
-] 
-</code> 
- 
-<WRAP warning> 
-**WARNING**:  The data on the host can also be accessed at the path listed as the Mountpoint.  Avoid altering this as it can cause data corruption if applications or containers are unaware of changes. 
-</WRAP> 
- 
- 
-Start a new container and attach DataVolume1: 
- 
-<code bash> 
-docker run --rm -ti -v DataVolume1:/datavolume1 ubuntu 
-cat /datavolume1/Example1.txt 
-</code> 
- 
-Displays: 
- 
-<code> 
-Output 
-Example1 
-</code> 
- 
- 
-Exit the container. 
- 
-<code bash> 
-exit 
-</code> 
- 
-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:/datavolume2 ubuntu 
-</code> 
- 
-Write some data to the volume: 
- 
-<code bash> 
-echo "Example2" > /datavolume2/Example2.txt 
-cat /datavolume2/Example2.txt 
-</code> 
- 
-Output: 
- 
-<code> 
-Example2 
-</code> 
- 
-Exit the container: 
- 
-<code bash> 
-exit 
-</code> 
- 
- 
-Restart it - the volume will mount automatically. 
- 
-<code bash> 
-docker start -ai Container2 
-</code> 
- 
- 
-Verify that the volume has indeed mounted and our data is still in place: 
- 
-<code bash> 
-cat /datavolume2/Example2.txt 
-</code> 
- 
-Output 
- 
-<code> 
-Example2 
-</code> 
- 
- 
-Exit and clean up. 
- 
-<code bash> 
-exit 
-</code> 
- 
- 
-Docker won't allow a volume to be removed if it's referenced by a container.  Try anyway: 
- 
-<code bash> 
-docker volume rm DataVolume2 
-</code> 
- 
-The message tells us that the volume is still in use and supplies the long version of the container ID: 
- 
-Output: 
- 
-<code> 
-Error response from daemon: Unable to remove volume,  
-volume still in use: remove DataVolume2: volume is in use -  
-[719f98391ecf1d6f0f053ffea1bbd84cd2dc9cf6d31d5a4f348c60d98392804c] 
-</code> 
- 
- 
-Use the ID to remove the container: 
- 
-<code bash> 
-docker rm 719f98391ecf1d6f0f053ffea1bbd84cd2dc9cf6d31d5a4f348c60d98392804c 
-</code> 
- 
-Output: 
- 
-<code> 
-719f98391ecf1d6f0f053ffea1bbd84cd2dc9cf6d31d5a4f348c60d98392804c 
-</code> 
- 
-Removing the container won't affect the volume.  Check it's still present on the system: 
- 
-<code bash> 
-docker volume ls 
-</code> 
- 
-Output: 
- 
-<code> 
-DRIVER              VOLUME NAME 
-local               DataVolume2 
-</code> 
- 
- 
-Remove the volume: 
- 
-<code bash> 
-docker volume rm DataVolume2 
-</code> 
- 
-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:/var ubuntu 
-</code> 
- 
-All the content from the base image's /var directory is copied into the volume, and we can mount that volume in a new container.  This time, rather than relying on the base image's default bash command, we'll issue our own ls command, which will show the contents of the volume without entering the shell: 
- 
-<code bash> 
-docker run --rm -v DataVolume3:/datavolume3 ubuntu ls DataVolume3 
-</code> 
- 
-DataVolume3 has a copy of the contents of the base image's /var directory: 
- 
-Output: 
- 
-<code> 
-backups 
-cache 
-lib 
-local 
-lock 
-log 
-mail 
-opt 
-run 
-spool 
-tmp 
-</code> 
- 
-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 ===== 
- 
-<WRAP warning> 
-**WARNING**:  Attaching multiple containers to the same data volume is relatively straightforward, but there's one critical caveat: at this time, Docker doesn't handle file locking.  If you need multiple containers writing to the volume, the applications running in those containers must be designed to write to shared data stores in order to prevent data corruption. 
-</WRAP> 
- 
-==== 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:/datavolume4 ubuntu  
-</code> 
- 
- 
-Create a file and add some text: 
- 
-<code bash> 
-echo "This file is shared between containers" > /datavolume4/Example4.txt 
-</code> 
- 
- 
-Exit the container. 
- 
-<code bash> 
-exit 
-</code> 
- 
-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 /datavolume4/Example4.txt 
-</code> 
- 
-Output: 
- 
-This file is shared between containers. 
- 
-Append some text from the second container: 
- 
-<code bash> 
-echo "Both containers can write to DataVolume4" >> /datavolume4/Example4.txt 
-</code> 
- 
- 
-Exit the container: 
- 
-<code bash> 
-exit 
-</code> 
- 
- 
-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 /datavolume4/Example4.txt 
-cat /DataVolume4/Example4.txt 
-</code> 
- 
-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 
-</code> 
- 
- 
-Remember that Docker doesn't handle any file locking, so applications must account for the file locking themselves.  It is possible to mount a Docker volume as read-only to ensure that data corruption won't happen by accident when a container requires read-only access by adding **:ro**.  
- 
- 
-==== 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.  To make the volume read-only, we append **:ro** to the end of the container name: 
- 
-<code bash> 
-docker run -ti --name=Container6 --volumes-from Container4:ro ubuntu 
-</code> 
- 
- 
-Check the read-only status by trying to remove our example file: 
- 
-<code bash> 
-rm /datavolume4/Example4.txt 
-</code> 
- 
-Output: 
- 
-<code> 
-rm: cannot remove '/datavolume4/Example4.txt': Read-only file system 
-</code> 
- 
- 
-Exit the container and clean up our test containers and volumes: 
- 
-<code bash> 
-exit 
-</code> 
- 
- 
-Clean up our containers and volume: 
- 
-<code bash> 
-docker rm Container4 Container5 Container6 
-docker volume rm DataVolume4 
-</code> 
- 
- 
-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.1487778464.txt.gz · Last modified: 2020/07/15 09:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki