Docker actual combat summary (very comprehensive), collected!

Migrant worker brother 2020-11-08 16:10:32
docker actual combat summary comprehensive

Docker brief introduction

Docker Is an open source application container engine , Developers can package their own applications into containers , And then move to other machines docker Application , Rapid deployment can be achieved .

Simple understanding ,docker It's a software containerization platform , It's like a boat 、 train 、 Trucks carry containers regardless of the cargo inside them , The software container serves as the standard unit of software deployment , It can contain different code and dependencies .

Container software in this way , Developers and IT Professionals only need to make few or no changes , You can deploy it to different environments , If something goes wrong , You can also do it by mirroring , Fast recovery service .

Docker advantage

1. Feature advantage 2. Resource advantages

Docker Basic concepts

Client( client ): yes Docker The client side of , Can accept user commands and configuration IDS , And with Docker daemon signal communication .

Images( Mirror image ): Is a read-only template , Including creation Docker Description of the container , It's a bit like the operating system installation CD .

Containers( Containers ): The running instance of the image , The relationship between image and container is similar to class and object in object-oriented .

Registry( Warehouse ): It is a service for centralized storage and distribution of images . Most commonly used Registry It's official. Docker Hub .

Docker What has changed ?

Docker Changed cloud services , Make the ideal of cloud service integration and common gradually become possible . also Docker It's already part of the cloud strategy , Many developers are planning to use Docker Move business to the cloud . in addition , In order to avoid being bound by cloud service providers ,Docker Become the first choice for many developers .

Docker Changed product delivery , It provides a complete set of solutions and processes for the whole life cycle of products .

Docker Changed the way of development , Provides simplified environment configuration 、 Encapsulated running environment and unified environment . And it provides a way to deploy quickly .

Docker Changed the test , Multi version testing becomes extremely convenient , It's also easier to quickly build a test environment without developer intervention or setup .

Docker Changed operation and maintenance , The consistency of the environment makes operation and maintenance easier , At the same time, the support of hot update makes the operation and maintenance no longer need to work overtime to deploy updates in the middle of the night , Updates can be made at any time . When major problems arise , It can also quickly roll back to the specified version .

Docker Changed the architecture , Automated capacity expansion support makes architecture simpler , Distributed systems are also easier to build and support . At the same time, the legacy monomer application is also easy to transform into modern application .

All in all , In a way ,Docker Changed some rules of the game in product development . although Docker It's a technology , But it also brings new ideas , New processes and working methods ,Docker In promoting the development of the industry ,Docker Already changing the world , And it's gradually becoming a fact …… Pay attention to the official account of the brother of migrant workers , reply 1024 obtain 2TB One copy of information , Help you learn technology better .

Docker Install and use

operating system :CentOS 7

1、 Installation dependency

yum install -y yum-utils device-mapper-persistent-data lvm2

2、 Add software source

yum-config-manager --add-repo  #  Specify the alicloud image source

