What is a Docker

Docker was originally an internal project initiated by dotCloud’s founder in France. It is an innovation based on dotCloud’s cloud service technology for many years, and it is maintained on the main project code. The Docker project later joined the Linux Foundation and was founded to promote it.

Docker has attracted extensive attention and discussion since it was opened to the public. So far, Docker has more than 57,000 stars and more than 10,000 forks. Even due to the popularity of the Docker project, at the end of 2013, Docker was originally developed and implemented on Ubuntu 12.04; Red Hat has supported Docker since RHEL 6.5. Google also uses Docker extensively in its PaaS products.

Why Docker

As an emerging virtualization method, Docker has many advantages over traditional virtualization methods.

More efficient use of system resources

Docker has a higher utilization of system resources because the container does not require additional overhead such as hardware virtualization and running a full operating system. Whether it is application execution speed, memory consumption or file storage speed, it is more efficient than traditional VIRTUAL machine technology. Therefore, a host with the same configuration can often run more applications than virtual machine technology.

Faster startup time

Traditional VIRTUAL machine technology usually takes several minutes to start application services, while Docker container applications can be started in seconds or even milliseconds because they run directly in the host kernel and do not need to start the complete operating system. Greatly saving the development, testing, deployment time.

Consistent operating environment

A common problem in development is environmental consistency. Because the development environment, test environment, and production environment are inconsistent, some bugs are not found in the development process. The image of Docker provides a complete runtime environment in addition to the kernel, ensuring the consistency of the application running environment, so that there will no longer be “no problem with this code on my machine”.

Continuous delivery and deployment

The most desirable thing for development and operations () people is a single creation or configuration that can run anywhere.

With Docker, continuous integration, continuous delivery and deployment can be achieved by customizing application images. Developers can build the image and integrate it with the system for integration testing, while operations can quickly deploy the image directly into production or even automate it with the system.

By making the image build transparent, not only the development team can understand the application environment, but also the operations team can understand the conditions required for the application to run, helping to better deploy the image in a production environment.

Easier migration

Docker ensures the consistency of execution environment, making application migration easier. Docker can run on many platforms, whether physical machine, virtual machine, public cloud, private cloud, or even laptop, and its running results are consistent. Therefore, users can easily migrate an application running on one platform to another without worrying that the application will not run properly due to the change of the operating environment.

Easier maintenance and extension

Docker uses layered storage and image technology, which makes it easier to reuse the repeated parts of the application, easier to maintain and update the application, and it is also very simple to further expand the image based on the basic image. In addition, Docker team and various open source project teams maintain a large number of high-quality, which can be used directly in the production environment and can be further customized as a basis, greatly reducing the cost of image production of application services.

Docker one-click install

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
Copy the code

Docker command combat

Common commands

Based on the actual combat

1, the mirror

Download the latest image
root@hello:~# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
7d63c13d9b9b: Pull complete 
5cb019b641b5: Pull complete 
d477de77abf8: Pull complete 
c60e7d4c1c30: Pull complete 
365a49996569: Pull complete 
039c6e901970: Pull complete 
Digest: sha256:168a6a2be5c65d4aafa7a78ca98ff8b110fe44c6ca41e7ccb4314ed481e32288
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
Copy the code
Copy the code
Viewing a Local Mirror
root@hello:~# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx       latest   e9ce56a96f8e   8 hours ago   141MB
root@hello:~
Copy the code
Remove the mirror
root@hello:~# docker images

REPOSITORY  TAG    IMAGE ID    CREATED    SIZE

nginx     latest   e9ce56a96f8e  8 hours ago  141MB

root@hello:~# 

root@hello:~# docker rmi e9ce56a96f8e

Untagged: nginx:latest

Untagged: nginx@sha256:168a6a2be5c65d4aafa7a78ca98ff8b110fe44c6ca41e7ccb4314ed481e32288

Deleted: sha256:e9ce56a96f8e0e9f75051f258a595d1257bd6bb91913b79455ea77e67e686c5c

Deleted: sha256:6e5a463ea9608e4712465e1c575b2932dde96f99fa2c2fc31a5bacbe69c725cb

Deleted: sha256:a12cc243b903b34c8137e57160d206d6c1ee76a1ab6011a1cebdceb8b6ff8768

Deleted: sha256:a562e4589c72b0706526e13eed9c4f037ab5d1f50eb4529b38670abe353248f2

Deleted: sha256:fd67efaafabe1a0b146e9f7d958de79ec8fcec9aa6ee13ca3052b4acd8a3b81a

Deleted: sha256:c3967df88e47f739c3048492985aafaafecd5806de6c6870cbd76997fc0c68b0

