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.
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:
In my example, I am mounting an image called /data to my image.
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
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"