Data management using Docker bindmount and tmpfs (personal memorandum)

Data management using bind mount

Unlike volume, it mounts files and directories managed by the host user on a container. Again, the same file or directory can be mounted in multiple containers. Since it is an area managed by the user, basically, prepare the files and directories of the mount source in advance, specify the path, and mount it in the container. By placing the project source code and configuration file on the host and mounting it, you can check the operation while editing the file on the host side.

Use -v to bind mount a directory on the current directory to a container

-# "$(pwd)"Source directory on the current directory(not exist)Is mounted.
$ docker run -itd --name bind-test1 -v “$(pwd)”/source:/app nginx
$ ls
source

If you use -v, a directory is automatically created on the host if the directory does not exist.

When using --mount

-# “$(pwd)”/You are mounting a directory that does not exist in source2.
$ docker run -itd —-name bind-test2 —mount type=bind,source=“$(pwd)”/source2,target=/app nginx

Specify Type as bind. Execute in the state where the directory called source2 does not exist in the current directory. In this case an error will occur. In case of --mount, an error will occur if the source directory does not exist. This will prevent you from accidentally mounting an empty directory. The status of bind mount can be confirmed by displaying the detailed information of the container as well as volume.

Checking the mount of the container

-# bind-Detailed confirmation of test1
$ docker inspect bind-test1 
"Mounts": [
            {
                "Type": "bind",
                "Source": "/home/docker/source",
                "Destination": "/app",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],

You can check each by Type, Source, and Destination. It is mounted readable and writable by default. Note that bind mount hides the mount directory and files in the bind directory. If you cover important files and directories with bind, the container may not work properly. You can also mount it read-only with bind mount.

Data management using tmpfs

Mount the host memory area on the container as a file system. Since the data is stored in the memory, the data will be lost if the docker host is shut down or the container is stopped. The purpose is to use it as a place to store data that is temporarily stored instead of storing persistent data, and store data that can be deleted without any problem. Cache, one-time password, etc. You can optionally limit the mount size.

When using --mount

-#Example
$ docker run -itd --name tmptest --mount type=tmpfs,destination=/app nginx

When using tmpfs, specify tmpfs for type. By default, the mount size is unlimited and if you run out of memory, swap will be used. The mount size can be limited by specifying options.

-#Example
$ docker run -itd --name tmptest2 --mount type=tmpfs,destination=/app,tmpfs-size=500000000,tmpfs-mode=700 nginx

I set the mount size to 500MB and the file mode to 700.

Recommended Posts

Data management using Docker bindmount and tmpfs (personal memorandum)
Data management using volume in Docker (personal memorandum)
Construction of data analysis environment using Docker (personal memorandum)
Building a CICD pipeline using Docker (personal memorandum)
Docker Machine (personal memorandum)
Docker network (personal memorandum)
Creating a docker host on AWS using Docker Machine (personal memorandum)
Launch docker container on EC2 (personal memorandum)
Docker Compact Manual (2: Mount and Data Persistence)
Docker memorandum
Docker memorandum
Kaggle environment construction using official Docker and vscode
Django development environment construction using Docker-compose (personal memorandum)