Home | 简体中文 | 繁体中文 | 杂文 | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | Github | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

第 1 章 Docker

目录

1.1. Installation
1.1.1. CentOS 8 Stream
1.1.1.1. docker-compose
1.1.1.2. 切换镜像
1.1.2. Ubuntu docker-ce
1.1.3. 测试 Docker
1.1.4. 重置 Docker
1.1.5. 早起版本
1.1.5.1. CentOS 7 docker-ce
1.1.5.2. CentOS 6
1.1.5.3. Ubuntu
1.2. 配置 Docker
1.2.1. 开启远程访问
1.2.1.1.
1.2.1.2. /etc/docker/daemon.json
1.2.1.3.
1.2.1.4. 配置SSL证书
1.2.1.5. 通过 SSH 连接远程 Docker
1.2.2. 镜像配置
1.2.2.1. 临时选择镜像
1.2.2.2. 切换国内镜像
1.2.3. DNS
1.2.4. ulimit 资源
1.3. 镜像
1.3.1. 搜索镜像
1.3.2. 获取镜像
1.3.3. 列出本地镜像
1.3.4. tag
1.3.5. 保存和载入镜像
1.3.6. 删除本地镜像
1.3.7. history 镜像历史纪录
1.4. 容器
1.4.1. 查看容器
1.4.2. 启动与终止容器
1.4.3. 进入容器
1.4.4. 运行容器内的命令
1.4.5. 导出和导入容器
1.4.5.1. Ubuntu
1.4.5.2. Mac 导出与导入
1.4.6. 停止所有容器
1.4.6.1. 信号处理
1.4.7. 删除容器
1.4.8. log-driver
1.4.9. 操作系统
1.4.9.1. 设置环境变量
1.4.9.2. /etc/hosts 配置
1.4.9.3. sysctl
1.4.9.4. ulimits
1.4.10. 查看容器内运行的进程
1.4.11. 更新容器资源配置
1.4.12. 查看容器的退出状态
1.4.13. 暂停与恢复容器
1.4.14. 对比容器的变化
1.4.15. 查看容器状态
1.4.16. 重启容器
1.4.17. DNS
1.5. 卷管理
1.5.1. 列出卷
1.5.2. 创建卷
1.5.3. 挂在镜像
1.5.4. 检查卷
1.5.5. 删除卷
1.5.6. 销毁所有未使用的卷
1.5.7. 在多个容器间共享卷
1.5.8. 容器绑定本地文件系统
1.5.9. 只读权限
1.6. Docker 网络
1.6.1. docker0 IP地址
1.6.2. 容器指定固定IP地址
1.6.3. 创建子网
1.6.4. 创建 overlay 网络
1.6.5. 网络命令空间
1.7. 日志
1.7.1. 查看默认驱动
1.7.2. Fluentd 配置
1.7.3. Docker 配置
1.7.4. docker-compose 编排
1.8. 仓库
1.8.1. Docker 官方仓库
1.8.1.1. 登陆仓库
1.8.1.2. 获取镜像
1.8.1.3. 上传镜像
1.8.2. 私有仓库
1.8.2.1. 搭建私有仓库
1.8.2.2. 推送镜像到私有仓库
1.8.2.3. 查询镜像
1.8.2.4. registry 镜像高级配置
1.8.2.4.1. 私有仓库认证
1.8.2.5. registry 接口
1.8.3. Harbor
1.9. Swarms
1.9.1. 管理 Swarms
1.9.1.1. 查看 Swarms 版本
1.9.1.2. 初始化 Swarms
1.9.1.3. 显示 join-token
1.9.1.4. 创建虚拟机
1.9.1.5. 显示虚拟机列表
1.9.1.6. 设置管理节点
1.9.1.7. 环境变量
1.9.1.8. 切换节点
1.9.1.9. 启动/停止节点
1.9.1.10. 离线
1.9.2. Stack
1.9.3. 服务
1.9.3.1. 创建 Service
1.9.3.2. 删除 Service
1.9.3.3. inspect
1.9.4. swarm 卷管理
1.9.4.1. Host Volumes
1.9.4.2. Named Volumes
1.9.4.3. 共享卷
1.10. docker 命令
1.10.1. docker - A self-sufficient runtime for containers
1.10.1.1. 连接远程主机
1.10.1.2. 查看 docker 信息
1.10.1.3. run
1.10.1.3.1. --privileged 让 root 具备真正的 root 权限
1.10.1.4. start / stop / restart
1.10.1.5. ps
1.10.1.5.1. 不截断输出,显示完整信息
1.10.1.6. kill 信号
1.10.1.7. top
1.10.1.8. inspect
1.10.1.8.1. 获取容器名称
1.10.1.8.2. 容器镜像名称
1.10.1.8.3. 获取容器主机名 Hostname
1.10.1.8.4. 查询 IP 地址
1.10.1.8.5. 查询子网
1.10.1.8.6. 容器日志
1.10.1.8.7. 获取 json 配置
1.10.1.8.8. 函数
1.10.1.8.9. 综合查询
1.10.1.9. images
1.10.1.9.1. Getting a new image
1.10.1.10. logs
1.10.1.10.1. 跟踪实时日志
1.10.1.10.2. 显示时间戳
1.10.1.10.3. 显示一段范围内的日志
1.10.1.11. 重置 Docker
1.10.1.12. 仓库操作
1.10.1.12.1. 登陆
1.10.1.12.2. 注销
1.10.1.13. build
1.10.1.14. 网络管理
1.10.1.15. 事件信息
1.10.1.16. 从 docker 中复制文件
1.10.1.17. 查看历史记录
1.10.1.18. 安全漏洞扫描
1.10.1.19. Contexts
1.10.1.19.1. 查看
1.10.1.19.2. 创建
1.10.1.19.3. inspect
1.10.1.19.4. 使用 context
1.10.1.19.5. 删除
1.10.1.19.6. --context 参数
1.10.2. docker-compose - Define and run multi-container applications with Docker.
1.10.2.1. 安装 docker-compose
1.10.2.1.1. 使用 pip 安装
1.10.2.1.2. OSCM 安装
1.10.2.2. 查看版本号
1.10.2.3. 快速入门
1.10.2.4. 启动
1.10.2.5. 停止
1.10.2.5.1. 停止
1.10.2.5.2. 启动
1.10.2.6. 查看进程
1.10.2.7. 查看日志
1.10.2.8. 执行命令
1.10.2.9. 运行
1.11. Dockerfile
1.11.1. 基于 Dockerfile 创建镜像
1.11.1.1. 创建 Dockerfile 文件
1.11.1.2. 创建镜像
1.11.1.3. 运行镜像
1.11.1.4. 测试 Nginx
1.11.1.5. 提交镜像
1.11.2. Dockerfile 缺失的工具
1.11.2.1. debian 镜像
1.11.2.2. centos
1.11.3. Dockerfile 语法
1.11.3.1. COPY
1.11.3.2. EXPOSE
1.11.3.3. ENTRYPOINT
1.11.3.3.1. docker-entrypoint.sh 文件
1.12. docker-compose.yml 容器编排
1.12.1. 版本号
1.12.2. 镜像
1.12.3. 容器名称
1.12.4. 启动策略
1.12.5. 容器用户
1.12.6. 挂在卷
1.12.7. 映射端口的标签
1.12.8. 添加 hosts 文件
1.12.9. 网络配置
1.12.9.1. 自定义 IPv4 子网地址
1.12.9.2. external 外部网络
1.12.9.3. 配置 IPv6
1.12.10. links 主机别名
1.12.11. 链接外部容器
1.12.12. 服务依赖
1.12.13. working_dir
1.12.14. 设置环境变量
1.12.15. 临时文件系统
1.12.16. 编译 Dockerfile
1.12.17. resources 硬件资源分配
1.13. Docker Example
1.13.1. registry
1.13.1.1. Auth + SSL
1.13.2. Example Java - Spring boot with Docker
1.13.2.1. 获取 CentOS 7 镜像
1.13.2.2. 安装 openjdk
1.13.2.3. Spring boot 包
1.13.2.4. 启动 Spring boot 项目
1.13.2.5. 基于 CentOS 7 制作 spring 镜像
1.13.3. Redis
1.13.3.1. Docker 命令
1.13.3.1.1. 获取 Redis 镜像
1.13.3.1.2. 启动一个 Redis 实例
1.13.3.1.3. 进入 Redis
1.13.3.1.4. 启动一个 Redis 实例并映射 6379 端口
1.13.3.1.5. 维护容器
1.13.3.2. Docker compose
1.13.3.3. Docker Stack
1.13.3.4. somaxconn/overcommit_memory
1.13.4. Nginx
1.13.4.1. nginx:latest
1.13.4.2. 安装 Docker Nginx alpine
1.13.4.3. 安装依赖工具
1.13.4.4. 容器内优雅重启
1.13.5. MySQL
1.13.6. MongoDB
1.13.6.1. 使用 mongodb 用户运行
1.13.6.2.
1.13.7. Node
1.14. Docker FAQ
1.14.1. 常用工具
1.14.1.1. Debian/Ubuntu
1.14.1.2. CentOS
1.14.2. 检查 Docker 是否可用
1.14.3. Bitnami
1.14.4. 删除 <none> 镜像