3、 install docker-ce( There are certain requirements for the system kernel ,centos6 I won't support it )

yum clean all  yum makecache fast        #  Regenerate cache
yum -y install docker-ce docker-ce-cli

4、 Set auto start and start

systemctl enable docker
systemctl start docker

5、 View version

docker version

Run the example :Nginx

1、 Search and download images

docker search nginx
docker pull nginx

2、 Start a container and map ports to local

docker run -d -p 8080:80 --name Nginx nginx    #  See below for details of parameters

3、 Access local mapping port

Docker Common commands

1. Mirror control

 Search mirroring :docker search  [OPTIONS]  TERM
Upload the image :docker push  [OPTIONS]  NAME[:TAG]
Download mirroring :docker pull  [OPTIONS]  NAME[:TAG]
Submit image :docker commit [OPTIONS]  CONTAINER NAME[:TAG]
Build a mirror image :docker build  [OPTIONS]  PATH
delete mirror :docker rmi [OPTIONS]  IMAGE  [IMAGE...]
Add image labels :docker tag SOURCE_IMAGE[:TAG]  TARGET_IMAGE[:TAG]
View all mirrors :docker images  [OPTIONS]  [REPOSITORY[:TAG]]

2. Container control

 start-up / Restart container :docker start/restart CONTAINER
stop it / Stop the container :docker stop/ kill CONTAINER
Delete container :docker rm [OPTIONS] CONTAINER [CONTAINER...]
Rename container :docker rename CONTAINER CONTAINER_NEW
Into the container :docker attach CONTAINER
Execute container command :docker exec CONTAINER COMMAND
View container log :docker logs [OPTIONS] CONTAINER
Look at the container list :docker ps [OPTIONS]

3. Container start up

docker  run  [OPTIONS]  IMAGE  [COMMAND]  [ARG...]
-d :  Background running container , And return to the container ID
-i: Run container in interactive mode , Usually with  -t Use at the same time
-t: Reassign a pseudo input terminal to the container , Usually with  -i Use at the same time
-v: Bind mount Directory
--name="mycontainer":  Specify a name for the container
--net="bridge":  Specify the network connection type of the container , Support the following :
     bridge / host / none / container:<name|id>
-p/-P : Port mapping , The format is as shown in the figure :

4. Other commands

 see docker Information :docker info
docker Order help :docker run --help
Copy files to container :docker cp custom.conf Nginx:/etc/nginx/conf.d/
Update container startup :docker container update --restart=always nginx
see docker journal :tail -f /var/log/messages

Reference resources : this 20 individual Docker Command, There are a few that you will ? For more information, please refer to the official website :

Docker Mirror construction

1.Docker commit(1 function 2 modify 3 preservation )

# Run container
docker run -dit -p 8080:80 --name Nginx nginx
# Modify the container ( I'm just doing a demonstration here , So just copy the file , The specific modification needs to be based on your actual situation )
docker cp custom.conf Nginx:/etc/nginx/conf.d/
# Save the container as a new image
docker commit Nginx zwx/nginx

2.Dockerfile(1 To write 2 structure )

# To write Dockerfile file
vim Dockerfile
# perform Dockerfile file
docker build -t zwx/nginx . # There's a little bit in the back , Represents... In the current directory dockerfile file

3.Dockerfile Commonly used instructions More on that :Dockerfile File,

Docker Local repository

1、 Pull image warehouse

docker search registry
docker pull registry

2、 Start the image service

docker run -dit 
--name=Registry   #  Specify the container name
-p 5000:5000     #  The default port of the warehouse is 5000, Map to host , In this way, the host address can be used to access
--restart=always    #  Automatic restart , So every time docker After the restart, the warehouse container will also start automatically
--privileged=true   #  Add security rights , In general, there is no need for
-v /usr/local/my_registry:/var/lib/registry      #  Save the warehouse image data to the host

3、 register https agreement ( You need to download the image through the local warehouse , All need to be configured )

vim /etc/docker/daemon.json #  No such file by default , You need to add , If there is one, please add something .
   { "insecure-registries":[" xx.xx.xx.xx:5000"] 
   }  # Appoint ip Address or domain name

4、 newly added tag Indicate warehouse address

docker tag zwx/nginx x.xx.xx.xx:5000/zwx/nginx  #  If the warehouse address is specified at build time , You can omit

5、 Upload image to local warehouse

docker push x.xx.xx.xx:5000/zwx/nginx

6、 Check out the local warehouse

curl -XGET http://x.xx.xx.xx:5000/v2/_catalog

More on that :docker Build a local private warehouse

Docker With graphics management tools Portainer

1. brief introduction Portainer yes Docker Graphical management tools for , Provide status display panel 、 Rapid deployment of application template 、 Basic operation of container image network data volume ( Including uploading and downloading images , Create container and other operations ).

Event log display 、 Container console operation 、Swarm Centralized management and operation of clusters and services 、 Login user management and control functions . Very comprehensive , It can basically meet all the requirements of small and medium-sized units for container management . More on that :docker Visual management tools recommend

2. Install and use

# Search and download images
docker search portainer
docker pull portainer/portainer
# Stand alone operation
docker run -d 
-p 9000:9000  # portainer The default port is 9000, Map to local 9000 port , Access... Via local address
--restart=always   #  Set automatic restart
-v /var/run/docker.sock:/var/run/docker.sock   #  A single machine must specify docker.sock
--name Prtainer portainer/portainer

visit http://localhost:9000, The first time you log in, you need to register users , to admin User set password , Then choose the stand-alone version local Connect to . Control management

Docker And cluster management tools Swarm

1. brief introduction

Swarm yes Docker An official cluster management tool , Its main function is to turn a number of Docker The host is abstracted as a whole , And manage these through a single portal Docker All kinds of things on the mainframe Docker resources .2. Install and use

Swarm stay Docker 1.12 Before the release, it was a separate project , stay Docker 1.12 After the release , The project was merged into Docker in , Become Docker A subcommand of .

start-up swarm The cluster only needs to execute the initialization command :

docker swarm init        #  The default initialization node is the management node
--advertise-addr xx.xx.xx.xx    # Designated to use ip
--listen-addr xx.xx.xx.xx:2377   # Specify listening ip and port, The default is 2377

Set up manager node

docker swarm join-token manager  # Get management node token, Put the following command
docker swarm join 
--advertise-addr xx.xx.xx.xx 
--listen-addr xx.xx.xx.xx:2377 
--token SWMTKN-1-29ynh5uyfiiospy4fsm4pd4xucyji2rn0oj4b4ak4s7a37syf9-ajkrv2ctjr5cmxzuij75tbrmz 

Set up worker node

docker swarm join-token worker  # Get work nodes token, Put the following command
docker swarm join 
--advertise-addr xx.xx.xx.xx 
--listen-addr xx.xx.xx.xx:2377 
--token SWMTKN-1-29ynh5uyfiiospy4fsm4pd4xucyji2rn0oj4b4ak4s7a37syf9-ajkrv2ctjr5cmxzuij75tbrmz 

Look at the node

docker node ls

Create services

docker service create [OPTIONS] IMAGE [COMMAND] [ARG...]
--detach , -d:   Specifies whether the container runs in the foreground or the background , The default is false
--name:   The service name
--network:   network connections
--publish , -p:   Port mapping
--env , -e:   Set the environment variable
--tty , -t:   Distribute tty equipment , It can support terminal login
--mount:   Document mount
--replicas:   Specify the number of tasks

More reference :Docker Swarm Cluster deployment practice

contrast K8s What are the similarities and differences ?

  • a) Different birth

