Method: 1. Use the run command, the syntax is "docker run --name test1 -it -v"; 2. Use the VOLUME instruction of dockerfile to create a mount point, the syntax is "VOLUME ["/data1" ,"/data2"]"; 3. Use container shared volumes.
The operating environment of this tutorial: linux7.3 system, docker-1.13.1 version, Dell G3 computer.
What are the mounting methods of docker?
Before introducing the VOLUME instruction, let’s take a look at the following scenario requirements:
1. The container is created based on the image. Finally The container file system includes a read-only layer and a writable layer of the image. The data persistence of process operations in the container is saved on the writable layer of the container. Once the container is deleted, the data is gone unless we manually back it up (or create a new image based on the container). Can the data persisted by the container process be saved on the host? In this way, even if the container is deleted, the data is still there.
2. When we develop a web application, the development environment is local to the host, but the running test environment is placed on the docker container.
In this case, after I modify the files (such as html, js, etc.) on the host, I need to synchronize them to the container. This is obviously more troublesome.
3. Multiple containers run a set of associated services. What if they want to share some data?
We can certainly think of various solutions to these problems. Docker itself provides a mechanism that can associate a directory on the host with a directory in the container (called a mount point, or volume). The content under the mount point on the container is the host. The contents of the directory are similar to the mount mechanism under Linux systems. In this case, when we modify the contents of the directory on the host, we do not need to synchronize the container, and it will take effect immediately for the container. Mount points can be shared by multiple containers.
Let’s introduce the specific implementation mechanism.
First pass the docker run command
1. Run the command: docker run --name test -it -v /home/xqh/myimage:/data ubuntu /bin/bash
The -v flag sets a mount point /data in the container (which is a directory in the container), and associates the contents of the /home/xqh/myimage directory on the host to /data.
In this way, operations on the /data directory in the container and operations on /home/xqh/myimage on the host are completely synchronized in real time, because these two directories actually point to the host directory. .
2. Run the command: docker run --name test1 -it -v /data ubuntu /bin/bash
The -v mark above only sets the mount point of the container, and does not Specify the associated host directory. At this time, docker will automatically bind a directory on the host. You can view it through the docker inspect command.
xqh@ubuntu:~/myimage$ docker inspect test1 [ { "Id": "1fd6c2c4bc545163d8c5c5b02d60052ea41900a781a82c20a8f02059cb82c30c", ............................. "Mounts": [ { "Name": "0ab0aaf0d6ef391cb68b72bd8c43216a8f8ae9205f0ae941ef16ebe32dc9fc01", "Source": "/var/lib/docker/volumes/0ab0aaf0d6ef391cb68b72bd8c43216a8f8ae9205f0ae941ef16ebe32dc9fc01/_data", "Destination": "/data", "Driver": "local", "Mode": "", "RW": true } ],
Each piece of information under Mounts above records the information of a mount point on the container. The "Destination" value is the mount point of the container, and the "Source" value is the corresponding host directory. It can be seen that the host directory corresponding to this method is automatically created. Its purpose is not to modify it on the host, but to share it with multiple containers.
2 Create a mount point through dockerfile
The mount point created by the -v flag of the docker run command introduced above can only be valid for the created container. Mount points can be created in the image through the VOLUME directive of the dockerfile, so that all containers created through the image will have a mount point. Another difference is that the mount point created through the VOLUME command cannot specify the corresponding directory on the host and is automatically generated.
#test FROM ubuntu MAINTAINER hello1 VOLUME ["/data1","/data2"]
The above dockfile file specifies two mount points /data1 and /data2 through the VOLUME instruction.
We use docker inspect to view the container generated by the image created by this dockerfile , you can see the following information
"Mounts": [ { "Name": "d411f6b8f17f4418629d4e5a1ab69679dee369b39e13bb68bed77aa4a0d12d21", "Source": "/var/lib/docker/volumes/d411f6b8f17f4418629d4e5a1ab69679dee369b39e13bb68bed77aa4a0d12d21/_data", "Destination": "/data1", "Driver": "local", "Mode": "", "RW": true }, { "Name": "6d3badcf47c4ac5955deda6f6ae56f4aaf1037a871275f46220c14ebd762fc36", "Source": "/var/lib/docker/volumes/6d3badcf47c4ac5955deda6f6ae56f4aaf1037a871275f46220c14ebd762fc36/_data", "Destination": "/data2", "Driver": "local", "Mode": "", "RW": true } ],
You can see the information of the two mount points.
Three-container shared volume (mount point)
Next we create another container that can share the /data1 and /data2 volumes with test1 (already created container). This is done in docker run Use the --volumes-from tag, such as:
can be from different mirrors, such as:
docker run --name test2 -it --volumes-from test1 ubuntu /bin/bash
, or it can be the same mirror, such as:
docker run --name test3 -it --volumes-from test1 myimage /bin/bash
The three above Each container test1, test2, and test3 has two directories: /data1 and /data2, and the contents in the directory are shared. If any container modifies the content, other containers can obtain it.
Four Best Practices: Data Container
If multiple containers need to share data (such as persistent databases, configuration files or data files, etc.), you can consider creating a specific data container. Containers have 1 or more volumes.
Other containers share the volumes of this data container through –volumes-from.
Because the volume of the container essentially corresponds to the directory on the host, this data container does not need to be started.
For example: docker run --name dbdata myimage echo “data container”
Note: There is a volume, data sharing between containers is more convenient, but there are also many problems that need to be solved, such as permissions Control, data backup, volume deletion, etc. These contents will be introduced in subsequent articles.
Recommended learning: "docker video tutorial"
The above is the detailed content of What are the mounting methods for docker?. For more information, please follow other related articles on the PHP Chinese website!

The methods of installing and using Docker on Ubuntu, CentOS, and Debian are different. 1) Ubuntu: Use the apt package manager, the command is sudoapt-getupdate&&sudoapt-getinstalldocker.io. 2) CentOS: Use the yum package manager and you need to add the Docker repository. The command is sudoyumininstall-yyum-utils&&sudoyum-config-manager--add-repohttps://download.docker.com/lin