https://www.docker.com

1.1. Installation

1.1.1. CentOS 8 Stream

安装 Docker

		
[root@localhost ~]# dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo
Adding repo from: https://download.docker.com/linux/centos/docker-ce.repo

[root@localhost ~]# dnf install -y docker-ce

[root@localhost ~]# systemctl enable docker
[root@localhost ~]# systemctl start docker
		
		

		
[root@localhost ~]# docker -v
Docker version 19.03.12, build 48a66213fe
		
		

1.1.1.1. docker-compose

https://docs.docker.com/compose/install/

			
curl -sL "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose -v
			
			

1.1.1.2. 切换镜像

			
[root@netkiller ~]# cat << EOF > /etc/docker/daemon.json
> 
> {
>   "registry-mirrors": [
>     "https://hub-mirror.c.163.com",
>     "https://mirror.baidubce.com",
>     "https://docker.mirrors.ustc.edu.cn/"
>   ]
> }
> 
> EOF

[root@netkiller ~]# cat /etc/docker/daemon.json 

{
  "registry-mirrors": [
    "https://hub-mirror.c.163.com",
    "https://mirror.baidubce.com",
    "https://docker.mirrors.ustc.edu.cn/"
  ]
}

[root@netkiller ~]# systemctl restart docker

[root@netkiller ~]# docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
  scan: Docker Scan (Docker Inc., v0.8.0)

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 20.10.7
 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.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: e25210fe30a0a703442421b0f60afac609f950a3
 runc version: v1.0.1-0-g4144b63
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 4.18.0-326.el8.x86_64
 Operating System: CentOS Stream 8
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 7.514GiB
 Name: netkiller
 ID: 5GBU:CMWS:VIVP:TREZ:Y5AP:OGOW:EABK:NP4R:AWUA:S4J2:2YQ2:U7MT
 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
 Registry Mirrors:
  https://hub-mirror.c.163.com/
  https://mirror.baidubce.com/
  https://docker.mirrors.ustc.edu.cn/
 Live Restore Enabled: false			
			
			

