Docker 教程

1,272 阅读21分钟

Docker 介绍

开源的应用容器引擎 基于 Go 语言 并遵从 Apache2.0 协议

开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器

容器是完全使用沙箱机制,相互之间不会有任何接口

容器性能开销极低

CE(Community Edition: 社区版) 和 EE(Enterprise Edition: 企业版)

Docker的应用场景

Web 应用的自动化打包和发布

自动化测试和持续集成、发布。

Docker 的优点

快速,一致地交付您的应用程序

持续集成和持续交付(CI / CD)工作流程

响应式部署和扩展 在同一硬件上运行更多工作负载

Docker 架构

三个基本概念:

镜像(Image)  类
容器(Container)  实例
仓库(Repository)

客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器

使用 Docker仓库 安装 Docker Engine-Community

设置仓库

安装 yum-config-manager

[root@master ~]# yum install -y yum-utils \
>   device-mapper-persistent-data \
>   lvm2

添加 阿里云 Docker 仓库

[root@master ~]# yum-config-manager \
>     --add-repo \
>     http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
已加载插件:fastestmirror
adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo

安装 Docker Engine-Community

yum install docker-ce docker-ce-cli containerd.io -y

列出并排序您存储库中可用的版本

yum list docker-ce --showduplicates | sort -r

启动 docker

systemctl start docker

运行 hello-world 映像来验证是否正确安装

docker run hello-world

镜像加速

因为安装时选择了阿里云镜像,默认已配置,若无,按以下方法配置:

vim /etc/docker/daemon.json
# 写入 {"registry-mirrors":["https://reg-mirror.qiniu.com/"]}
systemctl daemon-reload
systemctl restart docker

Docker 使用

Hello World

[root@master docker]# docker run ubuntu:15.10 /bin/echo "Hello world"
Hello world

参数解析:

docker: Docker 的二进制执行文件。

run: 与前面的 docker 组合来运行一个容器。

ubuntu:15.10 指定要运行的镜像,本地主机————镜像仓库 Docker Hub

/bin/echo "Hello world": 在启动的容器里执行的命令

运行交互式的容器

[root@master docker]# docker run -i -t ubuntu:15.10 /bin/bash
root@1c52c1d5eff1:/#

参数解析:

-t: 在新容器内指定一个伪终端或终端。

-i: 允许你对容器内的标准输入 (STDIN) 进行交互。

运行 exit 命令或者使用 CTRL+D 来退出容器

启动容器(后台模式)

[root@master docker]# docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
3b6b2877b71748220b8acca988d04821c7b0f0ff16c664200d6b272d30f772a7

容器 ID,对每个容器来说都是唯一的

通过容器 ID 来查看对应的容器发生了什么

docker ps 查看正在运行的容器

[root@master docker]#  docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS              PORTS     NAMES
3b6b2877b717   ubuntu:15.10   "/bin/sh -c 'while t…"   About a minute ago   Up About a minute             thirsty_mahavira

输出详情:

CONTAINER ID: 容器 ID。
IMAGE: 使用的镜像。
COMMAND: 启动容器时运行的命令。
CREATED: 容器的创建时间。
STATUS: 容器状态。
状态有7种:
	created(已创建)
	restarting(重启中)
	running  Up(运行中)
	removing(迁移中)
	paused(暂停)
	exited(停止)
	dead(死亡)
PORTS: 容器的端口信息和使用的连接类型(tcp\udp)。
NAMES: 自动分配的容器名称。

docker logs 查看容器内的标准输出

[root@master docker]# docker logs 3b6b2877b717
hello world
hello world
hello world
...

docker stop 停止容器

[root@master docker]# docker stop 3b6b2877b717
3b6b2877b717

客户端

直接输入 docker 命令来查看到 Docker 客户端的所有命令选项

[root@master docker]# docker

Usage:  docker [OPTIONS] COMMAND
...

docker command --help 更深入的了解指定的 Docker 命令使用方法

 [root@master docker]#  docker stats --help

