容器引擎 Docker

什么是 Docker?

Docker™ provides a way to run applications securely isolated in a container, packaged with all its dependencies and libraries.

环境搭建

MacOS

1
$ brew cask install docker

Windows

下载

 从 Toolbox 的 Archive 页面找到 DockerToolbox-19.03.1.exe 并下载

安装

 注意安装组件的时候,选择 Full installation,其他的均使用默认的选项,即可

配置

代理
1
$ vim ~/.docker/config.json
1
2
3
4
5
6
7
8
9
{
"proxies": {
"default": {
"httpProxy": "socks5://127.0.0.1:1080",
"httpsProxy": "socks5://127.0.0.1:1080",
"noProxy": "*.yuzhouwan.com"
}
}
}
镜像源
1
2
3
4
5
https://docker.mirrors.ustc.edu.cn
https://hub-mirror.c.163.com

# 另外,登陆阿里云账号后,可以通过该地址获取到专属的阿里云镜像
# https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
配置文件
1
$ vim ~/.docker/config.json
1
2
3
4
5
{
"registry-mirrors": [
"https://registry.docker-cn.com"
]
}
配置守护程序
1
2
# 增加如下配置项
$ vim /etc/docker/daemon.json
1
2
3
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}
1
2
3
4
5
# 重启 Docker 服务使其生效
$ sudo service docker restart

# 校验是否生效
$ docker system info
1
2
3
4
# ...
Registry Mirrors:
https://registry.docker-cn.com
# ..
命令行指定
1
$ docker-machine create --engine-registry-mirror=http://hub-mirror.c.163.com -d virtualbox kafka-connect
Desktop 设置

(对 Docker™ 的截图)

Boot 2 Docker

  • 打开 cmd 命令行
1
$ cmd
  • 查看帮助文档
1
$ docker-machine -h
  • 创建名为 kafka-connect 的镜像
1
$ docker-machine create --driver virtualbox kafka-connect
  • 进入 kafka-connect 镜像
1
$ docker-machine env kafka-connect
1
2
3
4
5
6
7
SET DOCKER_TLS_VERIFY=1
SET DOCKER_HOST=tcp://127.0.0.1:2376
SET DOCKER_CERT_PATH=C:\Users\BenedictJin\.docker\machine\machines\kafka-connect
SET DOCKER_MACHINE_NAME=kafka-connect
SET COMPOSE_CONVERT_WINDOWS_PATHS=true
REM Run this command to configure your shell:
REM @FOR /f "tokens=*" %i IN ('docker-machine env kafka-connect') DO @%i
  • 拷贝上面输出的最后一行,去掉 REM 注释,并运行
1
$ @FOR /f "tokens=*" %i IN ('docker-machine env kafka-connect') DO @%i
  • 此时,就可以执行 docker 相关的命令了
1
$ docker info
  • 移除 kafka-connect 镜像
1
$ docker-machine rm kafka-connect

常用命令

version

1
$ docker version
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Client: Docker Engine - Community
Version: 19.03.5
API version: 1.40
Go version: go1.12.12
Git commit: 633a0ea
Built: Wed Nov 13 07:22:34 2019
OS/Arch: darwin/amd64
Experimental: false

Server: Docker Engine - Community
Engine:
Version: 19.03.5
API version: 1.40 (minimum version 1.12)
Go version: go1.12.12
Git commit: 633a0ea
Built: Wed Nov 13 07:29:19 2019
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: v1.2.10
GitCommit: b34a5c8af56e510852c35414db4c1f4fa6172339
runc:
Version: 1.0.0-rc8+dev
GitCommit: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
docker-init:
Version: 0.18.0
GitCommit: fec3683

status

1
$ service docker status
1
2
3
4
5
6
7
8
9
10
Redirecting to /bin/systemctl status  docker.service
● docker.service - LSB: start and stop docker
Loaded: loaded (/etc/docker/docker; enabled; vendor preset: disabled)
Active: active (running) since Tue 2018-09-18 09:54:31 CST; 29min ago
Docs: man:systemd-sysv-generator(8)
Process: 14743 ExecStart=/etc/docker/docker start (code=exited, status=0/SUCCESS)
CGroup: /docker.slice/docker.service
├─14907 /opt/ali-iaas/docker/plugins/alinet 127.0.0.1
├─14919 /opt/ali-iaas/docker/plugins/tmpfs 127.0.0.1
└─14933 /opt/ali-iaas/docker/plugins/alilocal 127.0.0.1

logs

1
$ docker logs -f <容器名 or 容器ID>

login

1
$ docker login reg.docker.yuzhouwan.com

cp

1
2
3
4
# 从容器中将文件拷贝出来
$ docker cp <容器名 or 容器ID>:/home/benedict_jin/a.txt /opt/
# 从将文件拷贝到容器里面
$ docker cp /opt/ <容器名 or 容器ID>:/home/benedict_jin/a.txt

run

1
$ docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]

restart

1
$ docker restart <容器名 or 容器ID>

rmi

1
2
3
4
# usage
$ docker rmi [OPTIONS] IMAGE [IMAGE...]
# example
$ docker rmi k8s.gcr.io/coredns:1.3.1

rm

1
$ docker rm <容器名 or 容器ID>

实用技巧

Engine API

1
2
3
4
5
# 获取到 unhealthy 状态的容器
$ curl --unix-socket /var/run/docker.sock "http://localhost/containers/json?filters=\{\"health\":\[\"unhealthy\"\]\}"

# 获取到容器启动的时间戳
$ curl --unix-socket /var/run/docker.sock "http://localhost/containers/<容器 ID>/json" | jq '.State.StartedAt' | date +%s

踩到的坑

No default Boot2Docker ISO found locally

描述

1
$ docker-machine create --driver virtualbox kafka-connect
1
2
3
Running pre-create checks...
(dataagg) No default Boot2Docker ISO found locally, downloading the latest release...
Error with pre-create check: "Get https://api.github.com/repos/boot2docker/boot2docker/releases/latest: dial tcp 127.0.0.1:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond."

解决

 到 boot2docker 的 release 页面下载最新的 boot2docker.iso(当前最新版本为 v17.12.1-ce),并放到 C:\Users\BenedictJin\.docker\machine\cache 目录下,再次运行 docker-machine create xxx 命令即可

Docker.raw 单文件达到 64G

描述

1
$ ll -h /Users/benedictjin/Library/Containers/com.docker.docker/Data/vms/0/data/Docker.raw
1
-rw-r--r--  1 benedictjin  staff    64G  3 23 22:18 /Users/benedictjin/Library/Containers/com.docker.docker/Data/vms/0/data/Docker.raw

解决

 进入 Preferences 配置页面,选择 Disk 配置页,调整预分配磁盘大小,点击 Apply 按钮以应用修改

资料

Doc

Github

Resource

欢迎加入我们的技术群,一起交流学习

群名称 群号
人工智能(高级)
人工智能(进阶)
BigData
算法

欢迎关注我的其它发布渠道