Operation and maintenance chapter Docker first see the deity-1

Pay attention to the WeChat public account: Java Book Club for more good articles Yo Ollie

1. What is Docker?

Docker's official website

Modernize your applications, accelerate innovation Securely build, share and run modern applications anywhere

Realize application modernization, accelerate innovation and securely build, share and run modern applications anywhere

Docs

Docker is a platform for developers and sysadmins to build, share, and run applications with containers. The use of containers to deploy applications is called containerization. Containers are not new, but their use for easily deploying applications is.

*Docker is a platform for developers and system administrators to use containers to [build, share and run] applications. **The use of containers to deploy applications is called [containerization]. * Containers are not new, but they are used to easily deploy applications.

2. Why is Docker?

​ Let's first look at the development history of operation and maintenance, develop a project, and deploy online

  • Stand-alone deployment

[External link image transfer failed. The source site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-xDmBsofu-1583691157022)(E:\architect\architect\markdown\stand-alone deployment.png)]

Stand-alone deployment is convenient and fast at the beginning of the project, but when our project visits increase and the business complexity increases, many shortcomings will be exposed at this time:

成本高 部署慢 资源浪费``硬件限制``不利于扩展

  • The age of virtualization

[External link image transfer failed. The source site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-LJiei4yo-1583691157024)(E:\architect\architect\markdown\containerization current.png)]

Compared with before, the use of virtual machines can make better use of resources and is relatively easy to expand, but it also brings many disadvantages:

初始化占用资源较大 移植性较差 资源利用率低

  • Containerized now

[External link image transfer failed. The source site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-n1rssmPL-1583691157025)(E:\architect\architect\markdown\containerization current.png)]

The advantages of docker, to quote the official words:

  • Standard: Docker created the industry standard for containers, so they could be portable anywhere
  • Lightweight: Containers share the machine’s OS system kernel and therefore do not require an OS per application, driving higher server efficiencies and reducing server and licensing costs
  • Secure: Applications are safer in containers and Docker provides the strongest default isolation capabilities in the industry
  • Standards: Docker has created industry standards for containers, so they can be ported anywhere
  • **Lightweight: **The container shares the computer's OS system kernel, so there is no need for every application to have an OS, which improves server efficiency and reduces server and license costs
  • **Security: **The application in the container is more secure. Docker provides the most powerful default isolation function in the industry

3. Docker environment construction

​ Install centos7 on windows, using Vagrant+Virtual Box:

  • Vagrant installation:
#1.访问vagrant官网
https://www.vagrantup.com
#2.点击Downloads,下载windows及对应版本
#3.next安装
#4.命令行输入vagrant 测试是否安装成功
  • Virtual Box installation:
#1.访问VirtualBox官网 
https://www.virtualbox.org/ 
#2.点击Downloads,下载windows及对应版本
#4.next安装 
#注意事项:(我在安装的时候没有遇到哦)
[win10中若出现]安装virtualbox快完成时立即回滚,并提示安装出现严重错误 
	1)打开服务 
	2)找到Device Install Service和Device Setup Manager,然后启动
	3)再次尝试安装
  • Install centos
#1.创建docker-centos7文件夹(注意目录路径不要有中文字符),进入文件夹 
#2. 在此目录下打开cmd(或者powershell),运行vagrant init centos/7 此时会在当前目录下生成Vagrantfile
#3 准备centos7的box (也可以运行vagrant up 此时会找centos7的镜像,本地有就用本地的,本地没有就会拉取远端的,但是直接拉去远端太慢,建议先下载后从本地拉取)
	1)下载地址:https://pan.baidu.com/s/1fKylFndofc77zFWCUjBo8w 下载完成后放入上述新建的文件夹中
	2)vagrant box add centos/7 XX\virtualbox.box (你存放的地址)	
	3)vagrant box list 查看本地的box(这时候可以看到centos/7)
	4)vagrant up (根据本地的centos7 box创建虚拟机。打开virtual box,可以发现centos7创建成功)