1.1.2. Ubuntu docker-ce

从官方网站获得最新社区版

		
#!/bin/bash

sudo apt update

sudo apt remove docker docker-engine docker.io containerd runc

sudo apt install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
	
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null


<!-- sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable" -->
   
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io

apt-cache madison docker-ce
		
		

查看 docker 运行状态

		
root@production:~# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2021-08-17 11:25:04 CST; 57s ago
     Docs: https://docs.docker.com
 Main PID: 7379 (dockerd)
   CGroup: /system.slice/docker.service
           └─7379 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Aug 17 11:25:04 production dockerd[7379]: time="2021-08-17T11:25:04.708262132+08:00" level=info msg="ClientConn switching balancer to \"pick_first\"" module=grpc
Aug 17 11:25:04 production dockerd[7379]: time="2021-08-17T11:25:04.742384618+08:00" level=warning msg="Your kernel does not support swap memory limit"
Aug 17 11:25:04 production dockerd[7379]: time="2021-08-17T11:25:04.742397707+08:00" level=warning msg="Your kernel does not support CPU realtime scheduler"
Aug 17 11:25:04 production dockerd[7379]: time="2021-08-17T11:25:04.742489785+08:00" level=info msg="Loading containers: start."
Aug 17 11:25:04 production dockerd[7379]: time="2021-08-17T11:25:04.811316570+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.18.0.0/16. Daemon option --bip can be used 
Aug 17 11:25:04 production dockerd[7379]: time="2021-08-17T11:25:04.836024290+08:00" level=info msg="Loading containers: done."
Aug 17 11:25:04 production dockerd[7379]: time="2021-08-17T11:25:04.858428922+08:00" level=info msg="Docker daemon" commit=b0f5bc3 graphdriver(s)=overlay2 version=20.10.7
Aug 17 11:25:04 production dockerd[7379]: time="2021-08-17T11:25:04.858470910+08:00" level=info msg="Daemon has completed initialization"
Aug 17 11:25:04 production systemd[1]: Started Docker Application Container Engine.
Aug 17 11:25:04 production dockerd[7379]: time="2021-08-17T11:25:04.875279830+08:00" level=info msg="API listen on /var/run/docker.sock"		
		
		

启动参数配置 /etc/default/docker

		
neo@ubuntu:~$ cat /etc/default/docker
# Docker Upstart and SysVinit configuration file

#
# THIS FILE DOES NOT APPLY TO SYSTEMD
#
#   Please see the documentation for "systemd drop-ins":
#   https://docs.docker.com/engine/admin/systemd/
#

# Customize location of Docker binary (especially for development testing).
#DOCKERD="/usr/local/bin/dockerd"

# Use DOCKER_OPTS to modify the daemon startup options.
#DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4"

# If you need Docker to use an HTTP proxy, it can also be specified here.
#export http_proxy="http://127.0.0.1:3128/"

