docker:share_data_between_docker_containers
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
docker:share_data_between_docker_containers [2017/02/22 15:27] – created 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. | ||
- | |||
docker/share_data_between_docker_containers.1487777231.txt.gz · Last modified: 2020/07/15 09:30 (external edit)