#4. vagrant基本操作
	1)vagrant ssh 进入刚才创建的centos7中
    2)vagrant status 查看centos7的状态 
    3)vagrant halt 停止centos7 
    4)vagrant destroy 删除centos7 
    5)vagrant status 查看当前vagrant创建的虚拟机 
#5.修改虚拟机属性配置文件Vagrantfile之后执行vagrant reload
	config.vm.box = "centos/7"
	config.vm.network "public_network"
	config.vm.provider "virtualbox" do |vb|
		   vb.memory="3000"
		   vb.name  ="alvin-centos7"
		   vb.cpus   = 2
	end
	

Based on the above installation steps of vagrant+virtual box+centos, we can already install docker, but the windos command window is not very friendly to us, so we choose xshell6 installation

  • xshell login
#1.查看centos7的基本信息,主要获取Hostname Port IndentityFile信息
vagrant ssh-config 

#2.进入虚拟机查看其ip地址,复制其ip地址192.168.110.164
vagrant ssh 
ip a
#3.使用root账户登录
	sudo -i	
	vi /etc/ssh/sshd_config	
	修改PasswordAuthentication yes
	修改root密码 passwd 
	systemctl restart sshd
#4.连接xshell
	使用root账户+刚才修改的密码进行登录。
  • Realize box distribution

    Before installing docker, we need to understand the distribution of virtual.box to ensure that we can quickly create a new virtual machine.

#1.回到我们上述的docker-centos目录
#2.退出虚拟机 vagrant halt
#3.执行vagrant package --output docker.centos7.box
#4.将上述执行结果docker-centos7.box添加到其他的vagrant环境中(注意:此时要新建一个目录,用来存放docker-centos7.box,然后进入到该目录下)
	vagrant box add docker-centos7 docker-centos7.box
#5.执行vagrant init docker-centos7 得到vagrantfile
#6.执行vagrant up就会发现启动了一个新的虚拟机。
  • Install Docker

Official website description: https://docs.docker.com/install/linux/docker-ce/centos/

#1.win+r打开命令窗口,执行cmd
#2.切换到docker-centos目录,执行vagrant up命令打开虚拟机(因为此时重启电脑了,虚拟机需要重新打开)
#3.使用xhsell登录虚拟机。
#4.执行以下命令:
	sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
	sudo yum install -y yum-utils \
  		 device-mapper-persistent-data \
  		 lvm2
#5.为了拉取镜像快速,可以登录自己的阿里云服务器->选择控制台->容器镜像服务->镜像中心->镜像加速器
	sudo mkdir -p /etc/docker
	sudo tee /etc/docker/daemon.json <<-'EOF'
	{
    
    
  	"registry-mirrors": ["https://gys4eox1.mirror.aliyuncs.com"]
	}
	EOF
	#(这个是免费的哦~)
	sudo systemctl daemon-reload
	sudo systemctl restart docker
#6.正式安装docker
	sudo yum install docker-ce docker-ce-cli containerd.io
#7.启动Docker
	sudo systemctl start docker
	sudo systemctl enable docker(设置开机启动)
	docker version --查看docker版本信息

4.Docker application scenarios

www.docker.com —> Solutions

#1.有助于微服务的落地和部署
#2.充分利用物理机资源,同时能够整合服务器资源
#3。提高开发效率,测试效率,部署效率,有利于DevOps的落地,CI/CD(持续集成)
#4.云原生落地,应用更好地迁移
...

5.Docker core

Docker Engine

Docker Engine is a client-server application with these major components:

  • A server which is a type of long-running program called a daemon process (the dockerd command).
  • A REST API which specifies interfaces that programs can use to talk to the daemon and instruct it what to do.
  • A command line interface (CLI) client (the docker command).

Docker Engine is a client-server application with the following main components:

  • The server is a long-running program called a daemon process ( dockerdcommand).
  • REST API, which specifies the interface that programs can use to communicate with the daemon and instruct its operations.
  • Command line interface (CLI) client ( dockercommand).

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-1o7N4glB-1583691157027)(E:\architect\architect\markdown\dockerEngine.png)]

