How to use Docker data volumes on Ubuntu 14.04

Introduction

In this article, we will introduce the concept of Docker data volumes: what they are, why they are useful, different types of volumes, how to use them and when to use them. We will also introduce some examples of how to use Docker volumes through the docker command line tool.

When we reach the end of the article, you should be used to creating and using any type of Docker data volume.

Preparation

To follow this tutorial, you need to have the following conditions:

Students who don’t have a server can buy it from here, but I personally recommend you to use the free Tencent Cloud Developer Lab for experimentation, and then buy server.

**Note: **Although the prerequisites provide instructions for installing Docker on Ubuntu 14.04, as long as Docker is installed, the docker command of the Docker data volume in this article can be run on other operating systems.

Explain Docker container

Using Docker requires an understanding of some Docker-specific concepts, and most documents focus on explaining how to use Docker's toolset, without explaining why any of these tools should be used. If you are new to Docker, this can be confusing, so we first introduce some basics before starting to use Docker containers. If you have used Docker before and just want to know how to start using data volumes, feel free to skip to the next section.

Docker containers are similar to virtual machines. It basically allows you to run a pre-packaged "Linux box" inside a container. The main difference between Docker containers and typical virtual machines is that Docker is different from ordinary virtual machines and is isolated from the surrounding environment. The Docker container shares the Linux kernel with the host operating system, which means it does not need to be "started" like a virtual machine.

Since so much has been shared, starting a Docker container is a quick and inexpensive operation-in most cases, you can start a complete Docker container (equivalent to a normal virtual machine) command line program while running normally. This is good because it makes the deployment of complex systems easier and modular, but it is different from the usual virtual machine approach and has some unexpected side effects for people from the virtualized world.

Learn the types of Docker data volumes

There are three main use cases for Docker data volumes:

  1. Keep data when removing container
  2. Sharing data between the host file system and the Docker container
  3. Share data with other Docker containers

The third case is slightly improved, so we will not discuss it in this tutorial, but the first two cases are very common.

In the first (and simplest) case, even if you delete the container, you only want the data to be idle, so usually the easiest way is to let Docker manage where the data is stored.

Keep data persistent

"Data volume" cannot be created directly in Docker, so we created a data volume container with a volume attached. For any other containers you wish to connect to this data volume container, please use Docker's --volumes-from option to get the volume from this container and apply it to the current container. This is a bit unusual at first glance, so let's look at a quick example of how we can use this method to keep the byebye file unchanged even if the container is removed.

First, create a new data volume container to store our volume:

docker create -v /tmp --name datacontainer ubuntu

This creates a container named datacontainer based on the ubuntu image and in the /tmp directory.

