Docker Introduction Notes

Recently, I want to use Docker, and I found a few entry-level blogs in the evening, summarizing and practicing.

Basic concepts

The problem of environment configuration

One of the biggest hassles in software development is environment configuration. The environments of users’ computers are different. How do you know which machines your software will run on?

Users must ensure two things: the settings of the operating system, and the installation of various libraries and components. Only if they are correct can the software run. For example, to install a Python application, the computer must have a Python engine, various dependencies, and possibly configure environment variables.

If some old modules are not compatible with the current environment, it will be troublesome. Developers often say “It works on my machine”, which means that other machines are likely not to run.

Configuring the environment was so troublesome that it would take time to do it all over again when changing a machine. Many people thought, can the problem be fundamentally solved, and the software can be installed with the environment? That is to say, when installing, copy the original environment exactly.

Virtual machine

Virtual machine (virtual machine) is a solution with environment installation. It can run another operating system within one operating system, such as Windows system running Linux system. The application has no awareness of this, because the virtual machine looks exactly like the real system, while for the underlying system, the virtual machine is just an ordinary file that can be deleted without any effect on other parts.

Although users can restore the original environment of the software through virtual machines. However, this solution has several drawbacks.

** (1) Too much resource occupation **

The virtual machine monopolizes a portion of memory and hard disk space. When it runs, other programs cannot use these resources. Even if the applications in the virtual machine actually use only 1MB of memory, the virtual machine still needs several hundred MB of memory to run.

** (2) Multiple redundant steps **

Virtual machines are complete operating systems, and some system-level operation steps often cannot be skipped, such as user login.

** (3) Slow startup **

As long as it takes to start the operating system, it takes to start the virtual machine. It may take several minutes for the application to actually run.

Linux

Because of these shortcomings, Linux developed another virtualization technology: Linux Containers (LXC).

** Linux container does not simulate a complete operating system, but isolates processes. ** In other words, it sets a保护层For the process inside the container, the various resources it touches are virtual, thus achieving isolation from the underlying system.

Since containers are process-level, they have many advantages over virtual machines.

** (1) Start fast **

The application inside the container is directly a process of the underlying system, not a process inside the virtual machine. Therefore, starting the container is equivalent to starting a process on the local machine, instead of starting an operating system, which is much faster.

** (2) Low resource usage **

Containers only occupy the resources they need, not those that are not used; Since virtual machines are complete operating systems, they inevitably occupy all resources. In addition, multiple containers can share resources, and virtual machines are exclusive resources.

** (3) Small size **

The container only needs to contain the components used, and the virtual machine is the packaging of the entire operating system, so the container file is much smaller than the virtual machine file.

In short, containers are a bit like lightweight virtual machines that can provide a virtualized environment, but at a much lower cost.

Docker

** Docker is an encapsulation of Linux containers, providing a simple and easy-to-use container interface. ** It is currently the most popular Linux container solution.

Docker packages the application and its dependencies in a file. Running this file will generate a virtual container. The program runs in this virtual container as if it were running on a real physical machine. With Docker, there is no need to worry about environmental issues.

Overall, Docker’s interface is quite simple. Users can easily create and use containers and put their own applications into containers. Containers can also be version managed, copied, shared, and modified, just like managing ordinary code.

Docker

There are currently three main categories of Docker’s main uses.

** (1) Provide a one-time environment. ** For example, provide Unit Test and build environments when testing other people’s software locally and continuous integration.

** (2) Provide elastic Cloud as a Service. ** Because Docker containers can be turned on and off, they are very suitable for dynamic scaling and shrinking.

** (3) Set up a micro-service structure. ** Through multiple containers, a machine can run multiple services, so the micro-service structure can be simulated locally.

image

** Docker packages the application and its dependencies in an image file. ** Only through this file can a Docker container be generated. The image file can be seen as a template for the container. Docker generates container instances from the image file. The same image file can generate multiple container instances running simultaneously.

Image is a binary file. In actual development, an image file is often generated by inheriting another image file and adding some personalized settings. For example, you can add an Apache server to it on the basis of Ubuntu image to form your image.

1
2
3
4
5
#
$

#
$

Image files are universal, and the image files from one machine are copied to another machine and can still be used. Generally speaking, in order to save time, we should try to use image files made by others instead of making them ourselves. Even if you want to customize, you should process based on other people’s image files instead of making them from scratch.

In order to facilitate sharing, after the image file is created, it can be uploaded to the online repository. Docker’s official repository Docker Hub It is the most important and commonly used image repository. In addition, it is possible to sell image files made by yourself.

Example: hello

Below we go through the simplest image file "hello world"Get a feel for Docker.

It should be noted that the domestic official repository connected to Docker is very slow and will be disconnected. You need to change the default repository to the domestic mirroring website. The specific modification method is in下一篇文章The first section of. Friends in need, you can take a look first.

First, run the following command to grab the image file from the repository to local.

1
$

In the above code, ‘docker image pull’ is the command to grab the image file. ‘library/hello-world’ is the location of the image file in the repository, where’library ‘is the group where the image file is located, and’hello-world’ is the name of the image file.

Due to the image files officially provided by Docker, they are all placed inlibraryGroup, so it’s the default group and can be omitted. Therefore, the above command can be written like this.

1
$

After the capture is successful, you can see the image file locally.

1
$

Now run this image file.

1
$

The docker container run command generates a running container instance from the image file.

Note that the’docker container run ‘command has the function of automatically crawling image files. If no image file is found locally, it will be automatically crawled from the repository. Therefore, the previous’docker image pull’ command is not a necessary step.

If it runs successfully, you will read the following output on the screen.

1
2
3
4
5
6
$

Hello
This

...

