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.
![](https://ntweekly-3e2e1f4957bdf35452c0-endpoint.azureedge.net/blobntweekly18036ad1fb/wp-content/uploads/2020/05/050120_0326_MountaDocke1.png)
Build
After I saved my dockerfile, I will build the image and call it webserver using the following command.
docker build -t webserver .
![](https://ntweekly-3e2e1f4957bdf35452c0-endpoint.azureedge.net/blobntweekly18036ad1fb/wp-content/uploads/2020/05/050120_0326_MountaDocke2.png)
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
![](https://ntweekly-3e2e1f4957bdf35452c0-endpoint.azureedge.net/blobntweekly18036ad1fb/wp-content/uploads/2020/05/050120_0326_MountaDocke3.png)
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"
![](https://ntweekly-3e2e1f4957bdf35452c0-endpoint.azureedge.net/blobntweekly18036ad1fb/wp-content/uploads/2020/05/050120_0326_MountaDocke4.png)
![](https://ntweekly-3e2e1f4957bdf35452c0-endpoint.azureedge.net/blobntweekly18036ad1fb/wp-content/uploads/2020/05/050120_0326_MountaDocke5.png)