Deleted: sha256:e8b689711f21f9301c40bf2131ce1a1905c3aa09def1de5ec43cf0adf652576e

root@hello:~# 

root@hello:~# docker images

REPOSITORY  TAG    IMAGE ID  CREATED  SIZE

root@hello:~#
Copy the code
Copy the code
Download the specified version image
root@hello:~# docker pull nginx:1.20.1 1.20.1: Pulling from library/nginx b380bbd43752: pull complete 83acae5e2daa: Pull complete 33715b419f9b: Pull complete eb08b4d557d8: Pull complete 74d5bdecd955: Pull complete 0820d7f25141: Pull complete Digest: sha256:a98c2360dcfe44e9987ed09d59421bb654cb6c4abe50a92ec9c912f252461483 Status: Downloaded newer image for nginx: 1.20.1 docker. IO/library/nginx: 1.20.1 root @ hello: ~ # docker images REPOSITORY TAG image ID ID CREATED SIZE nginx 1.20.1c8d03f6b8b91 5 weeks ago 133MB root@hello:~#Copy the code
Copy the code

2, container

docker run [OPTIONS] IMAGE [COMMAND] [ARG...] [docker run] [docker run] # -d: # --restart=always: # -p host port: root@hello:~# docker run --name=myningx -d --restart=always -p 88:80 nginx Unable to find image 'nginx:latest' locally latest: Pulling from library/nginx 7d63c13d9b9b: Pull complete 5cb019b641b5: Pull complete d477de77abf8: Pull complete c60e7d4c1c30: Pull complete 365a49996569: Pull complete 039c6e901970: Pull complete Digest: sha256:168a6a2be5c65d4aafa7a78ca98ff8b110fe44c6ca41e7ccb4314ed481e32288 Status: Downloaded newer image for nginx:latest 15db0ba492cf2b86714e3e29723d413b97e64cc2ee361d4109f4216b2e0cba60 root@hello:~# root@hello:~# curl -i 127.0.0.1:88 HTTP/1.1 200 OK Server: nginx/1.21.4 Date: Wed, 17 Nov 2021 02:03:13 GMT Content-Type: text/html Content-Length: 615 Last-Modified: Tue, 02 Nov 2021 14:49:22 GMT Connection: keep-alive ETag: "61814ff2-267" Accept-Ranges: bytes root@hello:~#Copy the code
Copy the code
View the currently running container
root@hello:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 15db0ba492cf nginx "/ docker-entryPoint...." About a minute ago Up About a minute 0.0.0.0:88->80/ TCP, :::88->80/ TCP myningx root@hello:~#Copy the code
Copy the code
Stop the container
root@hello:~# docker stop 15db0ba492cf
15db0ba492cf
root@hello:~# 
root@hello:~# docker  ps 
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
root@hello:~#
Copy the code
Copy the code
View all containers
root@hello:~# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 15db0ba492cf nginx "/ docker - entrypoint...." 2 minutes ago Exited (0) 12 seconds ago myningx root@hello:~#Copy the code
Copy the code
Start the container
root@hello:~# docker start 15db0ba492cf 15db0ba492cf root@hello:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 15db0ba492cf nginx "/docker-entrypoint...." 2 minutes ago Up 3 seconds 0.0.0.0:88->80/ TCP, :::88->80/ TCP myningx root@hello:~#Copy the code
Copy the code
Delete container that cannot be deleted while running
root@hello:~# docker rm 15db0ba492cf
Error response from daemon: You cannot remove a running container 15db0ba492cf2b86714e3e29723d413b97e64cc2ee361d4109f4216b2e0cba60. Stop the container before attempting removal or force remove
Copy the code
Copy the code
Forcibly deleting containers
root@hello:~# docker rm -f 15db0ba492cf
15db0ba492cf
root@hello:~#
Copy the code
Copy the code

3. Enter the container and operate the container

root@hello:~# docker exec -it b1d72657b /bin/bash
root@b1d72657b272:/# 
root@b1d72657b272:/#
Copy the code
Copy the code

4. Modify the container contents

root@hello:~# docker exec -it b1d72657b /bin/bash root@b1d72657b272:/# echo "123" > /usr/share/nginx/html/index.html Root @ b1d72657b272: / # root @ b1d72657b272: # / echo "cby" > / usr/share/nginx/HTML/index. The HTML root @ hello: ~ # curl 127.0.0.1:88 123 root@hello:~# root@hello:~# docker exec -it b1d72657b /bin/bash root@b1d72657b272:/# echo "cby" > The/usr/share/nginx/HTML/index. The HTML root @ hello: ~ # curl 127.0.0.1:88 cby root @ hello: ~ #Copy the code
Copy the code

5. Mount external data

