Procedure 1: Using Data Containers
Step 1 – Create Container
Data Containers are containers which sole responsibility to be a place to store/manage data.
Like other containers they are managed by the host system. However, they don’t run when you perform a
docker ps command.
To create a Data Container we first create a container with a well-known name for future reference. We use busybox as the base as it’s small and lightweight in case we want to explore and move the container to another host.
When creating the container, we also provide a -v option to define where other containers will be reading/saving data.
Create a Data Container for storing configuration files using
docker create -v /config --name dataContainer busybox
$ docker create -v /config --name dataContainer busybox Unable to find image 'busybox:latest' locally latest: Pulling from library/busybox fb46b04c527d: Pull complete Digest: sha256:2b30a608a73cb0706bee4fbf16cbb0cc25ccc5874d5a183cb93a7fd45b032c25 Status: Downloaded newer image for busybox:latest 3eac67e1f22836007fb99b305f84a990dec402962a3b866c2adf6b77aea918dd $
Step 2 – Copy Files
With the container in place, we can now copy files from our local client directory into the container.
To copy files into a container you use the command docker cp. The following command will copy the config.conf file into our dataContainer and the directory config.
docker cp config.conf dataContainer:/config/
Step 3 – Mount Volumes From
Now our Data Container has our config, we can reference the container when we launch dependent containers requiring the configuration file.
Using the –volumes-from <container> option we can use the mount volumes from other containers inside the container being launched. In this case, we’ll launch an Ubuntu container which has reference to our Data Container. When we list the config directory, it will show the files from the attached container.
docker run --volumes-from dataContainer ubuntu ls /config
If a /config directory already existed then, the volumes-from would override and be the directory used. You can map multiple volumes to a container.
Step 4 – Export / Import Containers
If we wanted to move the Data Container to another machine then we can export it to a .tar file.
docker export dataContainer > dataContainer.tar
docker import dataContainer.tar will import the Data Container back into Docker.
Procedure 2: Using Volumes
Step 1 – Data Volumes
Docker Volumes are created and assigned when containers are started. Data Volumes allow you to map a host directory to a container for sharing data.
This mapping is bi-directional. It allows data stored on the host to be accessed from within the container. It also means data saved by the process inside the container is persisted on the host.
This example will use Redis as a way to persist data. Start a Redis container below, and create a data volume using the -v parameter. This specifies that any data saved inside the container to the /data directory should be persisted on the host in the directory /docker/redis-data.
docker run -v /docker/redis-data:/data \
--name r1 -d redis \
redis-server --appendonly yes
We can pipe data into the Redis instance using the following command.
cat data | docker exec -i r1 redis-cli --pipe
Redis will save this data to disk. On the host we can investigate the mapped direct which should contain the Redis data file.
This same directory can be mounted to a second container. One usage is to have a Docker Container performing backup operations on your data.
docker run -v /docker/redis-data:/backup ubuntu ls /backup
Step 2 – Shared Volumes
Data Volumes mapped to the host are great for persisting data. However, to gain access to them from another container you need to know the exact path which can make it error-prone.
An alternate approach is to use -volumes-from. The parameter maps the mapped volumes from the source container to the container being launched.
In this case, we’re mapping our Redis container’s volume to an Ubuntu container. The /data directory only exists within our Redis container, however, because of -volumes-from our Ubuntu container can access the data.
docker run --volumes-from r1 -it ubuntu ls /data
This allows us to access volumes from other containers without having to be concerned how they’re persisted on the host.
Step 3 – Read-only Volumes
Mounting Volumes gives the container full read and write access to the directory. You can specify read-only permissions on the directory by adding the permissions :ro to the mount.
If the container attempts to modify data within the directory it will error.
docker run -v /docker/redis-data:/data:ro -it ubuntu rm -rf /data
Step 4 – Docker Volumes
Create volume using “#docker volume create”. In the example below we create “redis_data” volume used as data directory for redis container.
starting the redis container with “redis_data” volume as data directory
Now lets see what is present in the data directory and create a test file.
starting and sharing the same volume with second redis container ( redis2 ) data directory. We can see test.txt file created above already present and now created one more file test2.txt.
go back to the first redis container and expected we can see both files.