Mirroring is the basis of the container's operation, and the container is the pattern behind the mirror.
The concept of mirroring
Mirroring is a read-only file that contains the necessary environment and code for the program to run, and it uses a hierarchical file system that adds each change to the original read-only file in the form of a read-write layer
System structure of the mirror
The essence of the image is a collection of files on disk, creating a new image is actually the deletion and modification of the existing image files. But the mirror is not isolated, but there is a one-way file dependencies, such as, the Mirror 1 Filea is a modified version of the Mirror 2FileA, NewFile is the newly created file, the other files are all from the Mirror 2
Write-time replication mechanism for mirrors
When you create a container from the Docker Run command, you actually create an empty read-write file system layer on the mirror, which you can think of as a new temporary image, and the image specified in the command is called the parent image, and the contents of the parent image are mounted in read-only mode. The container reads the contents of the shared parent image. However, once the parent image file needs to be modified, Docker will be triggered to copy the file from the parent image into the temporary image, all of the changes will occur in your file system without any effect on the parent image, which is the write-time copy mechanism of the Docker image.
management of local mirrorsView
View all mirrors on this machine via Docker images
[Email protected] ~]# docker images; REPOSITORY TAG IMAGE ID CREATED VIRTUAL sizeimport latest e9757477bda9 21 hours ago 190.6 mbimported centos 4ef3250d4123 hours ago 190.6 mbcentos 6.7 a8b4df3be85c 3 months ago 190.6~]#
Explanation of the fields in the above code results:
reoository: Warehouse name. The warehouse name is used to hold the same type of mirror, whose name is specified by its creator, or <none> if not specified
About the warehouse name, there are several forms:
- [Namespace\centos]: consists of the command space and the actual warehouse name, China is separated by \
- [CentOS]: Only the warehouse name, for such a warehouse name without command space, can be considered to belong to the top-level namespace that the space of the warehouse only for the official mirror
- [dl.dockerpool.com:5000\centos:6.7]: Specify the way URL path, if the image is not to prevent on the Docker hub, but to prevent on your own or third-party hub, then use this command mode
- TAG: Used to differentiate between different mirrors in the same warehouse, and if not specified, the default is latest
- Image ID: Each mirror has a string type, 64-bit Hashid, which is used to identify a mirror across the network. The field is only shown in the previous section because this part is sufficient to uniquely identify a mirror in this machine.
- CREATED: Creation time of the mirror
- Virtual Size: The size that the image takes up, which includes the size of all shared files
Download
When the Docker Run command runs a mirror, Dokcer first looks for the image to be present on the local machine, and if it does not, it searches on the Docker hub
You can also search for a mirror that meets the requirements by searching the command
[[Email protected] ~]# Docker Search Wordpressname DESCRIPTION STARS OFFI cial automatedwordpress The WordPress rich content management Syst ... 1434[OK] appcontainers/wordpress Centos/debian Based Customizable WordPress ... the[Ok]bitnami/wordpress Bitnami Docker Image forWordpress -[Ok]kaihofstetter/WORDPRESS-CLI installs a configured and use Wor ... A[Ok]centurylink/wordpress WordPress image with MySQL removed. A[Ok]trafex/wordpress WordPress container with Nginx1.10& PHP-...3[Ok]maximiliend/wordpress A Enhanced docker image of WordPress with ...3[Ok]scjalliance/wordpress WordPress with GD and FreeType2[Ok]dsifford/wordpress A WordPress Docker environment that justW...2[Ok]dsteinkopf/wordpress WordPress clone plus some PHP extensions1[Ok]bakudankun/wordpress-ja WordPress Japanese Edition forked from off ...1[Ok]orukami/wordpress WordPress image based on FPM and Nginx.1[Ok]momonga/wordpress WordPress with Mbstring, SendMail1[Ok]ddaishin/wordpress WordPress Japanese Latest version Xdebug OSX ...1[Ok]indiehosters/wordpress WordPress Image forIndiehosters Service0[Ok]derjudge/wordpress1:1Clone of Docker-library/wordpress with ...0[Ok]deardooley/wordpress Production-ready minimal WordPress images.0[Ok]cuuats/wordpress WordPress Image forCuuats websites.0[Ok]miqueladell/wordpress Just a tailor made WordPress.0[Ok]ikoptiev/wordpress official WordPress image with MaxfileUPL ...0[Ok]rwindegger/wordpress A WordPress php-fpm backend.0[Ok]jamesits/wordpress Docker Image forWORDPRESS/APACHE/PHP7, op ...0[Ok]appeine/wordpress Container Holding WordPress sources0[Ok]svendocker/wordpress WordPress + mcrypt and Recode0[Ok]fusengine/wordpress WordPress with PHP7 and Nginx0[OK]
If the name of the command mirror does not have a namespace, it indicates that the image belongs to the official image of the Docker hub
Official: Indicates whether it is an official image
If you want to download through the pull command, but the image name must be complete containing the command space and the warehouse name, if there is more than one mirror in a warehouse, you need to make a tag, otherwise the default tag is latest
Delete
For an image that is not needed, it is removed by RMI command
If it is not possible to remove the situation, the mirror is generally dependent on the container, so you can force the removal through the-f parameter
Remove all containers: Docker RM $ (Docker ps–a-q)
Create a local mirrorTo create a local mirror using the commit command
First create and run a container with Docker run-t-I centos:6.7 and a new file in the home directory, write: My name is new and save
Remember the container's id:076c8448ce82 when creating the container
Then exit the container and execute the following command:
[email protected] ~]# Docker commit-m="Message" --author="zhaofan " 076c8448ce82 zhaofan/test:v1ad8352f07c0e9183b57c365dfac389e1327eb23be08935e8646f3ac7f31deddb
This time to view the previously created file, the discovery file is still
Creating a mirror using Dockerfile
The official recommendation is to create it in a dockerfile way
All operations that require mirroring are written to a single file
To create a mirror using the Docker build command
Dockerfile file in the unit of behavior, beginning with the Dockerfile command, the command is uppercase, followed by the parameters of the command
Here is an example of a dockerfile file:
# Version:1.0.1From Ubuntu:latestmaintainer xxh"[email protected]"#设置root用户为后续命令的执行者USER root# Perform the operation of the run apt-Get Updaterun apt-getInstall-y nginx# Use&&Stitching Command RunTouchTest.txt &&Echo "ABC">>abc.txt# External exposure port expose the 8080 1038#添加文件ADD abc.txt/opt/#添加文件夹ADD/webapp/opt/webapp# Adding network files add https://www.baidu.com/img/bd_logo1.png/opt/#设置环境变量ENV Webapp_port=9090#设置工作目录WORKDIR/opt/#设置启动命令ENTRYPOINT ["ls"] #设置启动参数CMD [" -A","- L"] #设置卷VOLUME ["/data","/var/www"] #设置子镜像的触发操作ONBUILD ADD. /app/Srconbuild RUNEcho "On build excuted">> Onbuild.txt
from : Specifies the parent image to be extended, in addition to the comment, at the beginning of the file must be a from instruction, and the next instruction runs in the environment of the parent mirror until the next from instruction is encountered. By adding multiple from directives, you can create multiple mirrors in the same dockerfile file
Maintainer : used to declare the author information to create the image, this is not required
RUN: the command used to modify the image, commonly used to install libraries, programs, and configuration programs. When a run command finishes executing, a new mirror layer is created on the current mirror, and the next instruction continues on the new mirror, with two forms of the run statement:
RUN apt-get Update
RUN ["Apt-get", "Update"]
The first form is to execute the specified command in the/bin/bash environment, and the second is to call exec directly to execute if you want to execute multiple commands through && connection
EXPOSE: a port used to indicate the opening of a process within a container, separated by a space between multiple ports
When you run the container:
The port specified in the EXPOSE can be mapped to another random port on the host via the parameter-p (uppercase)
Or host can communicate with this container through the mapped port. At the same time, we can also use the-p (lowercase) parameter
Set the ports not listed in EXPOSE in Dockerfile to public
ADD : adds a file to the new image, which can be a host file, a network file, or a folder.
The first parameter of the ADD command is used to specify the source file (folder), which can be the file path, the path to the folder, or the URL address of the network file. It is important to note that if it is a file path or folder path, it must be relative to the directory where the relative dockerfile is located. If it is a file URL, it is downloaded and then added to the image when the image is created. The second parameter is where the file needs to be placed in the destination image. If the source file is a compressed file in the form of a zip or tar on the host, Docker will first unzip and then add the file to the image at the specified location. If the source file is a network compressed file specified by URL, it will not be decompressed.
VOLUME : This command creates a mount point for the specified path (file or folder) in the image, which can be from a host or other container. Multiple containers can share data through the same mount point, and even if one of the containers is stopped, the mount point is still accessible, and the mount point is automatically deleted only when the container reference for the mount point disappears.
Workdir : Specifies a new working directory for the next instruction, which can be an absolute directory or a relative directory. Workdir can be specified more than once, as needed. When a container is started, the directory that the last Workdir instruction refers to is the current working directory in which the container runs.
ENV : Sets the environment variable for the container to run. This environment variable value can be modified by the-e parameter when the container is run
CMD : used to set the command to run by default when the container is started
The CMD parameter has the same format as RUN:
CMD Ls-l-A
CMD ["LS", "-L", "-a"]
entrypoint : Similar to CMD, it is also used to specify the command to run by default when the container starts.
In general, we will use CMD and entrypoint together. The entrypoint is used to specify which commands need to be run, and CMD is used to specify the parameters required to run the command, such as:
entrypoint ["LS"]
CMD ["-A", "-l"]
USER : Specifies the user or UID for the operation of the container and the subsequent run, CMD, EntryPoint, and other instructions.
Onbuild : trigger directive. When the image is built, the image builder of Docker saves all commands specified by the onbuild instruction to the mirrored metadata, which is not executed during the current mirroring build process. Execution is triggered only when the new mirror uses the from directive to specify the parent image as the mirror.
You can disable the caching mechanism by setting the Docker build command parameter--no-cache=true
Docker Hub
The Docker hub URL is https://hub.docker.com
The mirrors on the Docker hub fall into two categories. One is the official image, such as Ubuntu, Nginx, Redis, MySQL, WordPress, etc., such images are generally by the authoritative third party (such as canonical, Oracle, Red hat and other large companies with background) for development and maintenance, and finally need doc Ker Official certification passed. The other class is for normal user mirroring.
Image of Docker