After outputting this prompt, ‘hello world’ will stop running and the container will automatically terminate.

Some containers do not automatically terminate because they provide services. For example, installing and running Ubuntu image can experience the Ubuntu system from the command line.

1
$

For those containers that do not automatically terminate, you must usedocker container kill Command to terminate manually.

1
$

Container file

** The container instance generated by the image file is itself a file, called the container file. ** That is to say, once the container is generated, there will be two files at the same time: the image file and the container file. And closing the container does not delete the container file, it just stops running.

1
2
3
4
5
#
$

#
$

The output of the above command includes the ID of the container. This ID needs to be provided in many places, such as the’docker container kill 'command to terminate the container operation in the previous section.

The terminated container file will still occupy hard disk space and can be useddocker container rmCommand to delete.

1
$

After running the above command, using the command’docker container ls --all ', you will find that the deleted container file has disappeared.

Dockerfile

After learning to use image files, the next question is, how can you generate image files? If you want to promote your own software, it is necessary to make your own image files.

This requires the use of the Dockerfile. It is a text file used to configure image. Docker generates a binary image file from this file.

Practice

Building web services

First, we need to pull centos mirroring.

1
2
docker run -p 80 --name web -i -t centos /bin/bash
Copy the code

Next, we install the nginx server and execute the following command:

1
2
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
Copy the code

After installing the Nginx source, you can officially install Nginx.

1
2
yum install -y nginx
Copy the code

At this point, we can enter the command’whereis nginx 'to see the installation path. Finally, we also need to run Nginx.

1
2
nginx
Copy the code

Now, we do’ctrl + P + Q ‘to switch to the background. Then, use’docker ps -a’ to see the randomly assigned ports.

Then access http://127.0.0.1: Random Port Number through the browser.

Build mirroring

There are two ways to build mirroring in Docker. One way is to use the’docker commit ‘command, and the other way is to use the’docker build’ command and the’Dockerfile ‘file. Among them, it is not recommended to use the’docker commit’ command for building because it does not standardize the whole process. Therefore, it is more recommended to use the’docker build ‘command and the’Dockerfile’ file to build our mirroring in enterprises. We use the’Dockerfile 'file to make building mirroring more repeatable, while ensuring standardization of startup scripts and running programs.

Build the first

Now, let’s move on to the actual combat. Here, we build a mirroring from the web server we set up at the beginning. First, we need to create an empty Dokcerfile file.

1
2
3
4
5
mkdir dockerfile_test
cd dockerfile_test/
touch Dockerfile
nano Dockerfile
Copy the code

Next, we need to write a Dockerfile with the following code listing

1
2
3
4
5
6
FROM centos:7
MAINTAINER LiangGzone "[email protected]"
RUN rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
RUN yum install -y nginx
EXPOSE 80
Copy the code

Finally, we build with the’docker build 'command.

1
2
docker build -t="lianggzone/nginx_demo:v1" .
Copy the code

We can take a look at our new mirroring with docker images.

Now, let’s understand the whole process. First, ‘FROM centos: 7’ is the first step that Dockerfile must take. It will run a container from an existing mirroring. In other words, Docker needs to rely on a base mirroring for building. Here, we specify centos as the base mirroring, and its version is 7 (CentOS 7). Then, we specify the author of the mirroring as LiangGzone and the mailbox as [email protected] through’MAINTAINER LiangGzone “[email protected]”. This helps to tell the user its author and contact information. Next, we execute two RUN commands to download and install Nginx, and finally expose port 80 of the Dokcer container through’EXPOSE 80 '. Note that the execution order of Docker is executed from top to bottom, so we need to clarify the execution order of the entire process. In addition, Docker creates a new mirroring layer and commits after each instruction.

We use the’docker build ‘command to build, specifying’ -t ‘to tell Docker the name and version of mirroring. Note that if no tag is specified, Docker will automatically set a lastest tag for mirroring. Another point, we have a final’. 'to let Docker go to the current local directory to find the Dockerfile file. Note that Docker commits the result as mirroring at each build step, and then treats the previous mirroring layer as a cache, so when we rebuild a similar mirroring layer, we will directly reuse the previous mirroring. If we need to skip, we can use the ‘–no-cache’ option to tell Docker not to cache.

Command discrimination 1: RUN, CMD, ENTRYPOINT

The purposes of the three instructions’RUN ‘,’ CMD ‘, and’ENTRYPOINT’ are very familiar. The difference is that the’RUN ‘instruction is a command to run when the container is built, while’CMD’ and’ENTRYPOINT ‘execute shell commands when the container is started, and’RUN’ will be overwritten by the’docker run ‘command, but’ENTRYPOINT’ will not be overwritten. In fact, any arguments specified by the’docker run ‘command will be passed to the’ENTRYPOINT’ instruction again as arguments. The’CMD ‘and’ENTRYPOINT’ directives can also be used together. For example, we can use the exec form of’ENTRYPOINT ‘to set fixed default commands and parameters, and then use either form of’CMD’ to set other default values that may change.

1
2
3
4
FROM ubuntu
ENTRYPOINT ["top", "-b"]
CMD ["-c"]
Copy the code

Command discrimination 2: ADD, COPY

The’ADD ‘and’COPY’ directives are used in the same way, the only difference is that’ADD ‘supports extracting and decompressing archive files (tar, gzip, bzip2, etc.). Note that the directory that the’COPY’ directive needs to copy must be placed in the same directory as the Dockerfile file.

More docker commands

https://docs.docker.com/engine/reference/commandline/docker/

Reference link:

https://juejin.im/post/5cacbfd7e51d456e8833390c

https://www.ruanyifeng.com/blog/2018/02/docker-tutorial.html