Docker
什么是容器化技术
应用部署演化主要分为三个阶段:
物理机时代
虚拟机时代
容器化时代
物理机时代

经济成本较高:物理服务器硬件成本高,不易于维护
受制于硬件:内存、CPU能力提升有限
部署成本较高:主机存放、系统安装、程序环境、应用部署多个步骤
资源浪费:应用对硬件的要求不一,可能造成磁盘浪费,或者是cpu的浪费
难于扩展和迁移:比如增加内存、主机迁移、操作系统更换等
虚拟化时代

相比较物理机,虚拟化可以让物理机的资源得到最充分的应用。多个虚拟机运行在同一个物理机上。常用的虚拟化软件有VMware、KVM等。
多部署:一台物理机可以创建多个虚拟机
资源池:物理机的硬件资源被Hypervisor抽象为一个资源池,资源池中的资源可以被动态分配给每个虚拟机
资源隔离: 每个虚拟机的资源都是独立的,有自己的软件和硬件资源,不会相互影响
很容易扩展:资源可被随时扩展和缩减(比如给VM1分配2核2g内存),虚拟机可以随时创建
VM需要安装操作系统:较小的应用部署在一个VM上,会造成操作系统层面的资源浪费
容器化时代

相较于虚拟机,容器不会打包操作系统,所以每个容器所占用的资源是变小的。容器底层依赖于容器化引擎(比如Docker),而Docker引擎则直接依赖宿主机操作系统。
虚拟化是物理层面的隔离,比如内存大小分配等
容器是在APP层面的隔离,他们共享物理资源
指标对比:

容器化解决的问题
传统的服务器方式的缺点
上线流程繁琐: 开发、测试、申请资源、审批、部署、测试等环节
资源利用率低:普遍服务器利用率低,造成过多浪费(通常在百分之十以上)
扩容、缩容不及时:业务员高峰期扩容流程复杂,上线不及时
服务器环境臃肿,对维护迁移带来困难(要保证资源利用率提高,通常会在一台机器上部署多个应用,就会造成环境臃肿)
环境不一致
容器化的优点
容器化技术的产生,让软件开发的协作方式也发生了变化。传统上,一个应用程序的上线过程是极其繁杂的,需要开发人员和运维人员配合进行。开发人员需要将要上线的程序打包、将数据库数据导出、以及各种参数以及配置文件交给运维人员。运维人员会根据配置文档将环境搭建起来并部署应用程序。如果有任意一个步骤出现问题,就可能导致严重的上线问题。而有了容器之后,开发人员可以将应用程序、环境、配置都打包为一个容器,再交给运维人员,这样就不会再出现问题了。同时,容器化技术提供了标准化的应用部署方案,运维人员只需要通过容器编排工具,就可以很方便的将应用程序发布到各个集群的节点上;不仅如此,还提供了容器管理工具,便于运维人员监控和修复。

标准化的迁移方式:开发将程序所需的所有环境打包到镜像中,将镜像交给运维处理
统一参数配置:可对应用程序参数进行统一管理
自动化部署:镜像 > 容器的过程都是自动进行的
应用集群监控:通过监控方便了解程序的运行状态以及集群状态
是开发和运维之间沟通的桥梁
什么是Docker

Docker是一款开源的容器引擎,基于Go语言开发。
是一种操作系统级别的虚拟化技术(也就是操作系统沙箱机制)
主要依赖于Linux的内核特性:Namespace(资源隔离)和Cgroups(资源限制)
Docker是一个平台(鲸鱼),多个容器(集装箱)可以运行在Docker平台上
集装箱:可以保证不同的货物(应用程序)放在同一个货轮上(docker引擎),而不受影响
开发人员和运维人员职责逻辑分离
多环境保持一致性,保持应用程序在测试、开发、生产环境中整个生命周期的一致性以及标准化
开发人员:
开发项目
打包项目环境+代码为镜像
部署到容器平台
运维人员:
高效管理这些容器
做好预警
相关文档
基本组成

Docker Client,Docker命令(基于C/S架构)
Host,docker主机,也就是docker的守护进程,负责处理client的指令,用来从Registery中拉取镜像并创建容器
Docker Registry,镜像仓库,负责存储、提供镜像
安装Docker
前置操作
关闭selinux以及防火墙
开始安装
参见官方文档:Get Docker | Docker Documentation