root@hello:~# docker run --name=myningx -d --restart=always -p 88:80 -v /data/html:/usr/share/nginx/html/ nginx e3788cdd7be695fe9a1bebd7306c131d6380da215a416d19c162c609b8f108ae root@hello:~# root@hello:~# root@hello:~# curl 127.0.0.1:88 < HTML >< head><title>403 Forbidden</title></head> <body> <center> <hr><center>nginx/1.21.4</center> </body> </ HTML > root@hello:~# root@hello:~# echo "cby" > /data/ HTML /index.html root@hello:~# root@hello:~# root@hello:~# curl 127.0.0.1:88 cby root@hello:~#Copy the code
Copy the code

6. Build an image of the running container

root@hello:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e3788cDD7be6 nginx "/ docker-entryPoint...." 4 minutes ago Up 4 minutes 0.0.0.0:88->80/ TCP, :::88->80/ TCP myningx root@hello:~# root@hello:~# docker commit -a "cby" -m "my app" e3788cdd7be6 myapp:v1.0 sha256:07a7b54c914c79dfbd402029a3d144201235eca72a4f26c92e2ec7780c485226 root@hello:~# root@hello:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE MyApp v1.007a7b54c914c 4 seconds ago 141MB nginx latest e9CE56a96f8e 8 hours ago 141MB root@hello:~#Copy the code
Copy the code

7. Save and import images

root@hello:~# docker save -o cby.tar myapp:v1.0 root@hello:~# ll cby.tar -rw------- 1 root root 145910784 Nov 17 02:21 cby.tar root@hello:~# root@hello:~# docker load -i cby.tar Loaded image: Myapp :v1.0 root@hello:~# root@hello:~# Docker images REPOSITORY TAG ID CREATED SIZE MyApp v1.007a7b54C914c 3 Minutes ago 141MB nginx latest e9ce56a96f8e 8 hours ago 141MB nginx 1.20.1c8d03f6b8b91 5 weeks ago 133MB root@hello:~#Copy the code
Copy the code

8. Push to DockerHub and pull the image on other hosts

root@hello:~# docker tag myapp:v1.0 chenbuyun/ myApp :v1.0 root@hello:~# root@hello:~# docker login Login with your docker  ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one. Username: chenbuyun Password: WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded root@hello:~# Docker push chenbuyun/myapp: v1.0 The push refers to The repository docker. IO/chenbuyun/myapp 799 aefeaf6b1: Pushed fd688ba2259e: Mounted from library/nginx c731fe3d8126: Mounted from library/nginx 3b1690d8cd86: Mounted from library/nginx 03f105433dc8: Mounted from library/nginx bd7b2912e0ab: Mounted from library/nginx E8b689711F21: Mounted from library/nginx V1.0: Digest: sha256:f085a533e36cccd27a21fe4de7c87f652fe9346e1ed86e3d82856d5d4434c0a0 size: 1777 root@hello:~# root@hello:~# docker logout Removing login credentials for https://index.docker.io/v1/ root@hello:~# root@hello:~# docker pull chenbuyun/myapp:v1.0 v1.0: Pulling from chenbuyun/myapp Digest: sha256:f085a533e36cccd27a21fe4de7c87f652fe9346e1ed86e3d82856d5d4434c0a0 Status: Downloaded newer image for chenbuyun/myapp: v1.0 docker. IO/chenbuyun/myapp: v1.0 root @ hello: ~ # root @ hello: ~ # docker images REPOSITORY TAG IMAGE ID CREATED SIZE Chenbuyun/MyApp v1.007a7b54c914c 9 minutes ago 141MB MyApp v1.007a7b54c914c 9 REPOSITORY TAG IMAGE ID CREATED SIZE Chenbuyun/MyApp v1.007a7b54c914c 9 Minutes ago 141MB MyApp v1.007a7b54c914c 9 Minutes ago 141MB nginx latest e9ce56a96f8e 8 hours ago 141MB nginx 1.20.1c8d03f6b8b91 5 weeks ago 133MB root@hello:~#Copy the code
Copy the code

Above only for common commands, more docker related knowledge in: www.runoob.com/docker/dock…

Linux O&M Communication community

Linux o&M Community, Internet news and technical exchanges.

55 original articles

The public,

Blog.csdn.net/qq\_3392175…

my.oschina.net/u/3981543

www.zhihu.com/people/chen…

Segmentfault.com/u/hppyvyv6/…

Juejin. Cn/user / 331578…

Space.bilibili.com/352476552/a…

Cloud.tencent.com/developer/c…

Zhihu, CSDN, Open Source China, Sifu, Digg, Bilibili, Tencent Cloud

This article uses the article synchronization assistant to synchronize