Docker Architecture

Docker uses a client-server architecture. The Docker client talks to the Docker daemon, which does the heavy lifting of building, running, and distributing your Docker containers. The Docker client and daemon can run on the same system, or you can connect a Docker client to a remote Docker daemon. The Docker client and daemon communicate using a REST API, over UNIX sockets or a network interface.

Docker uses a client-server architecture. Docker client and Docker daemon dialogue, the daemon complete the construction, operation and distribution Docker containers heavy work. The Docker client and daemon can run on the same system, or you can connect the Docker client to a remote Docker daemon. The Docker client and daemon communicate using REST APIs on UNIX sockets or network interfaces.

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-j72X8wG2-1583691157028)(E:\architect\architect\markdown\docker architecture.png)]

Docker Images and Containers

A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings. A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. Container images become containers at runtime and in the case of Docker containers- images become containers when they run on Docker Engine.

Fundamentally, a container is nothing but a running process, with some added encapsulation features applied to it in order to keep it isolated from the host and from other containers. One of the most important aspects of container isolation is that each container interacts with its own, private filesystem; this filesystem is provided by a Docker image. An image includes everything needed to run an application – the code or binary, runtimes, dependencies, and any other filesystem objects required.

A Docker container image is a lightweight, standalone, executable software package that contains everything needed to run an application: code, runtime, system tools, system libraries, and settings. A container is a standard unit of software that packages code and all its dependencies, so an application can run quickly and reliably from one computing environment to another. The container image becomes a container at runtime. For a Docker container, the image is on the container Docker Engine . Becomes a container at runtime.

Fundamentally speaking, a container is just a running process, and some additional encapsulation functions are applied to it to isolate it from the host and other containers. One of the most important aspects of container isolation is that each container interacts with its own private file system. The file system is provided by the Docker image . The image includes everything needed to run the application-code or binaries, runtime, dependencies, and any other file system objects needed.

Containers and virtual machines

A container runs natively on Linux and shares the kernel of the host machine with other containers. It runs a discrete process, taking no more memory than any other executable, making it lightweight.

By contrast, a virtual machine (VM) runs a full-blown “guest” operating system with virtual access to host resources through a hypervisor. In general, VMs incur a lot of overhead beyond what is being consumed by your application logic.

The container runs locally on Linux and shares the host's kernel with other containers. It runs a discrete process and does not take up more memory from any other executable files, thus making it lightweight.

In contrast, a virtual machine (VM) runs a mature "guest" operating system and accesses host resources virtually through a hypervisor . In general, VM will incur a lot of overhead, which exceeds the overhead consumed by application logic.

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-Wp1m5rf9-1583691157029) (E:\architect\architect\markdown\container and virtual machine.png)]

6.Docker use

So much has been said above, so we have Docker, how do we use it? May wish to use docker to install some basic applications.

  • Docker first experience
#创建tomcat容器
	docker pull tomcat
	##启动tomcat
	docker run -d --name my-tomcat -p 9090:8080 tomcat
#创建mysql容器
	docker run -d --name my-mysql -p 3301:3306 -e MYSQL_ROOT_PASSWORD=alvin123 --privileged mysql
#进入到容器内部
	docker exet -it containerid /bin/bash
	

So where do we pull these container images from?

https://hub.docker.com/

  • Docker commonly used commands
#1.查看本地Docker镜像
	docker images
#2.拉取镜像
	docker pull
#3.创建容器
	docker run
#4.查看当前正在运行的容器
	docker ps
#4.查看所有运行过的容器
	docker ps -a

7. Docker design conjecture

Stratified thinking

[External link image transfer failed, the source site may have an anti-hotlink mechanism, it is recommended to save the image and upload it directly (img-pBfq2rO1-1583691157030) (E:\architect\architect\markdown\docker hierarchical design)]

The above is the basic information of the docker deity. Next time we will conduct a soul torture on docker, welcome to see~

Guess you like

Origin blog.csdn.net/aiwaston/article/details/104744926