Creating and Using Data Volume Containers on Oracle Linux 7

Creating and Using Data Volume Containers

If you specify a single directory argument to the -v option of docker run, Docker creates the directory in the container and marks it as a data volume that other containers can mount. You can also use the VOLUMEinstruction in a Dockerfile to create this data volume in an image. A container that contains such a data volume is called a data volume container. After populating the data volume with files, you can use the --volumes-fromoption of docker run to have other containers mount the volume and access its data.
The following example creates a data volume container that an HTTP server container can use as the source of its web content.
To create a data volume container image and an instance of a data volume container from this image:

  1. Make a directory where you can create the Dockerfile for the data volume container image, for example:
    # mkdir -p /var/docker_projects/mymod/dvc
  2. In the new directory, create a Dockerfile that defines the image for a data volume container:
    # Dockerfile that modifies oraclelinux:6.6 to create a data volume container
    FROM oraclelinux:6.6
    MAINTAINER A N Other <>
    RUN mkdir -p /var/www/html
    RUN echo "This is the content for file1.html" > /var/www/html/file1.html
    RUN echo "This is the content for file2.html" > /var/www/html/file2.html
    RUN echo "This is the content for index.html" > /var/www/html/index.html
    VOLUME /var/www/html
    ENTRYPOINT /usr/bin/tail -f /dev/null
    The RUN instructions create a /var/www/html directory that contains three simple files.
    The VOLUME instruction makes the directory available as a volume that other containers can mount by using the --volumes-from option to docker run.
    The ENTRYPOINT instruction specifies the command that a container created from the image always runs. To prevent the container from exiting, the /usr/bin/tail -f /dev/null command blocks until you use a command such as docker stop dvc1 to stop the container.
  3. Use the docker build command to create the image:
    #[root@host ~]# docker build -t="mymod/dvc:v1" /var/docker_projects/mymod/dvc
    Uploading context  2.56 kB
    Uploading context 
    Step 0 : FROM oraclelinux:6.6
     ---> 3e4b5e722ab9
    Step 1 : MAINTAINER A N Other <>
     ---> Using cache
     ---> debe47cef9b8
    Step 2 : RUN mkdir -p /var/www/html
     ---> Running in fa94df7dd3af
     ---> 503132e87939
    Removing intermediate container fa94df7dd3af
    Step 3 : RUN echo "This is the content for file1.html" > /var/www/html/file1.html
     ---> Running in f98a14371672
     ---> e63ba0d36d88
    Removing intermediate container f98a14371672
    Step 4 : RUN echo "This is the content for file2.html" > /var/www/html/file2.html
     ---> Running in d0dca96ad53c
     ---> 27f2e2b3d207
    Removing intermediate container d0dca96ad53c
    Step 5 : RUN echo "This is the content for index.html" > /var/www/html/index.html
     ---> Running in fe39aa35b577
     ---> 89f3cb1db1c3
    Removing intermediate container fe39aa35b577
    Step 6 : VOLUME /var/www/html
     ---> Using cache
     ---> 91d394fd412e
    Step 7 : ENTRYPOINT /usr/bin/tail -f /dev/null
     ---> Running in 91b872b93b35
     ---> c6e914249bfd
    Removing intermediate container 91b872b93b35
    Successfully built 91d394fd412e
  4. Create an instance of the data volume container, for example dvc1:
    [root@host ~]# docker run -d --name dvc1 mymod/dvc:v1 tail -f /dev/null
To test that other containers can mount the data volume (/var/www/html) from dvc1, create a container namedwebsvr that runs an HTTP server and mounts its data volume from dvc1.
[root@host ~]# docker run -d --volumes-from dvc1 --name websvr -P mymod/httpd:v2
After finding out the correct port to use on the host, use curl to test that websvr correctly serves the content of all three files that were set up in the image.
[root@host ~]# docker port websvr 80
[root@host ~]# curl http://localhost:49154
This is the content for index.html
[root@host ~]# curl http://localhost:49154/file1.html
This is the content for file1.html
[root@host ~]# curl http://localhost:49154/file2.html
This is the content for file2.html