Docker
preface
Why Docker? Before container technology, Internet celebrities were virtual machines (VMS), which were represented by VMWare and OpenStack. I believe many of you have used virtual machines. Virtual machine is in your operating system, install a software, and then through this software to simulate a sub-computer or even more. In the sub computer, you can run programs like a normal computer, such as opening QQ. If you like, you can make several “sub computers”, which are all open QQ. Between the “sub-computer” and the sub-computer, is mutually isolated, do not affect each other. VMS are virtualization technologies. Container technology like Docker is also a virtualization technology, belonging to lightweight virtualization. Virtual machines can isolate many children, but they take up more space, start up slower, and can cost money for virtual machine software (VMWare, for example). Container technology has none of these disadvantages. It doesn’t need to virtualize the entire operating system, just a small scale environment (like a sandbox).
It starts up quickly and can be done in seconds. Moreover, it is highly resource pair efficient (thousands of Docker containers can run simultaneously on a single host). In addition, it takes up a small amount of space. Virtual machines typically require several to tens of gigabytes of space, while containers require only megabytes or even kilobytes. Because of this, container technology has received a warm welcome and pursuit, rapid development.
Docker profile
Docker is an open source application container engine based on the Go language and open source under the Apache 2.0 protocol. Docker allows developers to package their applications and dependencies into a lightweight, portable container that can then be distributed to any popular Linux, as well as virtualization. Containers are completely sandboxed, with no interface to each other.) More importantly, container performance overhead is extremely low. Docker itself is not a container. It is a tool for creating containers. It is an application container engine.
Docker core concepts
Docker consists of three basic concepts:
Image: Docker Image, which is equivalent to a root file system. For example, the official ubuntu:16.04 image includes a complete set of root files for ubuntu 16.04’s minimum system. Container: The relationship between an Image and a Container is similar to that between a class and an instance in object-oriented programming. An Image is a static definition and a Container is an entity of the Image runtime. Containers can be created, started, stopped, deleted, paused, and so on. Repository: A Repository can be thought of as a code control center that holds images.
Docker use
Docker allows you to run an application container inside a container, using the Docker run command to run an application inside a container. Hello world output:
runoob@runoob:~$ docker run ubuntu:15.10 /bin/echo "Hello world"
Hello world
Copy the code
Analysis of each parameter:
Docker: binary execution file of docker. Run: Combined with the previous Docker to run a container. Ubuntu :15.10 Specifies the image to run. Docker first looks up the image from the localhost to see if it exists. If not, Docker downloads the public image from the image repository Docker Hub. /bin/echo "Hello world": the command is executed in the started containerCopy the code
Docker creates a new container using the Ubuntu15.10 image, executes bin/echo “Hello world” in the container, and prints the result.
Run interactive containers
Docker can be used to communicate with the docker container by using the following two parameters:
runoob@runoob:~$ docker run -i -t ubuntu:15.10 /bin/bash
root@0123ce188bd8:/#
Copy the code
-t: specifies a dummy terminal or terminal in the new container. -I: allows you to interact with standard input (STDIN) inside the container.Copy the code
Note the second line root@0123ce188bd8:/#, at which point we have entered a container for the ubuntu15.10 system
We try to run the cat /proc/version and ls commands in the container to view the version information of the current system and the list of files in the current directory, respectively
root@0123ce188bd8:/# cat /proc/version
Linux version 4.4.0-151-generic (buildd@lgw01-amd64-043) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.10) ) #178-Ubuntu SMP Tue Jun 11 08:30:22 UTC 2019
root@0123ce188bd8:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@0123ce188bd8:/#
Copy the code
We can exit the container by running the exit command or using CTRL+D.
root@0123ce188bd8:/# exit
exit
root@runoob:~#
Copy the code
Note that root@runoob:~# in the third line indicates that we have exited the current container and returned to the current host.
Start the container (background mode)
Use the following command to create a container that runs as a process
runoob@runoob:~$docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done" 2b1b7a428627c51ab8810d541d759f072b4fc75487eed05812646b8534a2fe63Copy the code
In the output, instead of the expected “Hello World”, we see a long string of characters
2b1b7a428627c51ab8810d541d759f072b4fc75487eed05812646b8534a2fe63
This long string is called the container ID. It is unique to each container, and we can use the container ID to see what happened to the corresponding container.
First, we need to confirm that the container is running, which can be checked with docker PS:
runoob@runoob:~$ docker ps CONTAINER ID IMAGE COMMAND ... 5917eAC21C36 Ubuntu :15.10 "/bin/sh -c 'while t..." .Copy the code
Output details:
CONTAINER ID: indicates the ID of a CONTAINER. IMAGE: IMAGE used. COMMAND: The COMMAND that is run when the container is started. CREATED: time when the container was CREATED. STATUS: indicates the STATUS of the container.Copy the code
There are seven states:
Created restarting running or Up Removing removing paused exited dead PORTS: The port information of the container and the connection type used (TCP \ UDP). NAMES: automatically assigned container NAMES.Copy the code
Use the docker logs command within the host host to view the standard output within the container:
runoob@runoob:~$ docker logs 2b1b7a428627
runoob@runoob:~$ docker logs amazing_cori
Copy the code
Stop the container
We use the docker stop command to stop the container:
Docker ps shows that the container has stopped working:
runoob@runoob:~$ docker logs 2b1b7a428627
runoob@runoob:~$ docker logs amazing_cori
Copy the code
You can see that the container is no longer there.
It can also be stopped with the following command:
runoob@runoob:~$ docker stop amazing_cori
Copy the code