Docker

What is Volume in Docker Image?

A Docker image is a template that includes instructions for creating/building Docker containers. Docker images are built from Dockerfile. The Dockerfile defines the base image to utilize and the dependencies to install. An image can be built or created using the Dockerfile after it has been created.

This write-up will explain about volume in the Docker image.

What is Volume in Docker Image?

In Docker, a volume is a way to persist data generated and utilized by Docker containers. The volume can be declared in a Dockerfile or on runtime. When the user declares the volume in Dockerfile using the “VOLUME” instructions, a new volume will be created each time a container is started from the image, even without specifying the “-v” option while running the container. For declaring the volume on runtime, the “-v” option is used with the “docker run” command to mount a host directory into a container directory.

How to Mount Volume in Docker Image?

To mount the volume in a Docker image, first, create a Dockerfile and declare the volume in it using the “VOLUME” instructions. Then, create/build a Docker image from the desired Dockerfile. Finally, execute the Docker image as a Docker container.

Step1: Create Dockerfile

First, create a new file named “Dockerfile” and paste the following code into it:

FROM nginx:latest
RUN echo "<h1>Welcome to Linuxhint application</h1>" > /usr/share/nginx/html/index.html
VOLUME /usr/share/nginx/html

 

In the above code:

  • FROM” instruction is utilized to specify a base image for the container. In our case, it is “nginx:latest”.
  • RUN” instruction is used to execute a command during the Docker image build/creating process.
  • echo” utility will output the string “<h1>Welcome to Linuxhint application</h1>” to the standard output, and the “>” operator is used to redirect this output to a file located at “/usr/share/nginx/html/index.html”.
  • VOLUME” instruction creates a new Docker volume at “/usr/share/nginx/html”.

This Dockerfile builds a new Docker image based on the Nginx image. The Dockerfile then runs a command to create a new file at “/usr/share/nginx/html/index.html” that contains the HTML code for the “Welcome to Linuxhint application” message. Finally, the “VOLUME” instruction creates a new Docker volume at “/usr/share/nginx/html”.

Step 2: Build Docker Image

Then, create a Docker image from the desired Dockerfile using the provided command:

docker build -t new-img .

 

Here, the “-t” option is utilized to specify the image name. For instance, we have specified the “new-img” as a name for the Docker image:

Step 3: Build Docker Container

Next, build and run the Docker container using the Docker image through the following command:

docker run --name new-cont1 -p 80:80 new-img

 

Here:

  • –name” is utilized to specify the container name.
  • new-cont1” is the name of the container.
  • -p” is used to assign the port to the container, i.e., “80:80”.
  • new-img” is the Docker image:

Step 4: Verification

Finally, open the desired browser, redirect to the allocated port, and view the deployed application:

As you can see using the “VOLUME” instruction, the local directory containing HTML files has been mounted to the “/usr/share/nginx/html” and the default Nginx landing page has been replaced with our custom content.

Conclusion

In Docker, a volume is a way or method to persist data generated and utilized by Docker containers. The volume can be declared in a Dockerfile by specifying the “VOLUME” instructions or on runtime using the “-v” option with the “docker run” command. To mount the volume in a Docker image, first, create a Dockerfile and declare the volume in it using the “VOLUME” instructions. Then, create an image from the Dockerfile. Finally, execute the Docker image as a container. This article explained about volume in the Docker image.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.