There are five objects in the figure: image, container, image warehouse, backup file, Dockerfile, and there are a number of commands marked in blue, which are used to convert various objects to each other and generate them. The following one is introduced:

The mirror

You can easily understand that an image is a compressed package that contains your programs or code and a file system. A file system is a package that contains all the necessary files, executables, runtime libraries, and so on for a standard operating system.

Docker solves the problem of application execution environment consistency by packaging the application and file system together. When the application runs, it loads dependencies from the image file system that you have packaged. For example, if your application relies on JDK1.8, then package the image with the base library of JDK1.8. Then, regardless of the environment on which the image is deployed, regardless of whether or not the JDK is installed on that host, or any version of the JDK is installed, your application will be able to load JDK1.8 in the image consistently when it starts, avoiding deployment problems and runtime bugs caused by different environments.

An image can be identified by its ID or name. ID is a HASH value in SHA256 format, for example:

b1183dab1c4049b9b9d0d0dff17d2eb04e8d9caf873f7ff505ff9fe8909e2a48
CopyCopy the code

You can abbreviate it b1183DAB1C40

The image name consists of the image name and Tag, with colons in the middle. If Tag is omitted, it indicates latest, for example:

# Docker official image, omitted Tag
ubuntu

# Docker official image, specify TagUbuntu: 16.04Docker Hub is not an official Docker imageMaichong/ubuntu 16.04# mirror in pulse cloud warehouse, with mirror warehouse addressMaichong. IO/project/ubuntu: 16.04 the CopyCopy the code

You can use the docker tag command to attach a new tag to an existing image, such as:

# for ubuntu: 16.04 to add a new label maichong. IO/project/ubuntu: 16.04Docker tag ubuntu: 16.04 maichong. IO/project/ubuntu: 16.04 the CopyCopy the code

View a list of all mirrors in the current system:

> docker images
REPOSITORY        TAG            IMAGE ID        CREATED            SIZE
ubuntu            16.04        e13f3d529b1a    6 days ago        115MB
CopyCopy the code

The container

It is easy to understand that a mirror is “dead”, a “static” file stored on disk, while a container is a running, in-memory, “dynamic” instance. Of course, containers don’t have to be running; containers can stop.

Docker run command can start a container from an image, and an image can start infinite container copies, each container is isolated from each other, each container can be understood as a virtual machine, each container has its own IP, file system and so on.

Container compared with those of the virtual machine runs though network, files and other resources are isolated, but Shared hosting the kernel, namely, kernel Docker didn’t start from scratch for each container, but in the current host system kernel to run programs in a container, the relative to the VM virtual machine has a very big performance advantages, so called the Docker lightweight virtualization.

You can use docker start/docker stop/docker restart to control the start, stop, and restart of containers.

You can run the Docker commit command to make a new version of the image of the running container, but Dockerfile is recommended.

Dockerfile

In short, a Dockerfile is a configuration file that generates an image, for example:

# specify tomcat versionThe FROM tomcat: 8.5.32 - jre8# specify the working directory
WORKDIR /app

Copy the packaged server.jar to the image
Source code can be packaged into JARS online using pulse Cloud's compile build service
ADD server.jar /app/server.jar

# set the startup command of the mirror
CMD java -jar /app/server.jar

Declare the port to listen on
EXPOSE 8080
CopyCopy the code

This file first declares the base image of the image. In general, the image you build depends on a base image, just as you install software on a computer that already has an operating system.

The Dockerfile then records the steps of creating a new image, including copying files and executing commands.

The Dockerfile also contains declarations of other information, such as environment variables, ports that need to be opened, and so on.

Using the Docker build command, you can follow the steps recorded in the Dockerfile to generate a new image step by step.

Executing commands, installing software, and then running Docker Commit in a running container can also generate a new image, but don’t do this. Because, using Dockerfile can record the image generation process, and can adjust the steps at any time, to regenerate the image. This is the fabled infrastructure code, the configuration of the underlying environment as if it were software programming.

Backup file

You can use docker save command, export the image generated on your computer into tar file, and then used for data backup, or copy the file to other computers, import the image with docker load command, realize image distribution.

However, this can be cumbersome, especially for remote transmission. Use the mirror warehouse for unified management and distribution.

Mirror warehouse

A mirror warehouse is a service that provides image storage online. Use Docker pull to pull (download) the image from the image repository, and use docker push to push (upload) the local image to the image repository.

Usually, all the images we use are from the official Docker warehouse, hub.docker.com. For enterprise users, Pulse Cloud also provides enterprise private image warehouse. You must have the permission to specify the project. After logging in via docker login maichong. IO, the image can be pushed and pulled into the pulse cloud.

Finally, as a general developer, pulse cloud is not required to remember all the Docker concepts and commands, just need to be able to write Dockerfile configuration application environment, we continue to explain how to write Dockerfile in the next section.