Now, if we run a new Ubuntu container with the --volumes-from flag and run bash again as before, anything we write to the /tmp directory will be saved to the datacontainer container. /tmp` volume.

First, start the ubuntu image:

docker run -t -i --volumes-from datacontainer ubuntu /bin/bash

- The t command line option invokes a terminal from inside the container. The -i flag makes the connection interactive.

At the bash prompt of the ubuntu container, create a file in the location of /tmp:

echo "I'm not going anywhere">/tmp/hi

Go ahead and type exit to return to the host's shell. Now, run the same command again:

docker run -t -i --volumes-from datacontainer ubuntu /bin/bash

This time the hi file already exists:

cat /tmp/hi

You should see:

I'm not going anywhere

You can add as many --volumes-from flags as needed (for example, if you want to assemble containers that use data from multiple data containers). You can also create as many data volume containers as you need.

The only caveat with this method is that you can only select the mount path inside the container when creating the data volume container (in our example it is /tmp).

Sharing data between the host and the Docker container

Another common use of Docker containers is to share files between the host and the Docker container. This is different from how the previous example works. There is no need to create a "data only" container first. You can simply run any Docker image container and overwrite one of the directories with the contents of the directories on the host system.

As a quick real-world example, suppose you want to use the official Docker Nginx image, but you want to keep a permanent copy of the Nginx log file for later analysis. By default, the nginxDocker image will log to the /var/log/nginx directory, but this is /var/log/nginx in the Docker Nginx container. Usually, it cannot be accessed from the host file system.

Let's create a folder to store our logs, and then use a shared volume to run a copy of the Nginx image so that Nginx writes its logs to the host's file system instead of /var/log/nginx inside the container:

mkdir ~/nginxlogs

Then start the container:

docker run -d -v ~/nginxlogs:/var/log/nginx -p 5000:80-i nginx

This run command is slightly different from the commands we have used so far, so let's break it down bit by bit:

If you pay close attention, you may also notice another difference from the previous run command. So far, we have been specifying a command at the end of all run statements (usually /bin/bash) to tell Docker what command to run inside the container. Because the Nginx image is an official Docker image, it follows Docker best practices, and the creator of the image sets up the image to run commands to automatically start Nginx. We can abandon the regular /bin/bash here and let the creator of the image choose the command to run in the container for us.

So, we now have a copy of Nginx running inside a Docker container on our machine, and our host port 5000 is directly mapped to a copy of Nginx port 80. Let's use curl to make a quick test request:

curl localhost:5000

You will get a screen display HTML from Nginx showing that Nginx is up and running. But more interestingly, if you look at the ~/nginxlogs folder on the host and look at the access.log file, you will see a log message from Nginx showing our request:

cat ~/nginxlogs/access.log

You will see something like:

172.17.42.1- - [23 /Oct/2015:05:22:51+0000]"GET / HTTP/1.1"200612"-""curl/7.35.0""-"

If you make any changes to the ~/nginxlogs folder, you can also view them from the Docker container in real time.

in conclusion

in conclusion! We have now introduced how to create a data volume container whose capacity can be used as a way to store data in other containers, and how to share folders between the host file system and the Docker container. In terms of Docker data volumes, this covers all use cases except the most advanced use cases.

If you are using Docker Compose, you can configure the Docker data volume in the docker-compose.yml file. For more information, please see How to install and use Docker Compose on Ubuntu 14.04.

Good luck and happy Dockering!

To learn more about using Docker data volume related tutorials, please go to Tencent Cloud+Community to learn more.


Reference: "How To Work with Docker Data Volumes on Ubuntu 14.04"

Recommended Posts

How to use Docker data volumes on Ubuntu 14.04
How to install and use Docker on Ubuntu 20.04
How to install and use Docker on Ubuntu 16.04
How to install Docker Compose on Ubuntu 18.04
How to use Samba server on Ubuntu 16.04
How to install Prometheus with Docker on Ubuntu 14.04
How to install and use Docker on CentOS 7
How to install and use Composer on Ubuntu 18.04
How to install and use Wine on Ubuntu 18.04
How to install and use Composer on Ubuntu 20.04
How to install and use BaasBox on Ubuntu 14.04
How to use Jenkins to build automatically on Ubuntu
How to install and use PostgreSQL on Ubuntu 16.04
How to use LVM to manage storage devices on Ubuntu 18.04
How to backup and restore Redis data on Ubuntu 14.04
How to install and use MySQL Workbench on Ubuntu 18.04
How to install Ruby on Ubuntu 20.04
How to install Java on Ubuntu 20.04
How to use hanlp in ubuntu
How to install VirtualBox on Ubuntu 20.04
How to install Elasticsearch on Ubuntu 20.04
How to install Protobuf 3 on Ubuntu
How to install Nginx on Ubuntu 20.04
How to install Apache on Ubuntu 20.04
How to install Git on Ubuntu 20.04
How to install Node.js on Ubuntu 16.04
How to install Vagrant on Ubuntu 20.04
How to install Bacula-Web on Ubuntu 14.04
How to install PostgreSQL on Ubuntu 16.04
How to install Git on Ubuntu 20.04
How to install Anaconda3 on Ubuntu 18.04
How to install Memcached on Ubuntu 18.04
How to install Jenkins on Ubuntu 16.04
How to install MemSQL on Ubuntu 14.04
How to install Go on Ubuntu 20.04
How to install MongoDB on Ubuntu 16.04
How to install Mailpile on Ubuntu 14.04
How to install PrestaShop on Ubuntu 16.04
How to upgrade to PHP 7 on Ubuntu 14.04
How to install Skype on Ubuntu 20.04
How to install Jenkins on Ubuntu 20.04
How to install Python 3.8 on Ubuntu 18.04
How to install KVM on Ubuntu 18.04
How to install KVM on Ubuntu 20.04
How to install opencv3.0.0 on ubuntu14.04
How to install Anaconda on Ubuntu 20.04
How to install Prometheus on Ubuntu 16.04
How to install Jenkins on Ubuntu 18.04
How to deploy Django on Ubuntu 14.04
How to install Apache on Ubuntu 20.04
How to install R on Ubuntu 20.04
How to install Moodle on Ubuntu 16.04
How to install Solr 5.2.1 on Ubuntu 14.04
How to install Teamviewer on Ubuntu 16.04
How to secure Nginx on Ubuntu 14.04
How to install MariaDB on Ubuntu 20.04
How to install Nginx on Ubuntu 20.04
How to install Mono on Ubuntu 20.04
How to install Go on Ubuntu 20.04
How to install Zoom on Ubuntu 20.04
How to uninstall software on Ubuntu