Usage:  docker stats [OPTIONS] [CONTAINER...]

Display a live stream of container(s) resource usage statistics

Options:
  -a, --all             Show all containers (default shows just running)
      --format string   Pretty-print images using a Go template
      --no-stream       Disable streaming stats and only pull the first result
      --no-trunc        Do not truncate output

容器使用

获取镜像

[root@master docker]# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
Digest: sha256:c95a8e48bf88e9849f3e0f723d9f49fa12c5a00cfc6e60d2bc99d87555295e4c
Status: Image is up to date for ubuntu:latest
docker.io/library/ubuntu:latest

启动容器

[root@master docker]# docker run -it ubuntu /bin/bash
root@b9f8b5d3ce5f:/#

启动已停止运行的容器

docker ps -a 查看所有的容器

 [root@master docker]# docker ps -a
CONTAINER ID   IMAGE             COMMAND                  CREATED              STATUS                        PORTS                     NAMES
b9f8b5d3ce5f   ubuntu            "/bin/bash"              About a minute ago   Exited (127) 3 seconds ago                              friendly_satoshi
...

docker start 启动一个已停止的容器

[root@master docker]# docker start b9f8b5d3ce5f
b9f8b5d3ce5f

后台运行

加了 -d 参数默认不会进入容器,想要进入容器需要使用指令 docker exec

[root@master docker]# docker run -itd --name ubuntu-test ubuntu /bin/bash
6674e565ca1e4d720269045c2c166286cfe16c2b6649268edc680e4fb8b1943f

停止和重启容器

docker stop <容器 ID> 停止容器

[root@master docker]#  docker stop b9f8b5d3ce5f
b9f8b5d3ce5f

docker restart <容器 ID> 重启容器

[root@master docker]# docker restart b9f8b5d3ce5f
b9f8b5d3ce5f

进入容器

attach 命令

从容器退出,会导致容器的停止

[root@master docker]#  docker attach b9f8b5d3ce5f
root@b9f8b5d3ce5f:/#

exec 命令

从容器退出,容器不会停止

[root@master docker]# docker exec -it b9f8b5d3ce5f  /bin/bash
root@b9f8b5d3ce5f:/#

导出容器

 docker export b9f8b5d3ce5f  > ubuntu.tar

导入容器

[root@master docker]# cat ubuntu.tar | docker import - test/ubuntu:v1
sha256:10f9c3548de8204d30181945984d7ad16845cc0db6d3df79886b084bcd40eace
[root@master docker]# docker images
REPOSITORY        TAG       IMAGE ID       CREATED              SIZE
test/ubuntu       v1        10f9c3548de8   About a minute ago   72.9MB

删除容器

删除<容器 ID>容器

[root@master docker]# docker rm -f b9f8b5d3ce5f
b9f8b5d3ce5f

清理处于终止状态的容器

[root@master docker]# docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
3b6b2877b71748220b8acca988d04821c7b0f0ff16c664200d6b272d30f772a7
1c52c1d5eff1c84f57bf5490d81049110ad8bf099ca1c4df8e336e46b6be65f5
9b59d67dd00c711d5ff14a4840acffa793a13f04f6b07ace8652727b5a9c387f
201a6a9c7783193ab4e23174b9c7ca2ae5b912ce6df706022e43916c42fd07c0
d6bd823aa49bb8c25e8453b321f0cb9a6087b01c3818e99670d7653e00aa761e
26dd7b28c118c021017f735384d6e426f08cdd18eb3d3c44b10faf28e9d9a479
233d908ca0d7136cf8b440ac8b0cfb08ef3703e23a93f62d7289557dac19f25a
0ea225696075295c73e7d4a154af5feb2c846f6547aa834fd2b47c970bfa9dfb
067294d64005db7ecb2f315fbe0479d306257f29ca5f4d1ab57be4b427539d76
f6f1c1233cc805f23a1ebc1ee9e08a7452986bb178819d84f036ee81a3a0250f
5c6e8b26c3aace09b90ec3940a813a19d7ec2649fefd366ebb2a7b54fbb09236
0813d0b8cc6e5274867b0794cf9c477154581bdc49cc0d581f9586a5e7db08ca
63088d7b238d363b22db7641f931dc92e5ebca9b65a48b89a0221b0583cc5177

