Docker – 3. Volume

In this part of the Docker series, we will show you how to work with the Volume (content) to work with the data.

In the previous part of this series we told you that if we want to change the content of the file index.html, there is a complication. To solve this complication, we will not copy anew the file index.html during each run of an image. Instead we will simply link to the file index.html on the hosting system. Container will not load the content from inside (from its file system), but from the outside (from the file system of the host system). It is a faster way of running and using images, thanks to which we will not loose the content during the container’s deletion (e.g. from the file index.html).

Hint: Docker is running perfectly on Active24 VPS. Try out the premium virtual server today!

We can share the data between container and the host system, but also between other containers. And thanks to the volume the data are stored only on the host computer (not into the container itself), which also does not increase the size of the container with more data stored.

Sharing content between host system and container

Same as in the previous part, we will now work with the text configuration file Dockerfile and also in the directory ~/docker-test and with the same image nginx-test. The difference will be in the content of the Dockerfile. We will no longer copy the file index.html from the host system to the container, we will only link to it. We will edit the content of the Dockerfile in the text editor nano as follows:

nano Dockerfile

and insert these 2 lines:

FROM nginx
WORKDIR /usr/share/nginx/html

Command from the first line will run the image with the webserver Nginx. Command on the second line will secure that the command line in the container will open, rather practically, in the directory html, where the file index.html is located. With that we can check immediately after entering the container, if the file index.html is in the correct directory and if the content is trully the same as on the host computer.

File is saved with the shortcut Ctrl + X and then by pressing key y and confirming with Enter.

Create a directory with a name html and move the file index.html there:

mkdir ~/docker-test/html && mv ~/docker-test/index.html ~/docker-test/html/

Build an image with the setting from the configuration file Dockerfile in the directory ~/docker-test, as shown in the previous part presne tak (do not forget to copy the dot at the end):

docker build -t nginx-test .

After a successful build, run the image nginx-test. In contrast to the previous part we will add one more setting that will link the container to a directory on the host server. (-v ~/docker-test/html:/usr/share/nginx/htm). Colon (:) separates the path to the directory on the host computer (here ~/docker-test/html) and the path to the directory in the container (here /usr/share/nginx/html). Tha path in the host computer is always the first (on the left before colon) and the path in the container is always the second (to the right of the colon):

docker run -d -v ~/docker-test/html:/usr/share/nginx/html nginx-test

Identify the container ID (12 characters from the first column), that is running from the image nginx-test:

docker ps

…and log in to the command line (bash) of the container (your ID will differ):

docker exec -it aaaf9e616e0b bash

Thanks to the command WORKDIR in the configuration Dockerfile we are immediately in the directory html, where you can see the file index.html. Here we can check if the webserver Nginx works and what is the current content of the index.html file:

curl localhost

In the previous part we have saved to the file index.html a welcome message “Hello from the Docker container!”, so you it will be displayed even now. To check if the changes in the content on the host computer will be displayed in the container (without the need to delete, build and run the image again), we will add another line in the host computer:

echo "This sentence was written on the host computer." >> ~/docker-test/html/index.html

Let’s write down the current content of the file index.html in the container again, to check, if what we edited on the host computer, will be displayed immediately in the container:

curl localhost

We didn’t have to stop, delete, build and run again the image, all that was necessary was to edit the file on the host computer. The closed container from the previous part we now opened in a way that enables to read and write to the files on the host compurer. In addition to this advantage we also gain anothe: Even after we stop the container and delete the image we will still have the content of the file index.html stored no matter if we made any changes on the host computer or from the container.

Summary

We have learned how to add content to the container via volume, so we could read and write to the files on the host computer.

Updated on December 18, 2024

Was this article helpful?

Related Articles