# This is also a handy place to tweak where Docker's temporary files go.
#export DOCKER_TMPDIR="/mnt/bigdrive/docker-tmp"

		
		

启动脚本 /etc/init/docker.conf

		
neo@ubuntu:~$ sudo cat /etc/init/docker.conf
[sudo] password for neo: 
description "Docker daemon"

start on (filesystem and net-device-up IFACE!=lo)
stop on runlevel [!2345]

limit nofile 524288 1048576

# Having non-zero limits causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
limit nproc unlimited unlimited

respawn

kill timeout 20

pre-start script
	# see also https://github.com/tianon/cgroupfs-mount/blob/master/cgroupfs-mount
	if grep -v '^#' /etc/fstab | grep -q cgroup \
		|| [ ! -e /proc/cgroups ] \
		|| [ ! -d /sys/fs/cgroup ]; then
		exit 0
	fi
	if ! mountpoint -q /sys/fs/cgroup; then
		mount -t tmpfs -o uid=0,gid=0,mode=0755 cgroup /sys/fs/cgroup
	fi
	(
		cd /sys/fs/cgroup
		for sys in $(awk '!/^#/ { if ($4 == 1) print $1 }' /proc/cgroups); do
			mkdir -p $sys
			if ! mountpoint -q $sys; then
				if ! mount -n -t cgroup -o $sys cgroup $sys; then
					rmdir $sys || true
				fi
			fi
		done
	)
end script

script
	# modify these in /etc/default/$UPSTART_JOB (/etc/default/docker)
	DOCKERD=/usr/bin/dockerd
	DOCKER_OPTS=
	if [ -f /etc/default/$UPSTART_JOB ]; then
		. /etc/default/$UPSTART_JOB
	fi
	exec "$DOCKERD" $DOCKER_OPTS --raw-logs
end script

# Don't emit "started" event until docker.sock is ready.
# See https://github.com/docker/docker/issues/6647
post-start script
	DOCKER_OPTS=
	DOCKER_SOCKET=
	if [ -f /etc/default/$UPSTART_JOB ]; then
		. /etc/default/$UPSTART_JOB
	fi

	if ! printf "%s" "$DOCKER_OPTS" | grep -qE -e '-H|--host'; then
		DOCKER_SOCKET=/var/run/docker.sock
	else
		DOCKER_SOCKET=$(printf "%s" "$DOCKER_OPTS" | grep -oP -e '(-H|--host)\W*unix://\K(\S+)' | sed 1q)
	fi

	if [ -n "$DOCKER_SOCKET" ]; then
		while ! [ -e "$DOCKER_SOCKET" ]; do
			initctl status $UPSTART_JOB | grep -qE "(stop|respawn)/" && exit 1
			echo "Waiting for $DOCKER_SOCKET"
			sleep 0.1
		done
		echo "$DOCKER_SOCKET is up"
	fi
end script
		
		
		

1.1.3. 测试 Docker

		
neo@MacBook-Pro ~ % docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

neo@MacBook-Pro ~ % docker image ls 
REPOSITORY                                 TAG                 IMAGE ID            CREATED             SIZE
hello-world                                latest              fce289e99eb9        2 months ago        1.84kB

neo@MacBook-Pro ~ % docker container ls --all
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                          PORTS               NAMES
ea694b443e9e        hello-world         "/hello"            About a minute ago   Exited (0) About a minute ago                       dreamy_feistel
		
		

1.1.4. 重置 Docker

		
docker stop $(docker ps -a -q)
docker rm -f $(docker ps -a -q)
docker rmi -f $(docker images -q)
docker volume rm $(docker volume ls -q)	
		
		

1.1.5. 早起版本

1.1.5.1. CentOS 7 docker-ce

下载 containerd.io https://download.docker.com/linux/centos/7/x86_64/stable/Packages/

		
[root@localhost ~]# yum install https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.13-3.2.el7.x86_64.rpm
		
			

从官方网站获得最新社区版

			
yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum makecache fast
yum -y install docker-ce

systemctl start docker
			
			

测试安装是否成功

			
docker run hello-world
			
			

1.1.5.2. CentOS 6

			
yum install docker-io
service docker start
chkconfig docker on
docker pull centos:latest
docker images centos
			
			

test

			
docker run -i -t centos /bin/bash
			
			

1.1.5.3. Ubuntu

Ubuntu 默认版本

			
$ sudo apt update
$ sudo apt install docker.io
$ sudo ln -sf /usr/bin/docker.io /usr/local/bin/docker
$ sudo sed -i '$acomplete -F _docker docker' /etc/bash_completion.d/docker.io
			
			
			
$ sudo docker run -i -t ubuntu /bin/bash