IO /question/24 in a very early post dockone. IO /question/24 someone asked: “Please tell me if the code is inside or outside the Docker”.
Because the code in the development environment is always changing, and many people collaborate with Git, the code is kept outside. Build an image of the runtime environment, and then use volume to map the code part into it, which is easy to adjust at any time.
My point of view is the same. At present, the Docker I have learned is mostly used for local development and has not yet been tested or deployed in a real environment. Therefore, I currently agree to use Docker as a deployment development environment, and then map the code and database into containers using volume.
So the topic of today’s article is: Learning Docker Volume
Docker Volume
A volume is a specially-designated directory within one or more containers that bypasses the Union File System. Volumes are designed to persist data, independent of the container’s life cycle. Docker therefore never automatically delete volumes when you remove a container, nor will it “garbage collect” volumes that are no longer referenced by a container. Also known as: data volume
There are three types of volumes: host, anonymous, and named:
A host volume lives on the Docker host’s filesystem and can be email exchange from within the container.
A named volume is a volume which Docker manages where on disk the volume is created, but it is given a name.
An anonymous volume is similar to a named volume, however, it can be difficult, to refer to the same volume over time when it is an anonymous volumes. Docker handle where the files are stored.
Docker Volume is mounted
You can mount volumes using -v or create a data volume container using –volumes-from.
-v mounting mode
-v [host-dir]:[container-dir]:[rw|wo]Copy the code
Among them,
· Host-dir: indicates the directory on the host. If it does not exist, Docker will automatically create the directory on the host. · Container-dir: indicates the corresponding directory inside the container. If the directory does not exist, Docker will also create it inside the container. Rw | ro: used to control the volume of read and write permissions.
Therefore, [host-dir]:[container-dir] contains four combinations. Whether container-dir exists is not considered.
Host-dir = host-dir = host-dir
docker run -it -p 8890:8080 --rm -v /usr/local/tomcat/webapps --name test1 the tomcat: 8.0Copy the code
Then use the following command to view the data volume mounted in the container:
docker inspect test1Copy the code
The mount path seen at this point is temporary; The corresponding directory in the container is not overwritten either:
Host-dir = host-dir = host-dir = host-dir
docker run -it -d -p 8891:8080 --rm -v /Users/ye/docker/learning/javademo/volume2:/usr/local/tomcat/webapps --name testTomcat: 8.0Copy the code
Then use the following command to view the data volume mounted in the container:
docker inspect test2Copy the code
You can see that the host local folder is mounted on it:
At this point, we can see that the files in the corresponding directory in the container are the same as those in the host directory
If we add a file named world.java to the host, let’s see:
Stay consistent!
– volumes – from mount
Many times, we will deploy related containers on the same host and want them to share some data. At this point, we can create a data volume container that can then be mounted by multiple containers.
I will not continue to elaborate here, because I have learned that Docker Volume has not really used the data Volume container, so I have no say, after I have used the data Volume container, I will supplement the learning content in this area.
Data volume operation commands
There are mainly create, inspect, ls, prune and rm commands, among which ls is an example.
docker volume ls
List volumes Displays all data volumes
Command:
docker volume ls [OPTIONS]Copy the code
Such as:
[OPTIONS] command:
Name, shorthand | Default | Description |
---|---|---|
–filter, -f | Provide filter values (e.g. ‘dangling=true’) | |
–format | Pretty-print volumes using a Go template | |
–quiet, -q | false | Only display volume names |
The other commands are relatively simple:
Command | Description |
---|---|
docker volume create | Create a volume |
docker volume inspect | Display detailed information on one or more volumes |
docker volume ls | List volumes |
docker volume prune | Remove all unused volumes |
docker volume rm | Remove one or more volumes |
More reference website description: docs.docker.com/engine/refe…
conclusion
Although in the learning process, it is found that the mount method of using -V is more than that of using data volume container, mainly because of local learning or single project development. But in real product development, I believe –volume from will be used a lot, especially in production environments. Wait for us to continue to learn, also hope someone can lift me ~~~, thank you!
Coding01 looks forward to your attention
And thank you for seeing this