Total reclaimed space: 27.81MB

运行一个 web 应用

构建应用
docker pull training/webapp
# 载入镜像
[root@master docker]# docker run -d -P training/webapp python app.py
9a79d72088a887e4f8d9895d2f339e670228cabf8892a54e1fd19919819c6d8a
# -P:将容器内部使用的网络端口随机映射到我们使用的主机上
[root@master docker]#  docker ps
CONTAINER ID   IMAGE             COMMAND           CREATED          STATUS          PORTS                     NAMES
9a79d72088a8   training/webapp   "python app.py"   2 minutes ago    Up 2 minutes    0.0.0.0:49153->5000/tcp   nostalgic_rhodes
# 查看 WEB 应用容器
 docker run -d -p 5000:5000 training/webapp python app.py
 #  -p 参数来设置不一样的端口
[root@master docker]# docker run -d -p 5000:5000 training/webapp python app.py
e4c3e8ef60343c18d957bd17b0759e968ead423fa9fa1c648c6bee0d69db5692
[root@master docker]#  docker ps
CONTAINER ID   IMAGE             COMMAND           CREATED             STATUS             PORTS                     NAMES
e4c3e8ef6034   training/webapp   "python app.py"   8 seconds ago       Up 6 seconds       0.0.0.0:5000->5000/tcp    modest_chaum

使用浏览器访问

查看网络端口的快捷方式
[root@master docker]#  docker port e4c3e8ef6034
5000/tcp -> 0.0.0.0:5000
查看 WEB 应用程序日志
[root@master docker]# docker logs -f e4c3e8ef6034
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
192.168.108.135 - - [18/Dec/2020 09:31:03] "GET / HTTP/1.1" 200 -
192.168.108.135 - - [18/Dec/2020 09:31:03] "GET /favicon.ico HTTP/1.1" 404 -
192.168.108.135 - - [18/Dec/2020 09:36:16] "GET / HTTP/1.1" 200 -
192.168.108.135 - - [18/Dec/2020 09:36:19] "GET / HTTP/1.1" 200 -
192.168.108.135 - - [18/Dec/2020 09:36:29] "GET / HTTP/1.1" 200 -
查看WEB应用程序容器的进程
[root@master docker]# docker top e4c3e8ef6034
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                69133               69109               0                   17:30               ?                   00:00:00            python app.py
检查 WEB 应用程序

返回一个 JSON 文件记录着 Docker 容器的配置和状态信息