Google According to its Linux Experience in container management , Transform to docker Management , Namely kubernetes. He did well in many ways , The most important thing is to construct in Google Many years of valuable experience are limited to .

kubernetes It's not for docker Written ,kubernetes Take the cluster to a new height , The price is a steep learning curve .docker-swarm In a different way , It is docker Native clustering tools .

The most convenient part is that it's exposed docker Standard programming interface , Means anything you've been using before with docker Tools of communication (docker CLI, docker compose etc. ), Can be seamless in docker swarm Upper use .

  • b) Different installation configuration

Installation settings swarm It's simple , Simple and flexible . All we need to do is install a service discovery tool , Then install... On all nodes swarm Containers .

By comparison ,kubernetes The installation of is a little complicated and obscure . Different operating systems have different installations . Each operating system has its own independent installation instructions .

  • c) The operation mode is different

Use Swarm It's no different from using containers . such as , You are used to using Docker CLI( Command line interface ), You can continue to use almost the same commands .

If you are used to using Docker Componse To run the container , You can go on Swarm Use in cluster . No matter how you used to use containers , You can still use , It's just used in larger level clusters .

Kubernetes Ask you to learn its own CLI( Command line interface ) And configuration . You can't use the docker-compose.yml To configure , You have to go new and Kubernetes Corresponding configuration .

You can't use what you learned before Docker CLI( Command line interface ). You have to study Kubernetes CLI( Command line interface )

Last , When need is in Docker Swarm and Kubernetes When making a choice , Consider the following :

  • Do you want to depend on Docker Solve the cluster problem by yourself . If it is , choice Swarm. If some functions are Docker China does not support it. , Then it's very likely to be in Swarm I can't find it in China. , because Swarm It depends on Docker API Of .
  • On the other hand , If you want a tool that can solve Docker The limitation of ,Kubernetes It would be a good choice .Kubernetes Not based on Docker, It's based on Google Years of experience in container management . It is acting in its own way .

Docker O & M flow chart

Docker Configuration Management

  • 1. After using the container , Do you need configuration management ?

At first we followed Docker It's official , Belong to the idealists . Naive to think that the container should be inmutable Of , When configuration changes are needed , Rebuild the image and redeploy .

Based on this idea , We are cSphere Image Auto building module added in , The user can configure the address of the code warehouse . The configuration file of the service is saved in Git perhaps SVN In the library , When configuration changes are needed , To the repository Push once , To pass automatically hook Trigger image build , And automatically complete the reconstruction of online containers .

Through this system , Users can update online services in batches conveniently , Not limited to configuration file changes , Code changes are also inherently supported . After actual use , This system can well meet the needs of development and test environment , Improve work efficiency .

however , When used in a production environment , We found that this process is not so perfect , Mainly in : Image building and deployment are automated , But the build is for VCS In one of the warehouses , If you change the configuration of one line, you have to rebuild it as a whole , When updating the container, you also need to redistribute the image to all machines , Configuration changes are too slow . Configuration changes in this way will involve service restart , This is not acceptable in some scenarios of the production environment , It may cause a short service interruption .

  • 2. What should the application profile do ?

Docker The application profile can maintain the ability to support changes for different environments . In addition, the configuration file supports online change , Restart will take effect . Generally divided into the following two ways .

a)Docker environment variable

When making a mirror image, you need to think about it in advance , Which parameters are often changed when deploying containers , Then extract these parameters and make them environment variables of the container , Then fill in different parameters when deploying the container . However, if it is found that some parameters are deployed in different scenarios, they will also be modified , Then we need to remake the image .

b) Application profile

The above management is not very flexible , The flexible management method is to peel the configuration file and image away , This way, the image will not be bound to .

Source :
author :LeoZhanggg
本文为[Migrant worker brother]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云