Mount a Docker Volume Using Dockerfile

In this blog post, I will show you how to create and mount a Docker image to a Docker image using a Dockerfile.

In the previous articles, I have shown you how to create a Docker volume and attach it to a container using the -v switch.

That is an excellent method; however, it is limited and hard to scale.

Dockerfile

Using the Docker VOLUME declaration, I can mount a volume to the image automatically.

Below, is a simple example of how to mount a Docker volume:

From centos
VOLUME /data

In my example, I am mounting an image called /data to my image.

Build

After I saved my dockerfile, I will build the image and call it webserver using the following command.

docker build -t webserver .

RUN \ Deploy

To deploy my image with the volume, I will deploy a container using the following command.

docker run -it --name web01 webserver /bin/sh

Inspect Volume

After the deployment, I can run the following commands to view the image configuration and the location of the mounted volume.

docker inspect web01
docker inspect web01 | grep "volume"

Processing…
Success! You're on the list.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.