Docker-- better understand the relationship between image and containers

No public concern, we can reply, "blog Park" No background in public, freely available knowledge of Java / information interview must-see. 

One mirror is docker core assembly, the mirror base when the container is running, the container is the Mirror operation morphology. Earlier we introduced the use of containers, and today we talk about problems image.

This article is the fourth in this series, read the previous article contribute to a better understanding of the article:


1. Docker portal and mounted [Docker series -1]

2. Docker container Basic Operations [Docker series-2]

3. Docker container Advanced Operations [Docker Series -3]


Overall, since the mirror is a program containing the necessary environment and run the code read-only file, which uses a hierarchical file system, the change in each layer is increased to the original form of a read-write layer read-only file. It's a bit like onion, layer by layer, and when we later learned Dockerfile, I believe we would be more accurate to understand this architecture.

Mirror image of the container

As already introduced to the reader through the use of containers, the careful reader may have noticed that when the container start or create, you must specify a name of the mirror or id, in fact, when the mirror's role is to stencil container, different the mirror may be constructed of different containers, with a mirror, we can construct a barrier containers by configuring different parameters. The following command:

  1. docker run -itd --name nginx nginx

The last command in nginx that means creating a mirrored (template) of the container need, of course, there is also omits some information, such as version number, which we will detail later.

Mirror architecture

The bottom image is a startup file system (the bootfs) mirror, the bootfs upper mirror image is called the root, in general, the mirror is a root operating system, e.g. Ubuntu, CentOS the like, the user must mirror built on the root mirror, on the root image, the user can build a variety of other images.
As can be seen from the above description the reader, it is actually a collection of files essence mirrored layer jacket layer structure somewhat similar to Git, a bit like life onion

When writing copy mechanism mirror

By docker runcreating the mirrored command specifies a container actually create the image on a blank writable file system hierarchy, the file system hierarchy may be temporary as a mirror treated, and the command template referred mirroring the image may be referred to as the parent. Father mirrored content is read-only way to mount come in, read the contents of the container will share the parent mirror, all the changes made by the user are in the file system, it will not have any impact on the parent image. Of course, users can make modifications by some other means to the parent persistent mirror, which we will explain in detail to the back.

In short, a mirror is fixed does not change the template file, is created out of the container based on this template, the container will make some changes on the basis of the template, these changes will not affect the template itself, we can also Creating more out of a container according to the template (mirror).

If necessary, we can modify the templates (image) of.

View Mirror

Users can docker imagesview all local mirroring command, as follows:

Where a total of five parameters, meaning are as follows:

  • TAG: TAG used to distinguish between different warehouses in the mirror, the default is latest.

  • IMAGE ID: IMAGE ID is a unique identifier for the image.

  • CREATED: CREATED time of creation mirror.

  • SIZE: SIZE represents the size of the mirror.

  • REPOSITORY: warehouse name, warehouse used to store the same general type of mirror. The name of the warehouse specified by its creator. If you do not specify compared  <none> . In general, there are different warehouse name the following forms:

  1. [namespace\ubuntu]: This warehouse by the name of the namespace name and the actual composition of the warehouse, by the middle  \ separated. When a developer creates a user on Docker Hub, the user name is the default namespace, this command space is used to distinguish between different users or organizations registered on Docker Hub (similar to the role the user name on GitHub), if readers want upload your own image to Docker Hub altar used by others, you must specify the namespace, or otherwise upload will fail.

  2. [ubuntu]: This is only the name of the warehouse, the warehouse for which no namespace name, you can think it belongs to the top-level namespace, the warehouse space used only for official mirrors, managed by Docker official, but generally will be licensed to third parties development and maintenance. Of course, user-created image can also use this naming scheme, but will not be uploaded to the Docker Hub share.

  3. [hub.c.163.com/library/nginx]: This specifies the url path way mirror for general non-Docker Hub name, such as a mirror or a third-party developers to build their own service provider of image center, you can use this naming names.

Use docker imagescommand to see all the local mirror if the mirror too much, can be matched by a wildcard, as follows:

To view the details of the image, the above-mentioned can also docker inspectview command.

Mirror Download

When the user executes docker runa command, it will automatically download the image to the Docker Hub, this will not repeat the demo, the developer can go to meet the requirements of image search on Docker Hub through the search command, as follows:

among them:

  • NAME: represents the name of the mirror.

  • DESCRIPTION: schematic description of the mirror.

  • STARS: represents the user to mirror the score, the higher the score the more safe to use.

  • OFFICIAL: whether the official mirror.

  • AUTOMATED: whether to use an automated build.

In the execution docker runto go download command, the speed a bit slow, if you want to quickly execute this command can be executed before the first use of docker pullthe command to download the image, and then run.

Run the following command:

Delete Mirror

Mirroring can docker rmibe deleted command, parameter is mirrored id or image name, parameter can have multiple, multiple parameters separated by spaces. as follows:

Sometimes, you can not delete an image, most of the reason is because the mirror was dependent on a container, then you need to delete the container, then you can delete the image, delete container commands can refer to the previous article in this series.

By reading the preceding article, the reader has learned that the so-called container actually creates a read-write file hierarchy based on parent mirrored on, all the changes are made on the file level, and not by the parent Mirror the impact, if readers need to be modified according to this to create a new local mirror, there are two different ways, first look at the first way: commit.

Create a container

First, a container according to the local Mirror operation, as follows:

 

Command interpreter:

  1. First execution docker images command to view a local mirror.

  2. According to local mirror of nginx mirror, create a container named nginx, and started.

  3. Copying the documents in a host into a container called index.html.

  4. Access to the container and found that change has become effective.

  5. Then re-create a container named nginx2.

  6. Access nginx2, found nginx2 the default page or nginx default page, has not changed.

commint create a local mirror

Next, according to the first container just created, create a local mirror, as follows:

 

Command interpreter:

  1. -M parameter is a brief description of the image created.

  2. --author representation of the image.

  3. ce1fe32739402 means creating a mirror is based container id.

  4. sang / nginx said the warehouse name, sang the name space, nginx is the image name.

  5. v1 represents the tag warehouse.

  6. Once created, by docker images command you can view the image you just created.

  7. By mirroring running just created a container, access to the container and found nginx default home page has changed.

This is the way we create by commint local mirror of the way, but there are some problems commit ways, such as lack of transparency, can not be repeated, larger, in order to solve these problems, consider using Dockerfile, in fact, the mainstream program is Dockerfile.

Dockerfile

Dockerfile is a normal text file, which contains the instructions within a section, each instruction will build layer. First look at a simple example.

 

First, create a file named Dockerfile in an empty directory, as follows:

Command interpreter:

  1. Shows the construction of the FROM nginx the mirror to mirror the existing nginx constructed based on the basis of the mirror.

  2. MAINTAINER instruction is used to declare creation of information and mailbox information mirrored, this command is not required.

  3. RUN command is used to modify the mirror, be regarded as a more frequent use of the instruction, the instruction can be used to install the program, install and configure applications such as libraries, a RUN command execution will create a new image layer on the basis of the current mirror, the next instruction will execute on this new image layer, RUN statements in two different forms: shell format and exec format. In this case the format used shell, shell command the same format as linux, exec format is a JSON array, the array can be placed in the command. When using the RUN command, when appropriate may be combined into a plurality RUN command, to avoid creating too many layers when creating the image.

  4. COPY statement sucked mirror context  hello.html files are copied to the mirror.

 

After the file is created, execute the following command to build:

Command interpreter:

  1. -t parameter is used to specify the namespace mirror, warehouse TAG name and other information.

  2. Rearmost  . refers mirror building context.

Note :

Docker using the C / S architecture, into Docker client (Docker executable) and Docker daemon, Docker client API in the form of a command line and the communication daemon Docker, Docker Docker provides services daemon. Thus, various docker command our operations are actually sent by the client to the docker docker daemon up execution. When we construct a mirror inevitably be necessary to copy a local file to the mirror, such as COPY command mentioned above, the user when constructing the mirror, the mirror needs to specify the path building context (i.e., foregoing .), docker build after obtaining this path, all the contents of the package will be the path, and then uploaded to the Docker engine.

 

After building the mirror, you can view by docker images command, as follows:

Then create and launch container, you can see the content before the entry into force.

to sum up

This paper to introduce the basic operations Docker in the mirror, the operation is not difficult, the key is to understand the relationship between a good image and containers, as well as mirroring the onion-like file structure.

Java technology geeks public number, is founded by a group of people who love Java technology development, focused on sharing of original, high-quality Java articles. If you feel that our article is not bad, please help appreciated, watching, forwarding support, encourage us to share a better article.

 

Guess you like

Origin www.cnblogs.com/justdojava/p/11271246.html