Docker study notes (d) Data Management

By default, all files are stored in the container vessel writable layer, this means:

  • When the container is no longer present, the data is not permanent, and if another data container needs, it may be difficult to obtain data from the container.
  • Writable layer container tightly coupled to the host running the container. No easy way to move data to another location.
  • Write writable layer container storage drivers need to manage the file system. Storage drivers using the Linux kernel file system joint. Written directly to the host file system data volume compared with the use of this additional abstraction reduced performance.

There are two options Docker containers can be stored in the host file, even if the files still exist after the container stop: volumes and bind mounts. Runs on Linux Docker, you can use tmpfs mount temporary mount.

Either way, the data in the container are the same. It discloses a container as a single file or directory in the file system.

The difference between the simple method of loading Volumes, Bind mounts tmpfs and considering the location data on the host Docker.

Difference between the three

  • Volumes stored by Docker (/ var / lib / docker / volumes / on Linux) part of the management of the host file system. Non-Docker process should not change this part of the file system. Volume is the best way to retain data in Docker.

  • Bind mounts may be stored in any location of the host system. They may even be important system files or directories. Docker host or non-Docker Docker process on the container can modify them at any time.

  • tmpfs mounts in memory of the host system, never written a host system stores only file system.

Volumes features

Volume is the preferred method of data persisted in the Docker containers and services. Some embodiments use volume comprising:

  • Shared data between a plurality of operating containers. If not explicitly create it, the volume is created in the first vessel to be loaded. When the container is stopped or removed, the volume is still there. A plurality of containers can be installed simultaneously in the same volume, may be read may be read-only. It will only delete the volume when you explicitly delete volume.

    When Docker host can not guarantee a given directory or file structure. Volume helps you configure the container to separate Docker host runtime.

  • To store the data if the container provider or cloud the remote host, rather than the local storage.

  • When you need to back up data from a host Docker, restore or migrate to another Docker host, volume is a better choice. You can stop the container volume and the backup volume directory (such as / var / lib / docker / volumes / )。

Bind mounts features

  • The configuration file from the host to share a container. This is the default installation Docker /etc/resolv.conf from a host through each vessel to provide the container way for DNS resolution.

  • Construction of the shared source or between the workpiece and the container on the development environment Docker host. For example, you can Maven target / directory is mounted to the container, each time building on the Maven project Docker host container can access the work of reconstruction.

    If you use the Docker to develop in this way, your production will be production-ready Dockerfile work directly copied to the image, rather than relying on the binding load.

  • When the required bind file or directory structure to ensure that the installation of the container Docker host agreement.

tmpfs

  • When data tmpfs undesirable persistence in the host or the container, is preferably used to mount. Protective properties of the container when this could be for safety reasons, or in the application need to write a large number of non-persistent state data.

Volumes and Bind mounts note

  • If the empty volume into a container present in the directory file or directory, the files or directories will propagate (replicate) into the volume. Similarly, if the container and start the specified volume does not yet exist, it will create an empty volume for you. This is a good way to another container pre-filled with the required data.

  • If the load or empty volume exists bind certain files or directories in the directory into a container, the loading of these files or directories will be covered, as will save the file to / mntLinux then install the USB host driver to enter / mnt. / Mnt before uninstalling the USB drive, the contents of the USB drive will block content. Hidden files are not deleted or changed, but can not access when installing or binding load volume.

Data volume (Volumes)

Data volume is a container for one or more special directory used, it bypasses the UFS, can provide some useful features:

  • Data volume can be shared and reused between the vessel

  • The data volume changes will take effect immediately

  • Updates to the data volume will not affect the image

  • The default data volume will always exist, even if the container is deleted

Note: Using data volume, similar to a directory or file mount under Linux, is designated as the mount point of the files in the directory will be hidden away in the mirror, to look at the display is mounted data volumes.

Create a data volume

$ docker volume create my-vol

See all data volumes

 $ docker volume ls
DRIVER              VOLUME NAME
local               my-vol

Use the following command in a host can view the specified data volume information

$ docker volume inspect my-vol
[
    {
        "CreatedAt": "2019-05-19T08:22:09Z",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/my-vol/_data",
        "Name": "my-vol",
        "Options": null,
        "Scope": "local"
    }
]

A starting container to mount the data volume

When using docker run command, using the -v and --mount numerals mount data volume container. In a docker run can be mounted in a plurality of data volumes.

-v or --mount difference:

If the option to specify the data volume, you must use --mount.

  • -v or --volume: it consists of three fields, the colon character (:) separated. Fields must be arranged in the correct order, and the meaning of each field is not obvious.
    • For the volume name, the first field is the name of the volume, and is unique on the given host. For anonymous volume, the first field is omitted.
    • The second field is a file or directory is mounted in the vessel path.
    • The third field is optional, is a comma-separated list of options, such as ro. These options will be discussed below.
  • --mount:, comma separated by a plurality of key-value pairs, each key = Consists of a set of tuples. The ratio is more detailed syntax --mount -v or --volume, but the order is not significant key, and the value of the flag is more easily understood.

    • type is the type of mount, can be a bind, volume, or tmpfs. This topic discusses volumes, so mount type is always volume.
    • source, named for the volume, which is the name of the volume. For anonymous volume, it is omitted. You can be specified as source or src.
    • destination file or directory path in the vessel. You may be designated as a destination, dst or target.
    • Readonly (if present) results in binding was charged into a container in a read-only manner.
    • volume-opt option can be specified more than once, it uses a key option names and their values ​​are right.

Note: If you start a container mounted volume path does not exist, Docker will be created automatically.

  • -v
$ docker run -d \
  --name devtest \
  -v myvol2:/app \
  nginx:latest
  • --mount
$ docker run -d \
  --name devtest \
  --mount source=myvol2,target=/app \
  nginx:latest

Delete data volumes

After you remove the container, Docker data volumes still exists.

  • For example, the volume name: awesome from a specific source outside of the container: / bar
  • No special anonymous source volume, so when deleting a container, it can be deleted by instruction --rm.

Delete Anonymous volume

To automatically delete anonymous volume, use --rm option. For example, create an anonymous / foo volume. After you remove the container, Docker daemon will delete / foo volume but does not delete awesome volume.

docker run --rm -v /foo -v awesome:/bar busybox top

Delete all unused data volumes and free space:

docker volume prune

Guess you like

Origin www.cnblogs.com/zuoruining/p/11031287.html