Docker practical experience (1) Introduction, installation and practical operation

insert image description here

Write an article three times, remove impurities, and refine dry goods. This is a series of blogs with temperature.

What pain points does Docker solve

The environment configuration is quite troublesome. If you change a machine, you have to do it all over again, which is laborious and time-consuming. Many people think, can the problem be solved fundamentally, and the software can be installed with the environment? That is to say, when installing, copy the original environment exactly the same. Developers can take advantage of Docker to eliminate the "just works on my machine" problem of collaborative coding.

In fact, this technology has long been realized, and it is called snapshot.

But a snapshot is big because it needs to package everything in the current environment, whether you need it or not.

The reason why Docker is growing so fast is because it provides a standardized solution for this-----Smooth system migration, container virtualization technology

Docker VS VMware

insert image description here

Container Virtualization Technology

Unlike virtual machines, containers do not need to be bundled with a complete operating system, only the library resources and settings needed for the software to work. The system thus becomes efficient and lightweight and guarantees that software deployed in any environment will run consistently.

The traditional virtual machine technology is to virtualize a set of hardware, run a complete operating system on it, and then run the required application process on the system; the application process in the container runs directly on the host's kernel, and the container does not have its own kernel. And there is no hardware virtualization. Therefore, containers are lighter than traditional virtual machines.

Each container is isolated from each other, and each container has its own file system. Processes between containers will not affect each other and can distinguish computing resources.

The underlying principle that containers are faster than virtual machines

1. Docker has fewer abstraction layers than virtual machines.
2. Docker uses the kernel of the host machine without loading the OS kernel.

Docker installation

Here are two Docker official websites for everyone:
Docker official website:
Docker warehouse:

The specific installation method is available on the official website, and I installed it under Linux/Ubuntu, so I will explain how to install Docker under Ubuntu.

knock on the blackboard: Do not install with the snap method recommended by the Ubuntu system! ! ! Who is who knows.

$ sudo apt-get update
$ sudo dpkg --configure -a
$ sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
3.添加Docker官方GPG key
$ curl -fsSL | sudo apt-key add -
$ sudo add-apt-repository "deb [arch=amd64] $(lsb_release -cs) stable"
$ sudo apt-get update
6.安装最新版本Docker CE(社区版)
$ sudo apt-get install docker-ce
$ docker --version
检查Docker CE 是否安装正确
$ sudo docker run hello-world

Docker basic composition

  • Mirror image: You should be familiar with the mirror image. A Docker image is a read-only template. Images can be used to create Docker containers, and one image can create many containers.
  • Container: Docker uses a container (Container) to run an application or a group of applications independently. An application or service runs in a container. A container is similar to a virtualized running environment. A container is a running instance created with an image. Just like classes and instance objects, images are static definitions, and containers are entities when images are run. Containers provide a standard and isolated runtime environment for images, which can be started, started, stopped, and deleted. Each container is an isolated, secure platform.
  • Warehouse: A place where image files are stored centrally. Repositories are divided into two forms: public repositories (Public) and private repositories (Private). The largest public repository is Docker Hub.

Docker common commands

Set boot up: systemctl enable docker

It doesn't matter if you don't turn off all the year round like me.

View docker status: systemctl status docker

List all local images: docker images

insert image description here


The same repository source can have multiple TAG versions, representing different versions of the repository source. We use REPOSITORY:TAG to define different images.
If you don't specify a version tag for an image, e.g. you only use ubuntu, docker will default to the ubuntu:latest image.

Download image: docker pull image name

If there is no special requirement for the version number, manufacturer and other data, just
docker pull the image name directly.

If you have special needs, you can search on dockerhub without logging in.
There will be follow-up instructions on dockerhub.

insert image description here

Delete image: docker rmi a XXX image name/ID

It is recommended to use the docker rmi image name: [tag]
without -f, it is to give us a chance to regret, in case that image is in use.
If you use the name instead of the id, there may be multiple mirrors sharing an ID, which will be seen later.
The reason for adding a tag is because there may be multiple mirrors with one name, as you have seen earlier.

Modify the image name: docker tag Old image name: old version number New image name: new version number

Let's say this:

[email protected]:~# docker tag ubuntu:15.10 runoob/ubuntu:v3
[email protected]:~# docker images   runoob/ubuntu:v3
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
runoob/ubuntu       v3                  4e3b13c8a266        3 months ago        136.3 MB

Typically used to prefix images. No one really uses this to change the version number or name of the mirror image. Isn't that cheating?
In this way, the IDs of the two mirrors in the past are the same, and there is a link relationship between the two.

Run the image: docker run [-itd] Image name: [tag] [bash]

If it's the only version, it's up to you with or without a version number.

This is the standard format to prevent some gibberish.

When the image runs, a container is generated. It can be said that the container is the process carrier of the image running.

The -it option is to make the container run explicitly and persistently.
The -d option is to let the container play by itself in the background.
bash is that since the container has been put on the surface, we must give people a workbench. It can also be sh, depending on personal preference.

Of course, there are other parameters. -h See for yourself.

View container: docker ps -a

Add a -a to view all containers, including deceased ones. Because sometimes you will think that you have started the container but you don't see it, don't doubt yourself, it is started, but it died for various reasons.
There are two more reasons:

1. The container that is not suitable for running in the background does not have -it, which causes it to feel that it has nothing to do, so it gets off work.
2. Some service configuration files that depend on configuration are wrongly written, such as our custom mysql.

There is also a lack of resources, but this possibility is not high.

Cut into the container: docker exec -it running container ID bash

If you started the container with -d, but at this time you want to explicitly cut into the container and operate. It's still like our mysql. Now that the container is up, we have to go in and start mysql, right? We have to build a database, build a table, and operate and maintain it.

At this time, the exec method is used. The container ID is recommended here. Of course, the container name can also be used. Yes, you can name the container.

Exit without closing the container: Ctrl + p + q

Remember this operation, remember, only this operation.
I won't tell you how to close the container, I'm afraid you won't have a place to cry if you misuse it.

If you're sure you don't want a container anymore, delete it explicitly.

Delete a container: docker rm container ID

Don't add -f here either, giving you a chance to regret it.

dangling mirror

This can be used to brag with the interviewer, you even know the floating mirror.

What is a dangling mirror? Images have no repository name or no tags.
What are the consequences of such mirroring? There are no consequences, it's just a little tasteless, and the corpse is a vegetarian meal, which is not pleasing to the eye.

Remove all dangling images:

​docker rmi $(docker images -q -f dangling=true)​​

If one is not enough, delete it several times. Sometimes this command is a bit problematic.

How, I think my conscience and warmth are everywhere in this article.

Guess you like