Docker Compact Manual (2: Mount and Data Persistence)

At first

This article "Docker Compact Manual" consists of 4 items. Other articles are here.

Post number Subtitle and access destination
1 Elementary / basic commands
2 Mount and data persistence(* This article)
3 docker-compose
4 Create a custom image

Purpose of this article

This is a continuation from the previous article. This time, the mount, volume, which cannot be avoided when using the container, Here is a summary of data persistence.

important ** This time is important. I have regretted myself without knowing the mount and persistence. : sob: Please try to build a comfortable container environment by mounting and perpetuating firmly. **: grin:

Persistence of container data

** The data in the container will be lost if you destroy the container. ** ** It's okay if you throw it away for test operation, but you want to avoid erasing data for use in Database. Data that should not be lost inside the container needs to be set to go out of the container and the container looks out.

Execution system = inside the container
Acquired data / update data = outside the container

I think that the image of is good.

Use the mount to remove this container.

Mount type

There are two types of mounts. Not which one is better I think it is necessary to use them properly depending on the content to be handled.

Mount type Mount destination Recommended usage
volume Area reserved on Docker Engine Data you don't want to see from your Docker container
Database data, etc.
bind Docker Host directory When you want to show Docker Host files to a container
Configuration file passing data
Data for which changes are reflected immediately

スクリーンショット 2020-10-26 11.14.10.png

スクリーンショット 2020-10-26 11.15.19.png

Volume creation command

Volumes can be created, listed, and deleted with the docker volume command. If you use inspect, you can find the save destination by Mount point of volume.

Subcommand Contents Usage example
create Volume creation docker volume create --name 'Volume name'
inspect Check the details of the volume docker volume inspect 'Volume name'
ls List of volumes docker volume ls
prune Delete all unmounted volumes docker volume prune
rm Delete volume docker volume rm 'Volume name'

About mount settings

Mount settings (use in combination with docker run, etc.) Set with the -v or --mount flags. Until now, -v is often used, and there are many descriptions in books and reference materials,

I think it's better to use --mount.

As a reason

1. If the volume does not exist, it will be created without permission.

The following is an excerpt from the official website, In the case of -v, if you make a typo, a new volume will be created. I get an error that the previous data is not visible. If this error occurs and you proceed with development, you will have a hard time dealing with it when you notice it later. On the other hand, --mount will return an error if it does not exist, so you can rest assured.

Source: docker docs If you bind mount a file or directory using> -v or --volume and the file or directory does not already exist on the Docker host, -v will generate that mount endpoint. In that case, it is always generated as a directory.

If you bind mount a file or directory using> --mount and the file or directory does not exist on the Docker host, Docker will not automatically generate the file or directory. An error will be output instead.

2. It is difficult to tell whether it is a bind mount or a volume mount.

Setting with -v

-v / home / ubuntu / ***: / usr / local / *** bind mount -v mysqlvolume: / var / lib / mysql volume mount The above difference is difficult to understand with or without the first /

--mount

--mount type = bind, src = / home / ubuntu / ***. Dst = / usr / local / *** bind mount --mount type = volume, src = mysqlvolume, dst = / var / lib / mysql Volume mount

For --mount, use type to mount bind or volume. Specify where to mount which data with src = sorce (mount source) and dst = destination (mount destination) It seems that the operation here will reduce mistakes.

Data backup

Backup is easy with bind mount, (Since it exists on the host, it can be handled by copying data as it is.) If it is a volume mount, backup requires work.

First, the sample command for backup is described below.

docker run --rm --mount type=volume,src=*****,dst=/src --mount type=bind,src="$PWD",dst=/dest busybox tar czf /dest/backup.tar.gz -C /src .

Let's look at the contents in order.

  1. Discard after running with docker run --rm
  2. Specify to execute volume mount in / src of busybox with --mount type = volume, src = *****, dst = / src
  3. Specify the bind mount in / dest of busybox with --mount type = bind, src =" $ PWD ", dst = / dest
    Specify the current directory of the Docker host with" $ PWD ".
  4. Lightweight Linux running on busybox
  5. Copy gz format file creation from / src to / dest with tar (command for handling archive files) + gzf
  6. Since it is bind-mounted by PWD, it is written to the current directory.

This completes the volume backup.

The above is one backup method This method requires you to know the ** backup destination volume name **. When managing many containers, it can be difficult to know which container is using which volume.

In that case

Use volumes-from.

--volumes-from is when you start the container It inherits another container mount information and mounts with the same settings. Since the backup target can be specified by the directory name of the container instead of the volume name You don't need to be aware of which volume the container's directory is mounted on. Very useful for data backup.

Sample code

docker run --rm --volumes-from {{Container name}} -v "$PWD":/dest busybox tar czf /dest/backup.tar.gz -C {{Target address}} .
  1. Specifying volumes-from
  2. Start busybox and back up the container The above 2 steps are easy.

Also, when using a volume, it seems better to set up a ** data volume container **. The data volume container does not operate mainly as a container activity, It mounts only the directories you need. Although this data volume container does not operate mainly

1. You can use the mount settings without being aware of the mount destination of the volume.
2. Since the volume mount information can be managed in the data volume container, the backup target is clarified.

It has the advantage of being used.

Backup restore

Restoration is the opposite of volume backup.

First, create a volume.

docker volume create *****

Then enter the command in the reverse direction of the data backup.

docker run --rm --mount type=volume,src=*****,dst=/dest --mount type=bind,src="$PWD",dst=/src busybox tar xzf /src/backup.tar.gz -C /dest
  1. Volume mount the restore destination volume to / dest
  2. Bind mount the current directory to / src
  3. Extract with tar xzf
  4. Mount the restored volume and the data will be available again.
    (Command posting omitted)

This article ends here. Thank you for reading.

Recommended Posts

Docker Compact Manual (2: Mount and Data Persistence)
Docker Compact Manual (3: docker-compose)
Docker Compact Manual (1: Basic / Basic Command)
Docker Compact Manual (4: Create a custom image)
CI/CD pipeline and Docker
Docker installation and initialization
Docker terms and commands