Mount a Docker Volume Using Dockerfile

In this blog post, we will learn 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"


Posted

in

by

Comments

One response to “Mount a Docker Volume Using Dockerfile”

  1. […] the following example, I will deploy a Docker container that has a volume attached to it. After the container has been deployed, I will rename the container using the docker […]