Dockerfile Volumes

29/12/2020
When you run a Docker container, none of the data in that container is saved. What I mean is, let’s say you started a Docker container. Then you created some files and folders in a directory (let’s say /data) there. When you stop the container, all the files and folders in the /data directory will be removed. When you start the container again, you will have to recreate all the files and folders if you need it again. None of the data will persist.

You can use Docker volumes to save changes of a certain directory of your Docker containers. In simple words, you can persist data of your Docker containers using Docker volumes.

You can tell Docker which directory of your Docker container to persist changes in your Dockerfile when you create custom Docker images using Dockerfile.

In this article, I will show you how to use Docker volumes in Dockerfile and how to manage these volumes. Let’s get started.

Using Volumes in Dockerfile:

In this section, I will create a custom Docker image of Apache 2 server from the Docker image httpd:2.4 and use Docker volumes to persist the Apache WEBROOT data.

First, create a new directory (let’s say www3) for your custom Docker image (let’s say www:v1) and navigate to it:

$ mkdir www3 && cd www3

Now create a Dockerfile with the following command:

$ touch Dockerfile

Now edit the Dockerfile file with nano or vim text editor.

Nano:

$ nano Dockerfile

Vim:

$ vim Dockerfile

Now add the following lines to it and save the file:

Here, FROM httpd:2.4 means, the base image of this custom Docker image is httpd:2.4

WORKDIR /usr/local/apache2/htdocs means, when you Iog into the shell (command line interface) of containers made from this custom image, you will be in the /usr/local/apache2/htdocs directory of the container by default.

VOLUME /usr/local/apache2/htdocs means, create a new volume for each of the containers created from this custom image and persist all the files in the /usr/local/apache2/htdocs directory of that container to this new volume.

Now that you’ve got your Dockerfile ready, it’s time to build our custom Docker image (I will call it www:v1).

To build your custom Docker image www:v1 using the Dockerfile, run the following command:

$ docker build -t www:v1

The custom Docker image www:v1 is created successfully.

Listing Locally Available Docker Images:

You can list all the locally available Docker images with the following command:

$ docker image list

As you can see, the www:v1 image is about 178MB in size and it was created about a minute ago.

Working with Docker Volumes and Containers:

We’ve created a volume enabled Docker image www:v1 in the earlier section of this article above.

To create a Docker container (let’s say app1) from the custom Docker image www:v1, run the following command:

$ docker run -d -it –name=app1 www:v1

The Docker container app1 should be created.

Now run the following command to bring up the shell of the app1 container:

$ docker exec -it app1 /bin/bash

As you can see, I am in the /usr/local/apache2/htdocs directory by default.

Let’s create a index.html file here with the following command:

$ echo "<h1>Hello World (app1)</h1>" > index.html

Now exit out of the app1 container.

$ exit

Now run the following command to find the IP address of the app1 container:

$ docker inspect app1 | grep Address

As you can see, the IP address is 172.17.0.2

Now from any web browser, if you visit the page at 172.17.0.2, you should see the index.html page that you just created.

Now let’s test whether persistence works or not.

First stop the app1 container with the following command:

$ docker container stop app1

Then start the container again with the following command:

$ docker container start app1

Now find the IP address of the container again:

$ docker inspectapp1 | grep Address

As you can see, the index.html page is unchanged when I access it from my web browser. It means Docker volume is persisting data from the app1 container.

Managing Docker Volumes:

You can list all the Docker volumes on computer with the following command:

$ docker volume list

As you can see, I have 2 Docker volumes on my computer. The long hexadecimal string as marked in the screenshot below, is the auto generated volume for the app1 container.

When you create a container from a Docker image that uses volumes, a new volume like this will be generated for each container you create.

For example, I created another container app2 from the www:v1 image that I built from the Dockerfile that uses volume.

As you can see, a new volume is generated.

Both of these volumes hold different data. The data persists for only one container for which it was generated. For example, the volume generated for the app1 container persists data only for the app1 container. The same goes for the app2 container. The volume generated for the app1 container won’t store data from the app2 container.

You can remove volumes if you don’t need it anymore. For that to work, you have to stop and remove the container the volume is attached to. If you try to remove a volume that is attached to a container, you will get the following error message.

Let’s say you want to delete the volume for app2 container, first run the following commands to stop and remove the container:

$ docker container stop app2
$ docker container rm app2&lt;

Now remove the volume that was attached to app2 container with the following command:

$ docker volume rm 962b2d970bcd96de860b348130a84aa86c0aaa55378b3dce35225659679ff808

The volume should be removed.

Adding Files to Volumes Using Dockerfile:

You can use the COPY command in your Dockerfile to copy files from your host computer to your Docker volumes. That way, any container you create from your custom built Docker image will have these files by default. You can check more about COPY in the Dockerfile documentation.

So that’s basically how you work with Docker Volumes using Dockerfile. Thanks for reading this article.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Docker Compose Tutorial

Docker’s popularity as a development tool is on the rise. Docker has breathed new life into the container movement. Developers...
28/12/2020

What Are Docker Image Tags, And How to Use Them?

Docker is an open platform to build, ship and run distributed applications. It simply means one can run different applications...
29/12/2020

How to Use Docker Registry

Docker is a technology which allows one to create virtual machines which encapsulate applications and all its dependences...
28/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024