Home | 简体中文 | 繁体中文 | 杂文 | 知乎专栏 | Github | OSChina 博客 | 云社区 | 云栖社区 | Facebook | Linkedin | 视频教程 | 打赏(Donations) | About
知乎专栏多维度架构

第 163 章 Docker

目录

163.1. Installation
163.1.1. CentOS 6
163.1.2. CentOS 7 docker-ce
163.1.3. CentSO 8
163.1.4. Ubuntu
163.1.5. Ubuntu docker-ce
163.1.6. 测试 Docker
163.1.7. 切换仓库镜像
163.1.8. 重置 Docker
163.2. 配置 Docker
163.2.1. 开启远程访问
163.3. 镜像
163.3.1. 搜索镜像
163.3.2. 获取镜像
163.3.3. 列出本地镜像
163.3.4. 保存和载入镜像
163.3.5. 删除本地镜像
163.3.6. 基于 Dockerfile 创建镜像
163.3.6.1. 创建 Dockerfile 文件
163.3.6.2. 创建镜像
163.3.6.3. 运行镜像
163.3.6.4. 测试 Nginx
163.3.6.5. 提交镜像
163.3.6.6. Dockerfile 语法
163.3.6.6.1. COPY
163.4. 容器
163.4.1. 启动与终止容器
163.4.2. 进入容器
163.4.3. 导出和导入容器
163.4.4. kill
163.4.5. 删除容器
163.5. 仓库
163.5.1. 登陆仓库
163.5.2. 获取镜像
163.5.3. 上传镜像
163.6. 卷管理
163.6.1. 列出卷
163.6.2. 创建卷
163.6.3. 挂在镜像
163.6.4. 检查卷
163.6.5. 删除卷
163.6.6. 销毁所有未使用的卷
163.6.7. 在多个容器间共享卷
163.6.8. 容器绑定本地文件系统
163.6.9. 只读权限
163.7. Swarms
163.7.1. 查看 Swarms 版本
163.7.2. 初始化 Swarms
163.7.3. 显示 join-token
163.7.4. 创建虚拟机
163.7.5. 显示虚拟机列表
163.7.6. 设置管理节点
163.7.7. 环境变量
163.7.8. 切换节点
163.7.9. 启动/停止节点
163.7.10. 离线
163.8. Stack
163.9. 操作系统
163.9.1. /etc/hosts 配置
163.9.2. sysctl
163.9.3. ulimits
163.10. docker 命令
163.10.1. 查看 docker 信息
163.10.2. run
163.10.3. start / stop / restart
163.10.4. ps
163.10.5. top
163.10.6. inspect
163.10.6.1. 获取容器名称
163.10.6.2. 容器镜像名称
163.10.6.3. 获取容器主机名 Hostname
163.10.6.4. 查询 IP 地址
163.10.6.5. 容器日志
163.10.6.6. 获取 json 配置
163.10.6.7. 函数
163.10.6.8. 综合查询
163.10.7. images
163.10.7.1. Getting a new image
163.10.8. logs
163.10.9. history 镜像历史纪录
163.10.10. 重置 Docker
163.10.11. 仓库操作
163.10.11.1. 登陆
163.10.11.2. 注销
163.10.12. build
163.10.13. 网络管理
163.11. docker-compose - Define and run multi-container applications with Docker.
163.11.1. 安装 docker-compose
163.11.1.1. 使用 pip 安装
163.11.1.2. OSCM 安装
163.11.2. 快速入门
163.11.3. 启动/停止
163.11.3.1. 启动
163.11.3.2. 停止
163.11.4. 查看进程
163.11.5. 查看日志
163.11.6. 执行命令
163.11.7. docker-compose.yml
163.11.7.1. 版本号
163.11.7.2. 镜像
163.11.7.2.1. 挂在卷
163.11.7.3. 映射端口的标签
163.11.7.4. 添加 hosts 文件
163.11.7.5. 设置环境变量
163.11.7.6. 临时文件系统
163.11.7.7. 编译 Dockerfile
163.11.7.8. resources 硬件资源分配
163.12. 私有仓库
163.12.1. 搭建私有仓库
163.12.2. 推送镜像到私有仓库
163.12.3. 查询镜像
163.12.4. registry 镜像高级配置
163.12.4.1. 私有仓库认证
163.12.5. registry 接口
163.12.6. Harbor
163.12.6.1.
163.13. Docker Example
163.13.1. Example Java - Spring boot with Docker
163.13.1.1. 获取 CentOS 7 镜像
163.13.1.2. 安装 openjdk
163.13.1.3. Spring boot 包
163.13.1.4. 启动 Spring boot 项目
163.13.1.5. 基于 CentOS 7 制作 spring 镜像
163.13.2. Redis
163.13.2.1. Docker 命令
163.13.2.1.1. 获取 Redis 镜像
163.13.2.1.2. 启动一个 Redis 实例
163.13.2.1.3. 进入 Redis
163.13.2.1.4. 启动一个 Redis 实例并映射 6379 端口
163.13.2.1.5. 维护容器
163.13.2.2. Docker compose
163.13.3. Nginx
163.13.3.1. nginx:latest
163.13.3.2. 安装 Docker Nginx alpine
163.14. Docker FAQ
163.14.1. 检查 Docker 是否可用
163.14.2. Bitnami
163.14.3. 删除 <none> 镜像

https://www.docker.com

163.1. Installation

163.1.1. 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
			
		

163.1.2. CentOS 7 docker-ce

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

			
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
			
		

163.1.3. CentSO 8

		
[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
		
		

163.1.4. 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
			
		

163.1.5. Ubuntu docker-ce

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

		
#!/bin/bash

sudo apt remove docker docker-engine

sudo apt install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common
	
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88

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

apt-cache madison docker-ce
		
		

启动参数配置 /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
		
		
		

163.1.6. 测试 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
		
		

163.1.7. 切换仓库镜像

临时选择镜像, 您可以在 Docker 守护进程启动时传入 --registry-mirror 参数:

		
$ docker --registry-mirror=https://registry.docker-cn.com daemon
		
		

设置默认镜像,修改 /etc/docker/daemon.json 文件,并添加上 registry-mirrors 键值。

		
{
  "registry-mirrors": ["https://registry.docker-cn.com"]
}
		
		

163.1.8. 重置 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)