Using Docker on Linux can improve development efficiency and simplify application deployment. 1) Pull Ubuntu image: dockerpullubuntu. 2) Run Ubuntu container: dockerrun-itubuntu/bin/bash. 3) Create Dockerfile containing nginx: FROMubuntu;RUNapt-getupdate&&apt-getinstall-ynginx;EXPOSE80. 4) Build the image: dockerbuild-tmy-nginx. 5) Run container: dockerrun-d-p8080:80

Docker simplifies application deployment and management on Linux. 1) Docker is a containerized platform that packages applications and their dependencies into lightweight and portable containers. 2) On Linux, Docker uses cgroups and namespaces to implement container isolation and resource management. 3) Basic usages include pulling images and running containers. Advanced usages such as DockerCompose can define multi-container applications. 4) Debug commonly used dockerlogs and dockerexec commands. 5) Performance optimization can reduce the image size through multi-stage construction, and keeping the Dockerfile simple is the best practice.

Docker is a Linux container technology-based tool used to package, distribute and run applications to improve application portability and scalability. 1) Dockerbuild and dockerrun commands can be used to build and run Docker containers. 2) DockerCompose is used to define and run multi-container Docker applications to simplify microservice management. 3) Using multi-stage construction can optimize the image size and improve the application startup speed. 4) Viewing container logs is an effective way to debug container problems.

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

The methods to view Docker logs include: using the docker logs command, for example: docker logs CONTAINER_NAME Use the docker exec command to run /bin/sh and view the log file, for example: docker exec -it CONTAINER_NAME /bin/sh ; cat /var/log/CONTAINER_NAME.log Use the docker-compose logs command of Docker Compose, for example: docker-compose -f docker-com

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.