启动并设置开机启动
systemctl start docker
systemctl enable docker
查看docker信息
$ docker info
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Docker Buildx (Docker Inc., v0.7.1-docker)
scan: Docker Scan (Docker Inc., v0.12.0)
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 20.10.12
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runtime.v1.linux runc io.containerd.runc.v2
Default Runtime: runc
Init Binary: docker-init
containerd version: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
runc version: v1.0.2-0-g52b36a2
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-957.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 972.6MiB
Name: localhost.localdomain
ID: YLZ6:JKLK:DSET:Q4ZR:ECLD:PHRC:YNC7:5UL2:JTNT:2A5N:T5GR:YGKM
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
配置镜像加速
网络代理:
Docker 官方中国区: https://registry.docker-cn.com
网易: http://hub-mirror.c.163.com
中科大: https://docker.mirrors.ustc.edu.cn
配置:
## 使用阿里云镜像加速器
[root@localhost ~]# mkdir -p /etc/docker
[root@localhost ~]# tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://9cpn8tt6.mirror.aliyuncs.com"]
}
EOF
配置完毕重启docker:
systemctl restart docker
Commands
[root@localhost ~]# docker --help
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
# 管理命令,就是分类后的命令
Management Commands:
app* Docker App (Docker Inc., v0.9.1-beta3)
builder Manage builds
buildx* Docker Buildx (Docker Inc., v0.7.1-docker)
config Manage Docker configs
container Manage containers
context Manage contexts
image Manage images
manifest Manage Docker image manifests and manifest lists
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
scan* Docker Scan (Docker Inc., v0.12.0)
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
# 命令,早期只有这个
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
To get more help with docker, check out our guides at https://docs.docker.com/go/guides/
镜像管理
镜像是什么
是一个分层存储文件,不是一个单一的文件
一个软件的环境
一个镜像可以创建N个容器
一种标准化的交付
一个不包含Linux内核而又精简的Linux操作系统
常用镜像管理命令
[root@localhost ~]# docker image --help
Usage: docker image COMMAND
Manage images
Commands:
构建镜像
build Build an image from a Dockerfile
查看镜像历史,也就是dockerfile镜像的分层、步骤
history Show the history of an image
导入镜像
import Import the contents from a tarball to create a filesystem image
显示一个或者多个镜像信息
inspect Display detailed information on one or more images
加载tar规定或者标准输入的镜像
load Load an image from a tar archive or STDIN
列出所有镜像
ls List images
删除未使用的镜像,释放空间
prune Remove unused images
拉取镜像到本地
pull Pull an image or a repository from a registry
推送镜像
push Push an image or a repository to a registry
删除镜像
rm Remove one or more images
保存一个或者多个镜像到一个 tar归档文件
save Save one or more images to a tar archive (streamed to STDOUT by default)
创建一个引用源镜像标记目标镜像
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
Run 'docker image COMMAND --help' for more information on a command.
容器管理
容器命令总览
[root@localhost ~]# docker container --help
Usage: docker container COMMAND
Manage containers
Commands:
attach Attach local standard input, output, and error streams to a running container
将容器的当前状态提交为一个新的镜像
commit Create a new image from a container's changes
拷贝文件/文件夹到容器中
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
在容器中运行某个命令
exec Run a command in a running container
export Export a container's filesystem as a tar archive
查看一个或者多个容器的详细信息
inspect Display detailed information on one or more containers
杀死容器
kill Kill one or more running containers
获取容器的日志
logs Fetch the logs of a container
列出所有容器
ls List containers
pause Pause all processes within one or more containers
列出或者指定容器端口映射
port List port mappings or a specific mapping for the container
移除已经停止的容器
prune Remove all stopped containers
rename Rename a container
重启容器
restart Restart one or more containers
移除容器
rm Remove one or more containers
run Run a command in a new container
启动容器
start Start one or more stopped containers
显示容器资源使用统计
stats Display a live stream of container(s) resource usage statistics
停止容器
stop Stop one or more running containers
显示一个容器运行的进程
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
wait Block until one or more containers stop, then print their exit codes
Run 'docker container COMMAND --help' for more information on a command.
创建容器
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
常用的创建容器的命令:
# -d, -detach 运行容器到后台
docker run nginx -d
# -i, -interactive 交互式
# -t, -tty 分配一个伪终端
docker exec -it 容器id
# 创建容器时指定环境变量
# -e 用于指定环境变量
docker run -d -e env=prod nginx
# 创建容器时指定端口映射
# 88时宿主机端口,80是容器服务端口
dokcer run -d -p 88:80 nginx
# 创建容器时将容器的所有服务端口都暴露给宿主机的随机端口
docker run -d -P nginx
# 创建容器名称,不指定名称会自动随机生成
docker run --name web -d nginx
# 设置容器的主机名 hostname
docker run -h web -d nginx
# 设置容器的退出重启策略,默认为no,可选值有 always、on-failure
docker run -d --restart always nginx
资源限制相关:
# 这个nginx最多可以使用内存500m,1核心的cpu
docker run -m="500m" --cpus="1" nginx
常用命令
docker ps -a 列出所有的容器
docker ps -q 列出所有容器id
docker rm $(docker ps -q) 删除所有容器
数据持久化
docker提供2种方式可以将数据从宿主机挂载到容器中:
volumes:Docker管理宿主机文件系统的一部分,主要位于
/var/lib/docker/volumes
位置下bind mounts:将宿主机上的任意的文件或者目录挂载到容器中
Bind Mounts
Jenkins、Gitlab等服务通常都是采用这种方式。
docker run -d --name=mginx-test --mount type=bind,src=/app/wwwroot,dst=/usr/share/nginx/html nginx
等同于
docker run -d --name=nginx-test -v /app/wwwroot:/usr/share/nginx/html nginx
注意,在容器启动时,宿主机的路径内容会挂载(覆盖)容器,以宿主机为准
Volumes
创建数据卷:
docker volume create nginx-vol
docker volume ls
docker volume inspect nginx-vol
使用数据卷:
docker run -d --name=nginx-test --mount src=nginx-vol,dst=/usr/share/nginx/html nginx
等同于
docker run 0d --name=nginx-test -v nginx-vol:/usr/share/nginx/html nginx
容器网络

veth pair:成对出现的一种虚拟网络设备,数据从一端进,从另一端出。用于解决网络命名空间中间的隔离
Docker0:网桥是一个二层网络设备(mac路由),通过网桥可以将Linux支持的不同的端口连接起来,并实现类似交换机那样的多对多的通信

DNAT:目标网络地址转换
SNAT:源地址转换
最后更新于
这有帮助吗?