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 |
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:
** 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.
I think that the image of is good.
Use the mount to remove this container.
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 |
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' |
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,
As a reason
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.
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.
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.
docker run --rm
--mount type = volume, src = *****, dst = / src
--mount type = bind, src =" $ PWD ", dst = / dest
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
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}} .
volumes-from
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
It has the advantage of being used.
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
Recommended Posts