[root@master docker]# docker inspect e4c3e8ef6034
[
    {
        "Id": "e4c3e8ef60343c18d957bd17b0759e968ead423fa9fa1c648c6bee0d69db5692",
        "Created": "2020-12-18T09:30:16.931960826Z",
        "Path": "python",
        "Args": [
            "app.py"
...
停止 WEB 应用容器
[root@master docker]# docker stop e4c3e8ef6034
e4c3e8ef6034
重启WEB应用容器
[root@master docker]# docker start e4c3e8ef6034
e4c3e8ef6034
查询最后一次创建的容器
[root@master docker]#  docker ps -l
CONTAINER ID   IMAGE             COMMAND           CREATED          STATUS          PORTS                    NAMES
e4c3e8ef6034   training/webapp   "python app.py"   14 minutes ago   Up 29 seconds   0.0.0.0:5000->5000/tcp   modest_chaum
移除WEB应用容器

删除容器时,容器必须是停止状态,否则会报如下错误

[root@master docker]# docker rm  e4c3e8ef6034
Error response from daemon: You cannot remove a running container e4c3e8ef60343c18d957bd17b0759e968ead423fa9fa1c648c6bee0d69db5692. Stop the container before attempting removal or force remove

移除WEB应用容器

[root@master docker]# docker stop e4c3e8ef6034
e4c3e8ef6034
[root@master docker]# docker rm  e4c3e8ef6034
e4c3e8ef6034

镜像使用

列出本地主机上的镜像
 [root@master docker]# docker images
REPOSITORY        TAG       IMAGE ID       CREATED             SIZE
test/ubuntu       v1        10f9c3548de8   About an hour ago   72.9MB

选项说明:

REPOSITORY:表示镜像的仓库源
TAG:镜像的标签
IMAGE ID:镜像ID
CREATED:镜像创建时间
SIZE:镜像大小

使用版本为15.10的ubuntu系统镜像来运行容器

docker run -t -i ubuntu:15.10 /bin/bash
获取一个新的镜像
 [root@master docker]#  docker pull ubuntu:13.10
13.10: Pulling from library/ubuntu
Image docker.io/library/ubuntu:13.10 uses outdated schema1 manifest format. Please upgrade to a schema2 image for better future compatibility. More information at https://docs.docker.com/registry/spec/deprecated-schema-v1/
a3ed95caeb02: Pull complete
0d8710fc57fd: Pull complete
5037c5cd623d: Pull complete
83b53423b49f: Pull complete
e9e8bd3b94ab: Pull complete
7db00e6b6e5e: Pull complete
Digest: sha256:403105e61e2d540187da20d837b6a6e92efc3eb4337da9c04c191fb5e28c44dc
Status: Downloaded newer image for ubuntu:13.10
docker.io/library/ubuntu:13.10
查找镜像
[root@master docker]#  docker search httpd
NAME                                    DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
httpd                                   The Apache HTTP Server Project                  3288      [OK]
centos/httpd-24-centos7                 Platform for running Apache httpd 2.4 or bui…   36
centos/httpd                                                                            33                   [OK]
arm32v7/httpd                           The Apache HTTP Server Project                  9
arm64v8/httpd                           The Apache HTTP Server Project                  6
polinux/httpd-php                       Apache with PHP in Docker (Supervisor, CentO…   4                    [OK]
salim1983hoop/httpd24                   Dockerfile running apache config                2                    [OK]
publici/httpd                           httpd:latest                                    1                    [OK]
solsson/httpd-openidc                   mod_auth_openidc on official httpd image, ve…   1                    [OK]
inanimate/httpd-ssl                     A play container with httpd, ssl enabled, an…   1                    [OK]
hypoport/httpd-cgi                      httpd-cgi                                       1                    [OK]
dariko/httpd-rproxy-ldap                Apache httpd reverse proxy with LDAP authent…   1                    [OK]
clearlinux/httpd                        httpd HyperText Transfer Protocol (HTTP) ser…   1
lead4good/httpd-fpm                     httpd server which connects via fcgi proxy h…   1                    [OK]
jonathanheilmann/httpd-alpine-rewrite   httpd:alpine with enabled mod_rewrite           1                    [OK]
appertly/httpd                          Customized Apache HTTPD that uses a PHP-FPM …   0                    [OK]
amd64/httpd                             The Apache HTTP Server Project                  0
manasip/httpd                                                                           0
trollin/httpd                                                                           0
interlutions/httpd                      httpd docker image with debian-based config …   0                    [OK]
e2eteam/httpd                                                                           0
manageiq/httpd_configmap_generator      Httpd Configmap Generator                       0                    [OK]
itsziget/httpd24                        Extended HTTPD Docker image based on the off…   0                    [OK]
manageiq/httpd                          Container with httpd, built on CentOS for Ma…   0                    [OK]
dockerpinata/httpd                                                                      0

选项说明:

NAME: 镜像仓库源的名称
DESCRIPTION: 镜像的描述
OFFICIAL: 是否 docker 官方发布
stars: 类似 Github 里面的 star,表示点赞、喜欢的意思。
AUTOMATED: 自动构建。
拖取镜像
[root@master docker]# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
6ec7b7d162b2: Already exists
17e233bac21e: Pull complete
130aad5bf43a: Pull complete
81d0a34533d4: Pull complete
da240d12a8a4: Pull complete
Digest: sha256:a3a2886ec250194804974932eaf4a4ba2b77c4e7d551ddb63b01068bf70f4120
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest
删除镜像
[root@master docker]#  docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:1a523af650137b8accdaed439c17d684df61ee4d74feac151b5b337bd29e7eec
Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b
Deleted: sha256:9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63
创建镜像

从已经创建的容器中更新镜像,并且提交这个镜像

[root@master docker]#  docker commit -m="has update" -a="runoob" c9ce132a0acd  runoob/ubuntu:v2
sha256:62ba08755fc78e28a11f6ebc69ead6974c514a88cf40a8b764d27632f6bcb673

参数说明:

-m: 提交的描述信息 
-a: 指定镜像作者
c9ce132a0acd 容器 ID
runoob/ubuntu:v2   指定要创建的目标镜像名

使用 Dockerfile 指令来创建一个新的镜像

[root@master docker]# vim Dockerfile
[root@master docker]# cat Dockerfile
FROM    centos:6.7
MAINTAINER      Fisher "fisher@sudops.com"

RUN     /bin/echo 'root:123456' |chpasswd
RUN     useradd runoob
RUN     /bin/echo 'runoob:123456' |chpasswd
RUN     /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
EXPOSE  22
EXPOSE  80
CMD     /usr/sbin/sshd -D

[root@master docker]#  docker build -t runoob/centos:6.7 .
Sending build context to Docker daemon  75.28MB
Step 1/9 : FROM    centos:6.7
 ---> 9f1de3c6ad53
Step 2/9 : MAINTAINER      Fisher "fisher@sudops.com"
 ---> Using cache
 ---> f14df0b17382
Step 3/9 : RUN     /bin/echo 'root:123456' |chpasswd
 ---> Running in 23cbe37429ee
Removing intermediate container 23cbe37429ee
 ---> 1302fdb3cef6
Step 4/9 : RUN     useradd runoob
 ---> Running in aeb861f2cd4a
Removing intermediate container aeb861f2cd4a
 ---> 3ad14f808f16
Step 5/9 : RUN     /bin/echo 'runoob:123456' |chpasswd
 ---> Running in a342ac1edf83
Removing intermediate container a342ac1edf83
 ---> c728d6cdc20e
Step 6/9 : RUN     /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
 ---> Running in da2f2c973c64
Removing intermediate container da2f2c973c64
 ---> ba76b9c9d0be
Step 7/9 : EXPOSE  22
 ---> Running in b5b8758aef68
Removing intermediate container b5b8758aef68
 ---> de07a8de1322
Step 8/9 : EXPOSE  80
 ---> Running in 0a38429cc7f7
Removing intermediate container 0a38429cc7f7
 ---> 1d6a78fc769b
Step 9/9 : CMD     /usr/sbin/sshd -D
 ---> Running in f0f6a7734d88
Removing intermediate container f0f6a7734d88
 ---> 44f4bb339cfa
Successfully built 44f4bb339cfa
Successfully tagged runoob/centos:6.7

FROM,指定使用哪个镜像源

RUN 指令告诉docker 在镜像内执行命令

参数说明:

-t :指定要创建的目标镜像名
. :Dockerfile 文件所在目录,可以指定Dockerfile 的绝对路径
设置镜像标签
docker tag 44f4bb339cfa  runoob/centos:dev

容器连接

网络端口映射

通过 -P 或 -p 参数来指定端口映射

-P :是容器内部端口随机映射到主机的高端口

[root@master docker]# docker run -d -P training/webapp python app.py
85f780a4c7683a67811f3e8fefc09422b53acfaca38705a0f81d2135a474783e
[root@master docker]#  docker ps
CONTAINER ID   IMAGE             COMMAND           CREATED          STATUS          PORTS                     NAMES
85f780a4c768   training/webapp   "python app.py"   14 seconds ago   Up 14 seconds   0.0.0.0:49154->5000/tcp   mystifying_goldstine

-p : 是容器内部端口绑定到指定的主机端口。

[root@master docker]# docker run -d -p 5000:5000 training/webapp python app.py
ece3ef938f534cb7a344f9e66f92d02a42234d85ab74440f1c0228af45672695
[root@master docker]# docker ps
CONTAINER ID   IMAGE             COMMAND           CREATED          STATUS          PORTS                     NAMES
ece3ef938f53   training/webapp   "python app.py"   13 seconds ago   Up 13 seconds   0.0.0.0:5000->5000/tcp    happy_lamarr

指定容器绑定的网络地址

[root@master docker]# docker run -d -p 127.0.0.1:5001:5000 training/webapp python app.py
79cf97425fe71708e424dc4d094e8a0484a93273a38c44351a388c8d14e2c704
[root@master docker]# docker ps
CONTAINER ID   IMAGE             COMMAND           CREATED              STATUS              PORTS                      NAMES
79cf97425fe7   training/webapp   "python app.py"   About a minute ago   Up About a minute   127.0.0.1:5001->5000/tcp   sharp_chaum

绑定 UDP 端口,可以在端口后面加上 /udp

[root@master docker]#  docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py
231e3abd72087afa5b6b6268246800a6df514304f3c202ee0dce86fd48158bf9
[root@master docker]# docker ps
CONTAINER ID   IMAGE             COMMAND           CREATED          STATUS          PORTS                                NAMES
231e3abd7208   training/webapp   "python app.py"   14 seconds ago   Up 13 seconds   5000/tcp, 127.0.0.1:5000->5000/udp   agitated_jemison

快捷地查看端口的绑定情况

[root@master docker]# docker port  79cf97425fe7
5000/tcp -> 127.0.0.1:5001

容器互联

容器命名

--name 标识来命名容器

 [root@master docker]# docker run -d -P --name runoob training/webapp python app.py
2738c8caec5779d2e25d4cf63d5570d4abf29ae6e8f67c742f0e08bc6c3c4fc7
[root@master docker]# docker ps -l
CONTAINER ID   IMAGE             COMMAND           CREATED          STATUS          PORTS                     NAMES
2738c8caec57   training/webapp   "python app.py"   12 seconds ago   Up 12 seconds   0.0.0.0:49155->5000/tcp   runoob
新建网络
[root@master docker]# docker network create -d bridge devops
0c566304756424b2d1a5a8c16e0161ab9d318284a69c7e725b6062dd291f54ce

参数说明: -d:参数指定 Docker 网络类型,有 bridge、overlay

连接容器
[root@master docker]# docker network create -d bridge devops
0c566304756424b2d1a5a8c16e0161ab9d318284a69c7e725b6062dd291f54ce
[root@master docker]#  docker run -itd --name test1 --network devops ubuntu /bin/bash
b17703c752b7ca7586e9dabf4de5bc70b9f04b6062385cb07ac1eef96bad484c
[root@master docker]#  docker run -itd --name test2 --network devops ubuntu /bin/bash
1f7b62a4dd45687233eafb6cde6f1f2ce9d57439bcd2c00da41740f6c8ae3914

配置 DNS

设置全部容器的 DNS

[root@master docker]# vim daemon.json
[root@master docker]# cat daemon.json
{"registry-mirrors":["https://reg-mirror.qiniu.com/"]}

{
  "dns" : [
    "114.114.114.114",
    "8.8.8.8"
  ]
}

配置完,需要重启 docker 才能生效

查看容器的 DNS 是否生效

[root@master init.d]# docker run -it --rm  ubuntu  cat etc/resolv.conf

nameserver 192.168.108.1

可见配置失败了,原因配置文件写的有误。

[root@master docker]# vim daemon.json
[root@master docker]# cat daemon.json
{
"registry-mirrors":["https://reg-mirror.qiniu.com/"]
  "dns" : [
    "114.114.114.114",
    "8.8.8.8"
  ]
}

配置完,重启 docker,有报错:

[root@master docker]# systemctl restart docker
Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.
[root@master docker]# systemctl status docker.service
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: failed (Result: start-limit) since 一 2020-12-21 11:56:06 CST; 10s ago
     Docs: https://docs.docker.com
  Process: 6216 ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock (code=exited, status=1/FAILURE)
 Main PID: 6216 (code=exited, status=1/FAILURE)

12月 21 11:56:04 master systemd[1]: Failed to start Docker Application Container Engine.
12月 21 11:56:04 master systemd[1]: Unit docker.service entered failed state.
12月 21 11:56:04 master systemd[1]: docker.service failed.
12月 21 11:56:06 master systemd[1]: docker.service holdoff time over, scheduling restart.
12月 21 11:56:06 master systemd[1]: Stopped Docker Application Container Engine.
12月 21 11:56:06 master systemd[1]: start request repeated too quickly for docker.service
12月 21 11:56:06 master systemd[1]: Failed to start Docker Application Container Engine.
12月 21 11:56:06 master systemd[1]: Unit docker.service entered failed state.
12月 21 11:56:06 master systemd[1]: docker.service failed.

原因 daemon.json 有误,少了逗号。

[root@master docker]# cat daemon.json
{
"registry-mirrors":["https://reg-mirror.qiniu.com/"], # 少逗号地方
  "dns" : [
    "114.114.114.114",
    "8.8.8.8"
  ]
}

重启 docker 后生效

[root@master docker]# systemctl restart docker
[root@master docker]# docker run -it --rm  ubuntu  cat etc/resolv.conf
nameserver 114.114.114.114
nameserver 8.8.8.8

手动指定容器的配置

[root@master init.d]# docker run -it --rm -h host_ubuntu  --dns=114.114.114.114 --dns-search=test.com ubuntu
root@host_ubuntu:/#

--rm:容器退出时自动清理容器内部的文件系统。

-h HOSTNAME 或者 --hostname=HOSTNAME: 设定容器的主机名,它会被写到容器内的 /etc/hostname 和 /etc/hosts。

--dns=IP_ADDRESS: 添加 DNS 服务器到容器的 /etc/resolv.conf 中,让容器用这个服务器来解析所有不在 /etc/hosts 中的主机名

--dns-search=DOMAIN: 设定容器的搜索域,当设定搜索域为 .example.com 时,在搜索一个名为 host 的主机时,DNS 不仅搜索 host,还会搜索 host.example.com。

仓库管理

Docker 官方维护了一个公共仓库 Docker Hub。

hub.docker.com

注册

登录和退出

[root@master docker]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: cpyaxjq
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@master docker]# docker logout
Removing login credentials for https://index.docker.io/v1/

查找官方仓库中的镜像

[root@master docker]# docker search ubuntu
NAME                                                      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu                                                    Ubuntu is a Debian-based Linux operating sys…   11650     [OK]
dorowu/ubuntu-desktop-lxde-vnc                            Docker image to provide HTML5 VNC interface …   481                  [OK]
websphere-liberty                                         WebSphere Liberty multi-architecture images …   265       [OK]
rastasheep/ubuntu-sshd                                    Dockerized SSH service, built on top of offi…   251                  [OK]
consol/ubuntu-xfce-vnc                                    Ubuntu container with "headless" VNC session…   228                  [OK]
ubuntu-upstart                                            Upstart is an event-based replacement for th109       [OK]
neurodebian                                               NeuroDebian provides neuroscience research s…   77        [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5      ubuntu-16-nginx-php-phpmyadmin-mysql-5          50                   [OK]
ubuntu-debootstrap                                        debootstrap --variant=minbase --components=m…   44        [OK]
open-liberty                                              Open Liberty multi-architecture images based…   40        [OK]
nuagebec/ubuntu                                           Simple always updated Ubuntu docker images w…   24                   [OK]
i386/ubuntu                                               Ubuntu is a Debian-based Linux operating sys…   24
1and1internet/ubuntu-16-apache-php-5.6                    ubuntu-16-apache-php-5.6                        14                   [OK]
1and1internet/ubuntu-16-apache-php-7.0                    ubuntu-16-apache-php-7.0                        13                   [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mariadb-10   ubuntu-16-nginx-php-phpmyadmin-mariadb-10       11                   [OK]
1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4         ubuntu-16-nginx-php-5.6-wordpress-4             7                    [OK]
1and1internet/ubuntu-16-apache-php-7.1                    ubuntu-16-apache-php-7.1                        6                    [OK]
darksheer/ubuntu                                          Base Ubuntu Image -- Updated hourly             5                    [OK]
pivotaldata/ubuntu                                        A quick freshening-up of the base Ubuntu doc…   4
1and1internet/ubuntu-16-nginx-php-7.0                     ubuntu-16-nginx-php-7.0                         4                    [OK]
pivotaldata/ubuntu16.04-build                             Ubuntu 16.04 image for GPDB compilation         2
smartentry/ubuntu                                         ubuntu with smartentry                          1                    [OK]
1and1internet/ubuntu-16-php-7.1                           ubuntu-16-php-7.1                               1                    [OK]
pivotaldata/ubuntu-gpdb-dev                               Ubuntu images for GPDB development              1
pivotaldata/ubuntu16.04-test                              Ubuntu 16.04 image for GPDB testing             0

docker pull 将官方 ubuntu 镜像下载到本地

[root@master docker]#  docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
Digest: sha256:c95a8e48bf88e9849f3e0f723d9f49fa12c5a00cfc6e60d2bc99d87555295e4c
Status: Image is up to date for ubuntu:latest
docker.io/library/ubuntu:latest

推送镜像

[root@master docker]# docker image ls
REPOSITORY        TAG       IMAGE ID       CREATED         SIZE
ubuntu            latest    f643c72bc252   3 weeks ago     72.9MB
[root@master docker]# docker tag ubuntu:latest cpyaxjq/ubuntu:18.04
[root@master docker]#  docker push cpyaxjq/ubuntu:18.04
The push refers to repository [docker.io/cpyaxjq/ubuntu]

Dockerfile

Dockerfile 是一个用来构建镜像的文本文件,文本内容包含了一条条构建镜像所需的指令和说明。

FROM 
RUN   在 docker build
COPY
ADD
CMD docker run 时运行
ENTRYPOINT
ENV
ARG
VOLUME
EXPOSE
WORKDIR
USER
HEALTHCHECK	
ONBUILD

Compose

Compose 是用于定义和运行多容器 Docker 应用程序的工具

Compose 使用的三个步骤:

  1. 使用 Dockerfile 定义应用程序的环境。

  2. 使用 docker-compose.yml 定义构成应用程序的服务,这样它们可以在隔离环境中一起运行。

  3. 最后,执行 docker-compose up 命令来启动并运行整个应用程序。

Compose 安装

步骤

[root@master src]# curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   651  100   651    0     0    667      0 --:--:-- --:--:-- --:--:--   667
 30 15.4M   30 4878k    0     0   3935      0  1:08:28  0:21:09  0:47:19     0
curl: (56) TCP connection reset by peer
[root@master src]# chmod +x /usr/local/bin/docker-compose
[root@master src]# ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
[root@master src]# docker-compose --version
[17111] Cannot open self /usr/local/bin/docker-compose or archive /usr/local/bin/docker-compose.pkg

安装时发现有报错,报错是因为网络原因下载不完整导致。

解决方法: 使用wget下载。