RedHat/CentOS8【Docker】镜像制作编排和容器部署

Docker (Community Edition: 社区版) 是一个开源(遵循 Apache 2.0 协议)的应用容器引擎。Docker 可以打包应用以及依赖包到一个轻量级、可移植的 镜像 中,然后通过镜像创建 容器 并发布到任何流行 Linux 系统的主机上,实现虚拟化。镜像 存储在远程仓库或者本地仓库中便于管理和使用;容器 是完全使用沙箱机制,性能开销极低,相互之间不会有任何接口。

本方案基于 CentOS8 系统设计,建议在 RedHat/CentOS 系统中使用。

目录

1.技术评估

2.Docker 的安装和配置

3.Docker 使用阿里云镜像仓库
-- 3.1.注册阿里云账号并开通容器服务
-- 3.2.使用阿里云仓库加速器
-- 3.3.使用阿里云私有仓库

4.Harbor 私有化镜像仓库
-- 4.1.Harbor 的安装和配置
-- 4.2.Docker 使用 Harbor 私有仓库

5.Docker 镜像制作

6.Docker 镜像打包、导入和运行

7.Docker Compose 容器编排
-- 7.1.Docker Compose 的安装和配置
-- 7.2.Docker Compose 容器编排

附录一 相关技术指南目录

附录二 安装 Xfce 桌面环境和 VNC 远程桌面

附录三 扩展 CentOS 基础镜像


1.技术评估

1、Docker 和 VM(虚拟机)的区别

VM 是一个运行在宿主机之上的完整的操作系统,VM 虚拟机本身会占用较多的 CPU、内存、硬盘等资源,在 VM 虚拟机上部署的应用程序还需继续占用更多资源。

Docker 只包含应用程序以及依赖库,基于容器运行在宿主机上,并处于一个隔离的环境中,这使得Docker 更加轻量高效,启动容器只需几秒钟之内完成,因此可以轻易的应用到构建标准化的应用中。但 Docker 隔离效果不如 VM,主要体现在:共享宿主机操作系统的一些基础库、网络配置功能相对简单(主要以桥接方式为主)、查看日志不够方便等问题。

Docker 和 VM 的虚拟化架构如下图:

Docker 和 VM 虚拟化架构图

2、Docker 的特点

1)主机资源利用率高,Docker 容器直接在宿主机的操作系统内核上运行,不需要硬件虚拟化;

2)启动时间快,Docker 容器秒级启动,优于 VM 虚拟机;

3)运行环境一致性,Docker 将应用同依赖环境打包成镜像,通过镜像创建和发布容器到系统上,各系统部署的应用具有很强的一致性;

4)部署简单,Docker 的安装和容器部署过程简单,标准化程度高,学习成本低。

3、Docker 的核心组件

Docker 组件关系图

1)Docker 主机【Host】: 一个物理机或虚拟机,用于运行 Docker 服务进程和容器。

2)Docker 服务端【Server】: Docker 守护进程, 运行 Docker 容器。

3)Docker 客户端【Client】: 客户端使用命令或其他工具调用 Docker API。

4)Docker 镜像【Images】: 镜像是 Docker 为创建容器使用的模板。

5)Docker 仓库【Registry】: 保存镜像的仓库,有远程仓库和本地仓库。一个 Registry 中可以包含多个仓库 【Repository】,每个仓库可以包含多个标签【Tag】,每个标签对应一个镜像。通常,一个仓库会包含同一个软件不同版本的镜像,而标签就常用于区分软件的版本,可以通过 "<仓库名>:<标签>" 的格式来指定特定版本的软件所对应的镜像,如果不给出标签,将以 "latest" 作为默认标签。国外官方的远程仓库地址是【https://hub.docker.com/】,国内阿里云提供的远程仓库地址是【https://cr.console.aliyun.com/cn-hangzhou/images

6)Docker 容器【Container】: 容器是从镜像生成对外提供服务的一个或一组独立运行的虚拟化服务。

7)Docker 宿主机【Machine】:宿主机是一个简化 Docker 安装的命令行工具,通过一个简单的命令行即可在相应的平台上安装 Docker 。

4、Docker 的应用场景

1)提供一次性的环境。例如在本地测试他人的软件、持续集成的时候提供单元测试和构建的环境。

2)提供弹性的云服务。因为 Docker 容器可以随开随关,很适合动态扩容和缩容。

3)组建微服务架构。通过多个容器,在一台主机上可以运行多个服务,因此能够在本机就可以模拟出微服务架构。


2.Docker 的安装和配置

1、从阿里云下载并安装 Docker 的 Yum 源。

1)打开 Docker 的阿里云 Yum 源网站【https://mirrors.aliyun.com/docker-ce/linux/centos/】,下载 Docker 阿里云 Yum 源文件到用户主目录中。

Docker 阿里云 Yum 源

2)将下载的 Yum 源文件拷贝至 "/etc/yum.repos.d" 目录下。

[centos@host ~ ]$ sudo mv ~/docker-ce.repo /etc/yum.repos.d

3)更新 Yum 源。

[centos@host ~]$ sudo dnf clean all
[centos@host ~]$ sudo dnf makecache
Docker CE Stable - x86_64                        53 kB/s |  25 kB     00:00

2、人工从阿里云下载并安装 Docker 的依赖软件 "containerd.io" 最高版本(Docker 的 Yum 源目前更新到 CentOS7 版本,还没有对 CentOS8 进行专门适配,因此需要人工处理此步骤,伴随 Docker 的 Yum 不断更新,以后可能不需要此步骤)。

1)打开 Docker 的 Yum 源文件,找到 Yum 源的互联网位置。

使用文本编辑器打开 "/etc/yum.repos.d/docker-ce.repo" 文件:

[centos@host ~]$ sudo gedit /etc/yum.repos.d/docker-ce.repo

从配置文件中找到 Yum 源的位置信息如下:

[docker-ce-stable]
name=Docker CE Stable - $basearch
# baseurl 属性定义了 Yum 源的互联网位置,【$basearch】变量表示操作系统的架构,本例中是"x86_64"。
# 因此完整的 Yum 源是【https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable】
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

2)在浏览器中打开 Yum 源网址,点击 "Packages" 子目录中:


进入 Packages 子目录

3)找到最新版本的 "containerd.io" 安装包,复制其下载地址:

最新版本的 "containerd.io" 安装包

在浏览器中复制其下载地址是:【https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm

4)通过 Yum 源指令人工下载安装最新版的 "containerd.io" 安装包:

[centos@host ~]$ sudo dnf install https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm

注意:如果使用本地或私有yum源,可忽略第1-2步,直接获取本地/私有yum源的配置文件使用即可。有关如何进行yum源的本地化/私有化,请阅读文章《RedHat/CentOS8 【国内/本地/私有 YUM 源】制作和使用》,文章地址【https://www.jianshu.com/p/68db74388600】。

3、通过 Yum 源安装 Docker 社区版。

[centos@host ~]$ sudo dnf install docker-ce

4、为 Docker 的使用和管理用户追加到 "docker" 扩展组,并重启后生效。

[centos@host ~]$ sudo usermod -aG docker centos
[centos@host ~]$ reboot

5、配置 Docker 的工作目录、配置目录。

使用文本编辑器创建配置文件:

[centos@host ~]$ sudo mkdir -p /data/docker
[centos@host ~]$ sudo chown -R root:docker /data/docker
[centos@host ~]$ sudo mkdir -p /etc/docker

6、在 "/etc/docker" 目录下创建或编辑 "daemon.json" 文件,并写入 Docker 工作目录配置信息。

使用文本编辑器创建配置文件:

[centos@host ~]$ sudo gedit /etc/docker/daemon.json

在文件中写入以下配置并保存(删除注释):

{
  # 加速器
  # "registry-mirrors": ["https://ijlmqclb.mirror.aliyuncs.com"],
  # 工作目录
  "data-root": "/data/docker"
}

7、开启网络转发,使 Docker 容器能够通过网络桥接 "bridge" 方式(默认网络网配)连通网络。

使用文本编辑器打开配置文件:

[centos@host ~]$ sudo gedit /etc/sysctl.conf

在文件中追加以下内容并保存:

net.ipv4.ip_forward = 1

重载配置文件:

[centos@host ~]$ sudo sysctl -p

8、修改 SELinux 配置文件,永久关闭 SELinux。

使用文本编辑器打开"/etc/selinux/config"文件:

[centos@host ~]$ sudo gedit /etc/selinux/config

将 "SELINUX" 参数设置为:"permissive" 或者 "disabled",并保存:

#     enforcing - 表示启用 SELinux 安全策略。
#     permissive - 表示启用 SELinux 安全策略,但不强制验证。如果执行第一步可以正常运行,则建议设置此值。
#     disabled - 关闭 SELinux 安全策略,相当于没有安装 SELinux。
SELINUX=disabled

重启服务器:

[centos@host ~]$ sudo reboot

9、关闭防火墙并禁用开机启动,CentOS8 使用 firewall 作为防火墙管理程序,firewalld 的底层调用的是 iptables,firewalld 启动或重启时,会将 iptables 的规则清空,导致 docker 启动容器出错。

[centos@host ~]$ sudo systemctl stop firewalld.service
[centos@host ~]$ sudo systemctl disable firewalld.service

10、配置 Docker 服务开机自启动(Docker 安装后已默认配置)。

使用文本编辑器打开配置文件:

[centos@host ~]$ sudo gedit /usr/lib/systemd/system/docker.service

验证或修改文件内容并保存:

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
BindsTo=containerd.service
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3

# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity

# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity

# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes

# kill only the docker process, not all processes in the cgroup
KillMode=process

[Install]
WantedBy=multi-user.target

11、启动 Docker 服务,并设置为开机自动启动。

[centos@host ~]$ sudo systemctl daemon-reload
[centos@host ~]$ sudo systemctl start docker
[centos@host ~]$ sudo systemctl enable docker

12、运行 Docker 命令,查看版本信息测试。

[centos@host ~]$ docker version

13、登录到 Docker 官方仓库【https://hub.docker.com】,创建 Docker 官方远程仓库账号,并使用该账号完成 Docker 的用户认证。Docker 安装后默认连接了官方远程仓库,需要在该网站上申请账号,并使用该账号登录 Docker 客户端。

1)创建账号:


第一步,创建账号
第二步,选择免费使用协议
第三步,提示邮箱验证账号
第四步,登录电子邮箱验证账号
第五步,账号注册成功

2)Docker 客户端登录仓库:

[centos@host ~]$ 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: zhangyisoho
Password: 
WARNING! Your password will be stored unencrypted in /home/centos/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

14、拉取 Docker 示例容器 "hello-world",拉取成功后在本地仓库中查询。

拉取示例容器 "hello-world":

[centos@host ~]$ docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:49a1c8800c94df04e9658809b006fd8a686cab8028d33cfba2cc049724254202
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest

在本地仓库中查询拉取容器:

[centos@host ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        7 months ago        13.3kB

15、运行 Docker 示例容器 "hello-world"。

[centos@host ~]$ docker run hello-world
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/

注意:在国内访问官方 Docker 远程仓库速度受限,会经常提示网络超时问题,需要反复尝试,几乎无法使用,不具备生产环境的使用条件,需要替换为国内仓库或者私有仓库使用。

Docker 常用命令使用指南请阅读文章【https://www.runoob.com/docker/docker-command-manual.html


3.Docker 使用阿里云镜像仓库

在国内访问官方 Docker 远程仓库速度受限,会经常提示网络超时问题,需要反复尝试,几乎无法使用,不具备生产环境的使用条件,需要替换为国内仓库或者私有仓库使用。

国内阿里云提供 Docker 容器服务,主要包括 官方镜像加速器私有仓库官方镜像加速器 在配置后能够极快的访问国外官方 Docker 远程仓库;私有仓库 为用户提供个人镜像仓库服务,用户可以将自定义的镜像上传到仓库中,以便于其他用户拉取使用。

3.1.注册阿里云账号并开通容器服务

1、访问阿里云站点【https://www.aliyun.com】,注册阿里云用户账号。(已注册用户忽略本步骤)

第一步,访问站点并注册账号
第二步,选择账号密码注册
第三步,填写信息并注册
第四步,注册完成后进入控制台

2、使用阿里云账号登录控制台。

第一步,访问站点并注册账号
第二步,选择账号密码登录
第三步,填写信息并登录
第四步,从导航菜单中选择"容器镜像服务"
第五步,前往开通容器服务
第六步,设置容器仓库登录密码
第七步,设置口令后确定开通容器服务

3.2.使用阿里云仓库加速器

1、注册阿里云用户并登陆控制台,开通容器镜像服务,操作过程请阅读章节 “3.1.注册阿里云账号并开通容器服务”。

2、从导航菜单中打开 "镜像加速器" 页面,使用阿里云提供的加速器地址,按照对应操作系统版本的操作指南配置 Docker 宿主机。如下图所示:

阿里云镜像加速器操作指南

3、配置 Docker 阿里云仓库加速器。在 "/etc/docker" 目录下创建或编辑 "daemon.json" 文件,并写入加速器配置信息。

使用文本编辑器创建配置文件:

[centos@host ~]$ sudo gedit /etc/docker/daemon.json

根据阿里云提供的加速器地址在文件中写入以下配置并保存(删除注释):

{
  # 加速器
  "registry-mirrors": ["https://ijlmqclb.mirror.aliyuncs.com"],
  # 工作目录
  "data-root": "/data/docker"
}

4、重新加载 Docker 服务并重启服务。

[centos@host ~]$ sudo systemctl daemon-reload
[centos@host ~]$ sudo systemctl restart docker

5、拉取 Docker 示例容器 "hello-world"(速度会大幅提高且不会断线),拉取成功后在本地仓库中查询。

拉取示例容器 "hello-world":

[centos@host ~]$ docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:49a1c8800c94df04e9658809b006fd8a686cab8028d33cfba2cc049724254202
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest

在本地仓库中查询拉取容器:

[centos@host ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        7 months ago        13.3kB

注意:加速器仅是使 Docker 在访问官方远程仓库时提高访问速度(解决了国外网络访问受限的问题),它并没有使用国内仓库进行替代,因此仍需使用官方仓库的用户登录 Docker 客户端,操作过程请阅读章节 “2.Docker 的安装和配置” 的第 8 个步骤。


3.3.使用阿里云私有仓库

1、注册阿里云用户并登陆控制台,开通容器镜像服务,操作过程请阅读章节 “3.1.注册阿里云账号并开通容器服务”。

2、从导航菜单中打开 "镜像仓库" 页面,创建和管理阿里云私有仓库。如下图所示:

创建和管理阿里云私有仓库

3、创建私有仓库。

第一步,输入仓库信息
第二步,选择代码源,并创建镜像仓库
第三步,查看已创建的仓库,并通过"管理"按钮查看操作指南
第四步,查看私有仓库操作指南

4、Docker 客户端登录阿里云私有仓库。

[centos@host ~]$ docker login registry.cn-hangzhou.aliyuncs.com
Username: zhangyi_soho
Password: 
WARNING! Your password will be stored unencrypted in /home/centos/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

5、Docker 客户端推送本地镜像到私有仓库中,以镜像 "hello-world" 为例。

查询镜像的 "ImageId":

[centos@host ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        7 months ago        13.3kB

推送本地镜像:

[centos@host ~]$ docker tag bf756fb1ae65 registry.cn-hangzhou.aliyuncs.com/zhangyi_soho/hello-world:latest
[centos@host ~]$ docker push registry.cn-hangzhou.aliyuncs.com/zhangyi_soho/hello-world:latest
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/zhangyi_soho/hello-world]
9c27e219663c: Pushed 
latest: digest: sha256:90659bf80b44ce6be8234e6ff90a1ac34acbeb826903b02cfa0da11c82cbc042 size: 525

在阿里云私有仓库的管理页面中查看:


查看私有仓库中的镜像

6、Docker 客户端从阿里云私有仓库中拉取镜像到本地,以镜像 "hello-world" 为例。

[centos@host ~]$ docker pull registry.cn-hangzhou.aliyuncs.com/zhangyi_soho/hello-world:latest
latest: Pulling from zhangyi_soho/hello-world
Digest: sha256:90659bf80b44ce6be8234e6ff90a1ac34acbeb826903b02cfa0da11c82cbc042
Status: Image is up to date for registry.cn-hangzhou.aliyuncs.com/zhangyi_soho/hello-world:latest
registry.cn-hangzhou.aliyuncs.com/zhangyi_soho/hello-world:latest

7、在本地仓库中查询拉取容器。

[centos@host ~]$ docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
registry.cn-hangzhou.aliyuncs.com/zhangyi_soho/hello-world   latest              bf756fb1ae65        7 months ago        13.3kB

4.Harbor 私有化镜像仓库

假设主机 IP 地址是【192.168.0.20】。

Harbor 是 VMware 公司开源(遵循 Apache-2.0 License 协议)的企业级 Docker Registry 项目,目标是帮助用户迅速搭建一个企业级的Docker registry 服务。它以 Docker 公司的开源项目 Registry 为基础,提供了管理界面、访问控制、AD/LDAP 集成、审计日志 等企业用户需求的功能,并支持中文语言。Harbor 的每个组件都是以 Docker 容器的形式构建的,因此需要使用 Docker Compose 进行部署。

Docker Compose 是用于定义和运行多容器 Docker 应用程序的工具。通过 Docker Compose 可以使用 YAML 文件来配置应用程序需要的所有服务。然后,使用一个命令,就可以从 YAML 文件配置中创建并启动所有服务。

4.1.Harbor 的安装和配置

1、下载和安装 Docker,操作过程请阅读章节 “2.Docker 的安装和配置”。

2、下载和安装 Docker Compose 。

1)打开 Docker Compose 下载页面【https://github.com/docker/compose/releases】,下载 Docker Compose 的编译程序到用户主目录中:

Docker Compose 下载页面

下载后将 Docker Compose 编译程序安装到 "/usr/local/bin" 目录中:

[centos@host ~]$ sudo mv ~/docker-compose-Linux-x86_64 /usr/local/bin/docker-compose

或者通过 "curl" 指令下载并安装:

[centos@host ~]$ sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

2)授予程序可行性权限,并建立软链接到系统默认的 shell 目录中 "/usr/bin":

[centos@host ~]$ sudo chmod +x /usr/local/bin/docker-compose
[centos@host ~]$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

3)测试是否安装成功:

[centos@host ~]$ docker-compose --version
docker-compose version 1.26.2, build eefe0d31

3、打开 Harbor 下载页面【https://github.com/goharbor/harbor/releases】,下载 Harbor 离线版编译程序 tgz 包到用户主目录中。

Harbor 下载页面

4、解压缩编译程序 tar 包到"/usr/local"目录中。

[centos@host ~]$ sudo tar -xvf harbor-offline-installer-v2.0.2.tgz -C /usr/local
[centos@host ~]$ ll /usr/local
drwxr-xr-x. 2 root root 122 8月   7 15:01 harbor

5、设置 Harbor 的配置文件参数。

拷贝配置文件模板创建配置文件:

[centos@host ~]$ sudo cp /usr/local/harbor/harbor.yml.tmpl /usr/local/harbor/harbor.yml

使用文本编辑器打开配置文件:

[centos@host ~]$ sudo gedit /usr/local/harbor/harbor.yml

修改或验证文件中的以下参数并保存:

########### 常用设置 ###########

# 目标计算机的IP地址或完全限定的域名,用于访问管理界面和仓库服务
# 例如:[ 192.168.0.20 或 reg.docker.harbor ]。不要使用 localhost 或 127.0.0.1 作为主机名(外部客户端将无法访问)
hostname: reg.docker.harbor

# http 配置
http:
  # http 服务端口号,默认值是【80】
  port: 80

# https 配置
https:
  # https 服务端口号,默认值是【80】
  port: 443
  # Nginx 的 SSL证书的路径,仅在协议设置为 https 时应用
  certificate: /usr/local/harbor/https/reg.docker.harbor.crt
  # Nginx 的 SSL密钥的路径,仅在协议设置为 https 时应用
  private_key: /usr/local/harbor/https/reg.docker.harbor.key

# 管理员账号【admin】初始口令,仅在首次登录时有效,首次登录后系统会提示更改管理员口令
harbor_admin_password: Harbor12345

# 数据库配置
database:
  # 数据库【root】账号口令
  password: root123
  # 数据库连接池中的最大连接数,当≤0时将不保留空闲连接
  max_idle_conns: 50
  # 数据库最大连接数,当≤0时不限制连接数,postgres 数据库的默认连接数是 1024 
  max_open_conns: 1024

# 数据存储目录目录
data_volume: /data/harbor

########### 其他设置(通常情况下使用默认值均可) ###########

# 内部组件之间的 tls 通信
# internal_tls:
     # 启动内部组件之间的 tls 通信
     # enabled: true
     # 证书和秘钥文件存放目录
     # dir: /etc/harbor/tls/internal

# 外部代理,当启用时,将不再使用主机名
# external_url: https://reg.mydomain.com:8433

# 存储服务配置,默认情况下,数据存储设置在本地文件系统上,如果要使用外部存储,则取消对存储服务设置的注释
# storage_service:
  # 自定义根证书的路径,它将被注入到注册表和图表存储库容器的信任库中。当用户使用自签名证书托管内部存储时,通常需要这样做。
  # ca_bundle:
  # 存储后端,默认为文件系统,选项包括 filesystem、azure、gcs、s3、swift 和 oss
  # 有关此配置的详细信息,请参阅https://docs.docker.com/registry/configuration/
  # filesystem:
  # maxthreads: 100
  # 设置为 true 时,禁用注册表重定向。
  # redirect:
    # disabled: false

# Clair(一款镜像安全工具) 配置
clair:
  # Clair 更新程序的间隔,单位为小时,设置为【0】以禁用更新程序
  updaters_interval: 12

# Trivy(一款镜像漏洞扫描工具) 配置
trivy:
  # 设置为 true 表示只显示已修复的漏洞
  ignore_unfixed: false
  # 启用或禁用从 GitHub 上更新 Trivy 数据库
  skip_update: false
  #
  # 设置为 true 表示不安全的标志跳过验证注册表证书
  insecure: false
  # 从 GitHub 上更新 Trivy DB 是的访问令牌,令牌创建参照:
  # https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line
  # github_token: xxx

# 作业配置
jobservice:
  # Maximum number of job workers in job service
  # 作业服务中的最大复制工作者数。对于每个镜像复制作业,工作程序将存储库的所有标记同步到远程目标。增加此数量可以在系统中执行更多并发复制作业。但是,由于每个工作者都消耗一定量的网络 / CPU / IO资源,请根据主机的硬件资源仔细选择该属性的值。
  max_job_workers: 10

# 通知配置
notification:
  # Maximum retry count for webhook job
  # Web 连接作业的最大重试数
  webhook_job_max_retry: 10

# 图表配置
chart:
  # 如果设置为启用图表将使用绝对网址,否则将其设置为禁用,图表将使用相对网址
  absolute_url: disabled

# 日志配置
log:
  # 日志级别,选项是调试,信息,警告,错误,致命
  level: info
  # 配置本地存储中的日志
  local:
    # 日志文件在被删除之前被轮换的次数。如果 count 为 0,则删除
    rotate_count: 50
    # 仅当日志文件大于 log_rotate_size 字节时才会轮换日志文件。如果大小后跟 k,则假定大小以千字节为单位。如果使用 M,则大小以兆字节为单位,如果使用 G,则大小以千兆字节为单位。设置值:100,100k,100M、100G 都是有效的
    rotate_size: 200M
    # 存储中日志的目录
    location: /var/log/harbor

  # 取消对以下行的注释以启用外部 syslog 终结点。
  # external_endpoint:
       # 用于将日志传输到外部端点的协议,选项是 tcp 或 udp
       # protocol: tcp
       # 外部终结点的主机
       # host: localhost
       # 外部终结点的端口号
       # port: 5140

# 此属性用于检测配置文件的版本,请勿修改!
_version: 2.0.0

# 使用外部数据库时配置。
# external_database:
  # harbor:
    # host: harbor_db_host
    # port: harbor_db_port
    # db_name: harbor_db_name
    # username: harbor_db_username
    # password: harbor_db_password
    # ssl_mode: disable
    # max_idle_conns: 2
    # max_open_conns: 0
  # clair:
    # host: clair_db_host
    # port: clair_db_port
    # db_name: clair_db_name
    # username: clair_db_username
    # password: clair_db_password
    # ssl_mode: disable
  # notary_signer:
    # host: notary_signer_db_host
    # port: notary_signer_db_port
    # db_name: notary_signer_db_name
    # username: notary_signer_db_username
    # password: notary_signer_db_password
    # ssl_mode: disable
  # notary_server:
    # host: notary_server_db_host
    # port: notary_server_db_port
    # db_name: notary_server_db_name
    # username: notary_server_db_username
    # password: notary_server_db_password
    # ssl_mode: disable

# 使用外部 Redis 服务器时配置
# external_redis:
  # host: redis
  # port: 6379
  # password:
  # registry_db_index: 1
  # jobservice_db_index: 2
  # chartmuseum_db_index: 3
  # clair_db_index: 4
  # trivy_db_index: 5
  # idle_timeout_seconds: 30

# 使用自签名的 uaa 证书
# uaa:
  # ca_file: /path/to/ca

# 全局代理
# Config http proxy for components, e.g. http://my.proxy.com:3128
# Components doesn't need to connect to each others via http proxy.
# Remove component from `components` array if want disable proxy
# for it. If you want use proxy for replication, MUST enable proxy
# for core and jobservice, and set `http_proxy` and `https_proxy`.
# Add domain to the `no_proxy` field, when you want disable proxy
# for some special registry.
proxy:
  http_proxy:
  https_proxy:
  no_proxy:
  components:
    - core
    - jobservice
    - clair
    - trivy

6、创建本地 DNS 配置(当配置文件中【hostname】参数设置为域名时需执行本步操作)。

使用文本编辑器打开本地 DNS 映射文件 "/etc/hosts":

[centos@host ~]$ sudo gedit /etc/hosts

在文件中追加配置并保存:

192.168.0.20 reg.docker.harbor

7、安装 Openssl(CentOS8 默认已安装)并创建 Https 的 SSL 自签证书(当配置文件中启用 https 时需执行本步操作)。

[centos@host ~]$ sudo dnf install openssl
[centos@host ~]$ sudo mkdir /usr/local/harbor/https
[centos@host ~]$ sudo openssl genrsa -out /usr/local/harbor/https/reg.docker.harbor.key 4096
[centos@host ~]$ sudo openssl req -x509 -new -nodes -sha512 -days 36500 \
    -subj "/C=cn/ST=sd/L=qd/O=private/OU=harbor/CN=reg.docker.harbor" \
    -key /usr/local/harbor/https/reg.docker.harbor.key \
    -out /usr/local/harbor/https/reg.docker.harbor.crt

8、进入 Harbor 安装目录,编译 Docker Compose 文件,编译完成后在 Harbor 安装目录中自动创建 "docker-compose.yml" 文件。

[centos@host ~]$ cd /usr/local/harbor
[centos@host harbor]$ pwd
/usr/local/harbor
[centos@host harbor]$ sudo ./prepare
[centos@host harbor]$ ll docker-compose.yml
-rw-r--r--. 1 root root      5926 8月   7 23:44 docker-compose.yml

9、进入"docker-compose.yml" 文件所在目录( Harbor 安装目录),执行 Harbor 安装脚本,安装 Harbor 的 Docker 容器。

[centos@host ~]$ cd /usr/local/harbor
[centos@host harbor]$ ll docker-compose.yml
-rw-r--r--. 1 root root      5926 8月   7 23:44 docker-compose.yml
[centos@host harbor]$ sudo ./install.sh

10、进入"docker-compose.yml" 文件所在目录( Harbor 安装目录),查看 Harbor 的 Docker 容器运行状态。

[centos@host ~]$ cd /usr/local/harbor
[centos@host harbor]$ pwd
/usr/local/harbor
[centos@host harbor]$ sudo docker-compose ps
      Name                     Command                  State                 Ports          
---------------------------------------------------------------------------------------------
harbor-core         /harbor/entrypoint.sh            Up (healthy)                            
harbor-db           /docker-entrypoint.sh            Up (healthy)   5432/tcp                 
harbor-jobservice   /harbor/entrypoint.sh            Up (healthy)                            
harbor-log          /bin/sh -c /usr/local/bin/ ...   Up (healthy)   127.0.0.1:1514->10514/tcp
harbor-portal       nginx -g daemon off;             Up (healthy)   8080/tcp                 
nginx               nginx -g daemon off;             Up (healthy)   0.0.0.0:80->8080/tcp     
redis               redis-server /etc/redis.conf     Up (healthy)   6379/tcp                 
registry            /home/harbor/entrypoint.sh       Up (healthy)   5000/tcp                 
registryctl         /home/harbor/start.sh            Up (healthy)                            

注意:所有容器的 State 应当是 "Up (healthy)" 则表示运行正常。

11、在浏览器中访问【https://reg.docker.harbor】或者【https://192.168.0.20】访问 Harbor 系统。

Harbor 仓库登录界面
Harbor 主界面

12、创建 Harbor 仓库项目。

第一步,在“项目”界面中点击“新建项目”按钮
第二步,输入项目信息后点击“确定”按钮
第三步,在“用户管理”界面中点击“新建用户”按钮
第四步,输入用户信息后点击“确定”按钮
第五步,选中用户后点击“设置为管理员”按钮
第六步,在“项目”界面中选中已创建的项目
第七步,在项目详情页面中选择“成员”选项卡,点击“+用户”按钮
第八步,输入已创建的用户账号,并设置用户的角色后点击“确定”按钮
第九步,用户已加入到项目成员中

13、进入"docker-compose.yml" 文件所在目录( Harbor 安装目录),通过 Docker Compose 对 Harbor 进行日常运维。

1)进入"docker-compose.yml" 文件所在目录( Harbor 安装目录):

[centos@host ~]$ cd /usr/local/harbor
[centos@host harbor]$ ll docker-compose.yml
-rw-r--r--. 1 root root      5926 8月   7 23:44 docker-compose.yml

2)检查 Docker Compose 的 YAML 配置文件:

[centos@host harbor]$ docker-compose config

3)停止 Harbor 的 Docker 容器:

[centos@host harbor]$ docker-compose stop

4)启动 Harbor 的 Docker 容器:

[centos@host harbor]$ docker-compose start

5)停止并删除 Harbor 的 Docker 容器:

[centos@host harbor]$ docker-compose down -v

6)安装并启动 Harbor 的 Docker 容器:

[centos@host harbor]$ docker-compose up -d

7)重新配置并启动 Harbor 的 Docker 容器:

[centos@host harbor]$ docker-compose down -v
[centos@host harbor]$ sudo ./prepare
[centos@host harbor]$ sudo ./install.sh

8)删除 Harbor 的镜像数据:

[centos@host harbor]$ sudo rm -rf /data/harbor

9)查看 Harbor 的容器状态:

[centos@host harbor]$ docker-compose ps
      Name                     Command                  State                 Ports          
---------------------------------------------------------------------------------------------
harbor-core         /harbor/entrypoint.sh            Up (healthy)                            
harbor-db           /docker-entrypoint.sh            Up (healthy)   5432/tcp                 
harbor-jobservice   /harbor/entrypoint.sh            Up (healthy)                            
harbor-log          /bin/sh -c /usr/local/bin/ ...   Up (healthy)   127.0.0.1:1514->10514/tcp
harbor-portal       nginx -g daemon off;             Up (healthy)   8080/tcp                 
nginx               nginx -g daemon off;             Up (healthy)   0.0.0.0:80->8080/tcp     
redis               redis-server /etc/redis.conf     Up (healthy)   6379/tcp                 
registry            /home/harbor/entrypoint.sh       Up (healthy)   5000/tcp                 
registryctl         /home/harbor/start.sh            Up (healthy)                            

注意:所有容器的 State 应当是 "Up (healthy)" 则表示运行正常。


4.2.Docker 使用 Harbor 私有仓库

1、创建本地 DNS 配置(当 Harbor 配置文件中【hostname】参数设置为域名时需执行本步操作)。

使用文本编辑器打开本地 DNS 映射文件 "/etc/hosts":

[centos@host ~]$ sudo gedit /etc/hosts

在文件中追加配置并保存:

192.168.0.20 reg.docker.harbor

2、创建 Harbor Https 的 SSL 证书目录(目录格式:/etc/docker/certs.d/【harbor hostname】)并下载 Harbor 生成的证书文件(当 Harbor 配置文件中启用 https 时需执行本步操作)。

[centos@host ~]$ sudo mkdir -p /etc/docker/certs.d/reg.docker.harbor
[centos@host ~]$ sudo scp centos@192.168.0.20:/usr/local/harbor/https/reg.docker.harbor.crt /etc/docker/certs.d/reg.docker.harbor
[centos@host ~]$ ll /etc/docker/certs.d/reg.docker.harbor
-rw-r--r--. 1 root root 2033 8月  10 09:46 reg.docker.harbor.crt

3、Docker 客户端登录 Harbor 私有仓库。

[centos@host ~]$ docker login reg.docker.harbor
Username: zhangyi_soho  
Password: 
WARNING! Your password will be stored unencrypted in /home/centos/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

4、Docker 客户端推送本地镜像到私有仓库中,以镜像 "hello-world" 为例。

查询镜像的 "ImageId":

[centos@host ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        7 months ago        13.3kB

推送本地镜像:

[centos@host ~]$ docker tag bf756fb1ae65 reg.docker.harbor/docker_store/hello-world:latest
[centos@host ~]$ docker push reg.docker.harbor/docker_store/hello-world:latest
The push refers to repository [reg.docker.harbor/docker_store/hello-world]
9c27e219663c: Pushed 
latest: digest: sha256:90659bf80b44ce6be8234e6ff90a1ac34acbeb826903b02cfa0da11c82cbc042 size: 525

在 Harbor 私有仓库的管理页面中查看:

在“项目”界面中选中已上传镜像的项目
查看已上传的镜像

5、Docker 客户端从 Harbor 私有仓库中拉取镜像到本地,以镜像 "hello-world" 为例。

[centos@host ~]$ docker pull reg.docker.harbor/docker_store/hello-world:latest
latest: Pulling from docker_store/hello-world
Digest: sha256:90659bf80b44ce6be8234e6ff90a1ac34acbeb826903b02cfa0da11c82cbc042
Status: Image is up to date for reg.docker.harbor/docker_store/hello-world:latest
reg.docker.harbor/docker_store/hello-world:latest

6、在本地仓库中查询拉取容器。

[centos@host ~]$ docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
reg.docker.harbor/docker_store/hello-world              latest              bf756fb1ae65        7 months ago        13.3kB

5.Docker 镜像制作

Docker 镜像从应用角度一般自下而上分为三个类别:

  • 基础镜像:CentOS、Ubuntu 等文件系统的镜像,一般从互联网的仓库中获取。
  • 环境镜像:基于基础镜像制作的部署 MySQL、JDK 等中间件的镜像,可用于直接部署应用。
  • 应用镜像:基于环境镜像制作的部署应用产品的镜像,用于项目交付。

假设主机 IP 地址是【192.168.0.20】,以基于 CentoOS 的 MySQL8 数据库的 Docker 环境镜像制作过程为例:

1、下载和安装 Docker,操作过程请阅读章节 “2.Docker 的安装和配置”。

2、配置和使用国内阿里云仓库,操作过程请阅读章节 “3.阿里云镜像仓库”。

登录阿里云私有仓库:

[centos@host ~]$ docker login registry.cn-hangzhou.aliyuncs.com
Username: zhangyi_soho
Password: 
WARNING! Your password will be stored unencrypted in /home/centos/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

或者在配置阿里云加速后登录官方仓库:

[centos@host ~]$ 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: zhangyisoho
Password: 
WARNING! Your password will be stored unencrypted in /home/centos/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

3、查询仓库中已发布的 centos 镜像,并拉取官方编译版本。

1)查询仓库中已发布的 centos 镜像:

[centos@host ~]$ docker search centos
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   6125                [OK]                

2)拉取 centos 官方编译版本:

[centos@host ~]$ docker pull centos
Using default tag: latest
latest: Pulling from library/centos
Digest: sha256:4062bbdd1bb0801b0aa38e0f83dece70fb7a5e9bce223423a68de2d8b784b43b
Status: Image is up to date for centos:latest
docker.io/library/centos:latest

3)查询本地仓库中已拉取的 centos 镜像:

[centos@host ~]$ docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
centos                          latest              831691599b88        7 weeks ago         215MB

4、使用 centos 创建一个 mysql 的部署容器。

[centos@host ~]$ docker run -tid --name=mysql  centos /sbin/init
a0fb0201e85d044be88d21f7a3e7882c91078802b6922142712e933192c12e3d

docker run :创建一个新的容器并运行一个命令。

语法:docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

[OPTIONS]:
-i: 以交互模式运行容器,通常与 -t 同时使用;
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用。
-d: 后台运行容器,并返回容器ID;
-v /data/mysql:/var/lib/mysql: 挂载宿主机目录,格式为:宿主机目录:容器目录(非必要参数,主要是为了使数据脱离容器存储,当目录不存在时自动创建);
--name="mysql": 为容器指定一个名称。

IMAGE:镜像名称或ID。

[COMMAND]:执行的命令,注意要设置为 "/sbin/init",否则在容器中 "systemctl" 指令故障。

5、查看 mysql 部署容器的状态。

[centos@host ~]$ docker ps -a
CONTAINER ID        IMAGE                                COMMAND                  CREATED             STATUS                 PORTS                                         NAMES
76983aa5b182        centos                               "/sbin/init"             6 minutes ago       Up 6 minutes                                                             mysql

6、进入 mysql 的部署容器。

[centos@optimus-prime ~]$ docker exec -it mysql /bin/bash
[root@e4599ea3ee0b /]#  uname -r         
4.18.0-193.14.2.el8_2.x86_64

docker exec :在运行的容器中执行命令。

语法:docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

[OPTIONS]:
-i: 以交互模式运行容器,通常与 -t 同时使用;
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用。

CONTAINER:容器名称或ID。

COMMAND:执行的命令,一般可设置为 "/bin/bash"。

7、将 mysql 容器中的 centos 升级到最新版本。

[root@e4599ea3ee0b /]#  dnf update

8、在 mysql 的部署容器中安装和配置 MySQL8 社区版数据库。

1)从 Yum 源中安装 MySQL8 社区版:

[root@e4599ea3ee0b /]#  dnf install mysql-server mysql

2)修改 MySQL 配置文件,设置 MySQL 服务器为本地免密登录模式:

使用文本编辑器打开配置文件,修改或验证文件中的以下参数并保存:

[root@e4599ea3ee0b /]#  vi /etc/my.cnf
[mysqld]
# 开启以下两项设置后,仅能够通过本地访问数据库,使用超级管理员身份登录且不需要进行身份认证。

# 跳过身份认证,默认不启用。
skip-grant-tables

# 跳过远程链接,默认不启用。
skip-networking

3)重新启动 MySQL 服务:

[root@e4599ea3ee0b /]#  systemctl restart mysqld.service

4)免密登录客户端并清除"root"账号口令:

[root@e4599ea3ee0b /]#  mysql
# 连接 mysql 数据库,默认数据库。
mysql>use mysql;

# 更新 root 账号口令为空(不能设置其他口令)。
mysql>update user set authentication_string='' where user='root';

# 退出客户端。
mysql> exit;

5)修改 MySQL 配置文件,设置 MySQL 服务器为正常启动模式:

使用文本编辑器打开配置文件,修改或验证文件中的以下参数并保存:

[root@e4599ea3ee0b /]#  vi /etc/my.cnf
[mysqld]
# 开启以下两项设置后,仅能够通过本地访问数据库,使用超级管理员身份登录且不需要进行身份认证。

# 跳过身份认证,默认不启用。
# skip-grant-tables

# 跳过远程链接,默认不启用。
# skip-networking

6)重新启动 MySQL 服务:

[root@e4599ea3ee0b /]#  systemctl restart mysqld.service

7)使用"root"账号登录客户端,设置新的口令和策略。密码要包含大写字母,小写字母,数字,特殊符号:

[root@e4599ea3ee0b /]#  mysql -u root

# 初始化root账号本地登录的口令。
# 格式:alter user '<账号>'@'<客户端IP策略>' identified by '<登录密码>';
# 说明:
# <客户端IP策略>可以使用通配符”%”,也可以指定具体IP地址或地址段,如:
# 允许全部远程地址访问,设置为:”%”;
# 允许IP地址访问,设置为:”192.168.0.1”;
# 允许IP地址段访问,设置为:”192.168.0.%”。
mysql> alter user 'root'@'localhost' identified by 'password';

# 创建root账号远程登录的策略和口令。
# 格式:create user '<账号>'@'<客户端IP策略>' identified with mysql_native_password by '<登录密码>';
# 说明:
# <客户端IP策略>可以使用通配符”%”,也可以指定具体IP地址或地址段,如:
# 允许全部远程地址访问,设置为:”%”;
# 允许IP地址访问,设置为:”192.168.0.1”;
# 允许IP地址段访问,设置为:”192.168.0.%”。
# with mysql_native_password 表示设置的口令可同时用于远程访问(不指定时用于本地访问)。
mysql> create user 'root'@'%' identified with mysql_native_password by 'password';

# 格式:grant all privileges on <库名>.<表名> to '<用户名>'@'<客户端IP策略>';
# 说明:
# <库名>和<表名>可以使用通配符”*”代替,表示全部数据库或全部表;
# <客户端IP策略>可以使用通配符”%”,也可以指定具体IP地址或地址段,如:
# 允许全部远程地址访问,设置为:”%”;
# 允许IP地址访问,设置为:”192.168.0.1”;
# 允许IP地址段访问,设置为:”192.168.0.%”。
mysql> grant all on *.* to 'root'@'%';

# 刷新用户权限。
mysql> flush privileges;

# 退出客户端。
mysql> exit;

8)设置 MySQL 服务为开机自启动:

[root@e4599ea3ee0b /]#  systemctl enable mysqld.service

9、从 mysql 的部署容器中退出并停止运行容器。

[root@e4599ea3ee0b /]#  exit
[centos@host ~]#  docker stop mysql

10、将 mysql 部署容器制作成新的镜像。

[centos@host ~]#  docker commit -m 'mysql8-ce' -a 'zhangyi_soho' mysql mysql8-ce:latest

docker commit :从容器创建一个新的镜像。

语法:docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

[OPTIONS]:
-a :提交的镜像作者;
-m :提交时的说明文字。

CONTAINER:容器名称或ID。

[REPOSITORY[:TAG]]:镜像名称:版本号(版本号不指定时默认为 "latest")。

11、查询本地仓库中已创建的 mysql 镜像。

[centos@host ~]$ docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
mysql8-ce                       latest                  0ceef1020774        7 minutes ago       700MB

12、删除 mysql 部署容器(一般情况下,完成镜像制作后无需保留部署容器,但删除与否并无其他影响)。

[centos@host ~]$ docker rm mysql

13、使用 mysql8-ce 镜像创建一个 mysql8 的实例容器,并建立宿主机和容器的挂载目录和服务端口映射。

[centos@host ~]$ docker run -d -p 3306:3306 -v /data/mysql:/var/lib/mysql --name=mysql8-ce-1 mysql8-ce:latest /sbin/init
a0fb0201e85d044be88d21f7a3e7882c91078802b6922142712e933192c12e3d

docker run :创建一个新的容器并运行一个命令。

语法:docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

[OPTIONS]:
-d: 后台运行容器,并返回容器ID;
-p 3306:3306: 端口映射,格式为:主机(宿主)端口:容器端口;
-v /data/mysql:/var/lib/mysql: 挂载宿主机目录,格式为:宿主机目录:容器目录(非必要参数,主要是为了使数据脱离容器存储,当目录不存在时自动创建);
--name="mysql8-ce-1": 为容器指定一个名称。

IMAGE:镜像名称或ID。

[COMMAND]:执行的命令,注意要设置为 "/sbin/init",否则在容器中 "systemctl" 指令故障。

创建容器的常见方式包括:

1)挂载宿主机目录:

[centos@host ~]$ docker run -d -p 3306:3306 -v /data/mysql:/var/lib/mysql --name=mysql8-ce-1 mysql8-ce:latest /sbin/init
a0fb0201e85d044be88d21f7a3e7882c91078802b6922142712e933192c12e3d

2)不挂载宿主机目录:

[centos@host ~]$ docker run -d -p 3306:3306 --name=mysql8-ce-1 mysql8-ce:latest /sbin/init
a0fb0201e85d044be88d21f7a3e7882c91078802b6922142712e933192c12e3d

14、进入 mysql8 的实例容器修改挂载目录的所有者。(当使用挂载宿主机目录时操作)

1)进入实例容器,修改挂载目录的所有者:

[centos@host ~]$ docker exec -it mysql8-ce-1 /bin/bash
[root@097fc4904b89 /]# chown -R mysql:mysql /var/lib/mysql

2)按照“8、在 mysql 的部署容器中安装和配置 MySQL8 社区版数据库。”的操作步骤,重新配置 MySQL8 数据库。

15、使用 Dockerfile 制作镜像。

Docker 提供一种通过编写 Dockerfile 文件来自动创建镜像的方法,但应用场景并不广泛。制作 Docker 镜像的初衷是为了将复杂的部署过程进行简化,并且在部署过程中仍然需要反复调试,如果把部署过程编写成 Dockerfile 文件来完成这项工作,在较为普遍情况下其实会增加制作和调试的难度,因此本文并不推荐这种制作方案,但并不排除在特定的应用场景中,使用 Dockerfile 制作镜像的优势和可行性。

Dockerfile 制作镜像请阅读文件【https://www.runoob.com/docker/docker-dockerfile.html


6.Docker 镜像打包、导入和运行

以在 “5.Docker 镜像制作” 章节中制作的镜像 "mysql8-ce" 为例:

1、查询本地仓库中的 mysql8-ce 镜像信息。

[centos@host ~]$ docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
mysql8-ce                       v1.0.0                  0ceef1020774        7 minutes ago       700MB

2、将 mysql8-ce 镜像打包成 tar 包到指定目录中。

[centos@host ~]$ docker save -o ~/mysql8-ce.tar mysql8-ce
[centos@host ~]$ ll mysql8-ce.tar
-rw-------. 1 centos centos 710726144 8月  10 16:14 mysql8-ce.tar

docker save : 将指定镜像保存成 tar 归档文件。

语法:docker save [OPTIONS] IMAGE [IMAGE...]

[OPTIONS]:
-o :输出到的文件。

IMAGE:镜像名称或ID,多个镜像使用空格分隔。

3、将镜像打包文件 mysql8-ce.tar 导入到 Docker 中。

[centos@host ~]$ docker load -i ~/mysql8-ce.tar

docker load : 导入使用 docker save 命令导出的镜像。

语法:docker load [OPTIONS]

[OPTIONS]:
--input , -i : 指定导入的文件。

4、使用镜像创建一个实例容器。

1)挂载宿主机目录:

[centos@host ~]$ docker run -d -p 3306:3306 -v /data/mysql:/var/lib/mysql --name=mysql8-ce-1 mysql8-ce:latest /sbin/init
a0fb0201e85d044be88d21f7a3e7882c91078802b6922142712e933192c12e3d

2)不挂载宿主机目录:

[centos@host ~]$ docker run -d -p 3306:3306 --name=mysql8-ce-1 mysql8-ce:latest /sbin/init
a0fb0201e85d044be88d21f7a3e7882c91078802b6922142712e933192c12e3d

docker run :创建一个新的容器并运行一个命令。

语法:docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

[OPTIONS]:
-d: 后台运行容器,并返回容器ID;
-i: 以交互模式运行容器,通常与 -t 同时使用;
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
-p 3306:3306: 端口映射,格式为:主机(宿主)端口:容器端口;
-v /data/mysql:/var/lib/mysql: 挂载宿主机目录,格式为:宿主机目录:容器目录(非必要参数,主要是为了使数据脱离容器存储,当目录不存在时自动创建);
--name="mysql8-ce-1": 为容器指定一个名称。

IMAGE:镜像名称或ID。

[COMMAND]:执行的命令,注意要设置为 "/sbin/init",否则在容器中 "systemctl" 指令故障。

4、进入创建一个实例容器。

[centos@optimus-prime ~]$ docker exec -it mysql8-ce-1 /bin/bash
[root@e4599ea3ee0b /]#

docker exec :在运行的容器中执行命令。

语法:docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

[OPTIONS]:
-i: 以交互模式运行容器,通常与 -t 同时使用;
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用。

CONTAINER:容器名称或ID。

COMMAND:执行的命令,一般可设置为 "/bin/bash"。


7.Docker Compose 容器编排

Docker Compose 是用于定义和运行多容器 Docker 应用程序的工具。通过 Docker Compose 可以使用 YAML 文件来配置应用程序需要的所有服务。然后,使用一个命令,就可以从 YAML 文件配置中创建并启动所有服务。

7.1.Docker Compose 的安装和配置

1、下载和安装 Docker,操作过程请阅读章节 “2.Docker 的安装和配置”。

2、配置和使用国内阿里云仓库,操作过程请阅读章节 “3.阿里云镜像仓库”。

登录阿里云私有仓库:

[centos@host ~]$ docker login registry.cn-hangzhou.aliyuncs.com
Username: zhangyi_soho
Password: 
WARNING! Your password will be stored unencrypted in /home/centos/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

或者在配置阿里云加速后登录官方仓库:

[centos@host ~]$ 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: zhangyisoho
Password: 
WARNING! Your password will be stored unencrypted in /home/centos/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

3、打开 Docker Compose 下载页面【https://github.com/docker/compose/releases】,下载 Docker Compose 的编译程序到用户主目录中。

Docker Compose 下载页面

下载后将 Docker Compose 编译程序安装到 "/usr/local/bin" 目录中:

[centos@host ~]$ sudo mv ~/docker-compose-Linux-x86_64 /usr/local/bin/docker-compose

或者通过 "curl" 指令下载并安装:

[centos@host ~]$ sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

4、授予程序可行性权限,并建立软链接到系统默认的 shell 目录中 "/usr/bin"。

[centos@host ~]$ sudo chmod +x /usr/local/bin/docker-compose
[centos@host ~]$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

5、创建工作目录。

[centos@host ~]$ sudo mkdir -p /data/docker-compose

6、测试是否安装成功。

[centos@host ~]$ docker-compose --version
docker-compose version 1.26.2, build eefe0d31

7.2.Docker Compose 容器编排

假设主机 IP 地址是【192.168.0.20】,基于 Docker 实现【Web 连接 MySQL】的 Docker Compose 镜像制作和容器编排过程为例:

1、Docker 容器规划。

1)MySQL 数据库容器:

  • 操作系统:CentOS8
  • 安装程序:MySQL8
  • 暴露 IP 和 PORT:127.0.0.1:3306

2)Tomcat 应用容器:

  • 操作系统:CentOS8
  • 安装程序:Oracle JDK,Tomcat
  • 暴露 IP 和 PORT:0.0.0.0:3306
  • 挂载宿主机目录:/data/tomcat/webapps

2、制作 MySQL8 社区版的 Docker 镜像。

按照 “5.Docker 镜像制作” 章节的第 3-12 步制作 MySQL8 社区版的 Docker 镜像。

3、制作 Tomcat 的 Docker 镜像。

1)下载 Oracle JDK,Tomcat 到主机用户主目录。

打开 Oracle JDK 官方网站下载页面【https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html】,下载 Oracle JDK 的编译程序 tar 包到用户主目录中:

Oracle JDK 下载页面

打开 Tomcat 官方网站下载页面【https://tomcat.apache.org/download-90.cgi】,下载 Tomcat 的编译程序 tar 包到用户主目录中:

Tomcat 下载页面

2)拉取 centos 镜像:

[centos@host ~]$ docker pull centos
Using default tag: latest
latest: Pulling from library/centos
Digest: sha256:4062bbdd1bb0801b0aa38e0f83dece70fb7a5e9bce223423a68de2d8b784b43b
Status: Image is up to date for centos:latest
docker.io/library/centos:latest

3)使用 centos 创建一个 tomcat 的部署容器:

[centos@host ~]$ docker run -tid --name=tomcat centos /sbin/init
8b66309840f982398528f39744b92f0cdfdabac8d8ce7fb4d1da9ed1c27299bb

4)查询 tomcat 的部署容器:

[centos@host ~]$ docker ps
CONTAINER ID        IMAGE                                COMMAND                  CREATED             STATUS                          PORTS                       NAMES
8b66309840f9        centos                               "/sbin/init"             18 minutes ago      Up 18 minutes                                               tomcat

5)从宿主机的用户主目录拷贝 Oracle JDK,Tomcat 的编译程序 tar 包到 tomcat 部署容器的用户主目录:

[centos@host ~]$ docker cp ~/jdk-8u261-linux-x64.tar.gz 8b66309840f9:/root
[centos@host ~]$ docker cp ~/apache-tomcat-9.0.37.tar.gz 8b66309840f9:/root

6)进入 tomcat 的部署容器:

[centos@host  ~]$ docker exec -it tomcat /bin/bash
[root@8b66309840f9 /]# 

7)安装 net-tools 工具:

[root@8b66309840f9 /]# dnf install net-tools

8)解压缩 Oracle JDK 和 Tomcat 的编译程序 tar 包到"/usr/local"目录中,并设置程序安装目录的所有者用户和组为"root",之后删除 tar 包:

[root@8b66309840f9 /]# tar zxvf ~/jdk-8u261-linux-x64.tar.gz -C /usr/local
[root@8b66309840f9 /]# chown root:root /usr/local/jdk1.8.0_261
[root@8b66309840f9 /]# rm -f ~/jdk-8u261-linux-x64.tar.gz
[root@8b66309840f9 /]# tar zxvf ~/apache-tomcat-9.0.37.tar.gz -C /usr/local
[root@8b66309840f9 /]# mv /usr/local/apache-tomcat-9.0.37 /usr/local/tomcat-9.0.37
[root@8b66309840f9 /]# chown root:root /usr/local/tomcat-9.0.37
[root@8b66309840f9 /]# rm -f ~/apache-tomcat-9.0.37.tar.gz

9)创建 Tomcat 开机启动服务配置文件:

使用文本编辑器打开配置文件:

[root@8b66309840f9 /]# vi /usr/lib/systemd/system/tomcat.service

在文件中编写以下内容并保存:

[Unit]
Description=Tomcat Server
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
Type=forking
User=root
Group=root

Environment=JAVA_HOME=/usr/local/jdk1.8.0_261
Environment=JRE_HOME=/usr/local/jdk1.8.0_261/jre

ExecStart=/usr/local/tomcat-9.0.37/bin/startup.sh
ExecStop=/usr/local/tomcat-9.0.37/bin/shutdown.sh
ExecReload=/usr/local/tomcat-9.0.37/bin/shutdown.sh

10)启动 Tomcat 服务,并设置为开机自动启动:

[root@8b66309840f9 /]# systemctl daemon-reload
[root@8b66309840f9 /]# systemctl start tomcat.service
[root@8b66309840f9 /]# systemctl enable tomcat.service

11)查看 Tomcat 服务的状态:

[root@ed204fd330f3 /]# systemctl status tomcat.service
tomcat.service - Tomcat Server
   Loaded: loaded (/usr/lib/systemd/system/tomcat.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2020-08-26 01:44:25 UTC; 3s ago
  Process: 323 ExecStart=/usr/local/tomcat-9.0.37/bin/startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 331 (java)
    Tasks: 30 (limit: 23668)
   Memory: 109.7M
   CGroup: /docker/8b66309840f982398528f39744b92f0cdfdabac8d8ce7fb4d1da9ed1c27299bb/system.slice/tomcat.service
           └─331 /usr/local/jdk1.8.0_261/jre/bin/java -Djava.util.logging.config.file=/usr/local/tomcat-9.0.37/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoade>

Aug 26 01:44:25 8b66309840f9 systemd[1]: Starting Tomcat Server...
Aug 26 01:44:25 8b66309840f9 startup.sh[323]: Tomcat started.
Aug 26 01:44:25 8b66309840f9 systemd[1]: Started Tomcat Server.
[root@ed204fd330f3 /]# netstat -ntap | grep 8080
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      383/java

12)从 tomcat 的部署容器中退出并停止运行容器。

[root@8b66309840f9 /]#  exit
[centos@host ~]#  docker stop tomcat

13)将 tomcat 部署容器制作成新的镜像。**

[centos@host ~]#  docker commit -m 'tomcat' -a 'zhangyi_soho' tomcat tomcat:latest
sha256:7e20454c6138aa2bd76d41d0c026e151a997c8c0a29e4f71db0a21a86e06ea51

14)查询本地仓库中已创建的 tomcat 镜像。

[centos@host ~]$ docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
tomcat                          latest              7e20454c6138        14 seconds ago      615MB

4、制作 Docker Compose 容器编排 YAML 文件。

1)在 Docker Compose 工作目录中创建 "WebApp" 项目目录:

[centos@host ~]$ sudo mkdir -p /data/docker-compose/webapp

2)创建 Docker Compose 的容器编排 YAML 文件:

使用文本编辑器打开配置文件:

[centos@host ~]$ sudo gedit /data/docker-compose/webapp/docker-compose.yaml

在文件中编写以下内容并保存(删除注释):

# 表示 YAML 文件的语法版本。
version: '3.7'
# 表示项目中编排的服务集合,每个服务定义了一个容器的启动参数。
services:
  # 服务名称(唯一标识)
  web:
    # 服务使用的镜像。
    image: tomcat:latest
    # 当指定为 true 时,容器中的 "root" 用户具备根管理员权限,当指定为 false 时,只具备普通用户的权限。
    privileged: true
    # 服务暴露的端口号,格式为:主机(宿主)端口:容器端口。
    ports:
     - 8080:8080
    # 服务挂载的宿主机目录,格式为:宿主机目录:容器目录。
    volumes:
     - /data/webapps:/usr/local/tomcat-9.0.37/webapps
    # 启动时依赖的服务,影响部署顺序。
    depends_on:
     - mysql
  mysql:
    image: mysql8-ce:latest
    privileged: true
    ports:
     - 127.0.0.1:3306:3306
    volumes:
     - /data/mysql:/var/lib/mysql

注意:当使用挂载目录时,需要在制作镜像时提前创建好主机目录,并将镜像目录中的数据拷贝到主机目录中,否则在使用镜像创建容器时,该目录会数据缺失。

在本例中,当制作 Tomcat 和 MySQL 镜像完成并退出部署容器后,需要执行以下操作:

[centos@host ~]$ mkdir /data
[centos@host ~]$ docker cp e4599ea3ee0b:/var/lib/mysql /data/
[centos@host ~]$ docker cp 8b66309840f9:/usr/local/tomcat-9.0.37/webapps /data/

3)进入 "WebApp" 项目目录,检查 YAML 文件,启动服务,查看服务状态:

[centos@host ~]$ cd /data/docker-compose/webapp

[centos@host webapp]$ docker-compose config

[centos@host webapp]$ docker-compose up -d
Creating webapp_mysql_1 ... done
Creating webapp_web_1   ... done

[centos@host webapp]$ docker-compose ps
webapp_mysql_1   /sbin/init   Up      127.0.0.1:3306->3306/tcp
webapp_web_1     /sbin/init   Up      0.0.0.0:8080->8080/tcp

注意:所有容器的 State 应当是 "Up" 则表示运行正常。

5、Docker Compose 编排容器的管理和运维。

1)进入 Docker Compose 的项目目录(以 "WebApp" 项目为例):

[centos@host ~]$ cd /data/docker-compose/webapp
[centos@host webapp]$ ll docker-compose.yaml
-rw-r--r--. 1 root root      5926 8月   7 23:44 docker-compose.yaml

2)检查 Docker Compose 的 YAML 配置文件:

[centos@host webapp]$ docker-compose config

3)停止 Docker 容器:

[centos@host webapp]$ docker-compose stop

4)启动 Docker 容器:

[centos@host webapp]$ docker-compose start

5)停止并删除 Docker 容器:

[centos@host webapp]$ docker-compose down -v

6)安装并启动 Docker 容器:

[centos@host webapp]$ docker-compose up -d

7)重新配置并启动 Docker 容器:

[centos@host webapp]$ docker-compose down -v
[centos@host webapp]$ docker-compose up -d

8)查看容器状态:

[centos@host webapp]$ docker-compose ps
webapp_mysql_1   /sbin/init   Up      127.0.0.1:3306->3306/tcp
webapp_web_1     /sbin/init   Up      0.0.0.0:8080->8080/tcp

注意:所有容器的 State 应当是 "Up" 则表示运行正常。

YAML 编写规范请阅读文章【https://www.runoob.com/w3cnote/yaml-intro.html

Docker Compose 编写规范请阅读文章【https://www.runoob.com/docker/docker-compose.html】、【http://www.dockerinfo.net/docker-compose-%e9%a1%b9%e7%9b%ae


附录一 相关技术指南目录


附录二 安装 Xfce 桌面环境和 VNC 远程桌面

假设主机 IP 地址是【192.168.0.20】,以基于 CentoOS 的 Xfce + tigervnc 的 Docker 环境镜像制作过程为例:

1、下载和安装 Docker,操作过程请阅读章节 “2.Docker 的安装和配置”。

2、配置和使用国内阿里云仓库,操作过程请阅读章节 “3.阿里云镜像仓库”。

登录阿里云私有仓库:

[centos@host ~]$ docker login registry.cn-hangzhou.aliyuncs.com
Username: zhangyi_soho
Password: 
WARNING! Your password will be stored unencrypted in /home/centos/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

或者在配置阿里云加速后登录官方仓库:

[centos@host ~]$ 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: zhangyisoho
Password: 
WARNING! Your password will be stored unencrypted in /home/centos/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

3、查询仓库中已发布的 centos 镜像,并拉取官方编译版本。

1)查询仓库中已发布的 centos 镜像:

[centos@host ~]$ docker search centos
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   6125                [OK]                

2)拉取 centos 官方编译版本:

[centos@host ~]$ docker pull centos
Using default tag: latest
latest: Pulling from library/centos
Digest: sha256:4062bbdd1bb0801b0aa38e0f83dece70fb7a5e9bce223423a68de2d8b784b43b
Status: Image is up to date for centos:latest
docker.io/library/centos:latest

3)查询本地仓库中已拉取的 centos 镜像:

[centos@host ~]$ docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
centos                          latest              831691599b88        7 weeks ago         215MB

4、使用 centos 创建一个 desktop 的部署容器。

[centos@host ~]$ docker run -tid -p 5901:5901 -p 6901:6901 --name=desktop  centos /sbin/init
a0fb0201e85d044be88d21f7a3e7882c91078802b6922142712e933192c12e3d

5、查看 desktop 部署容器的状态。

[centos@host ~]$ docker ps -a
CONTAINER ID        IMAGE                                COMMAND                  CREATED             STATUS                 PORTS                                         NAMES
76983aa5b182        centos                               "/sbin/init"             6 minutes ago       Up 6 minutes                                                         desktop

6、进入 desktop 的部署容器。

[centos@optimus-prime ~]$ docker exec -it desktop /bin/bash

7、将 desktop 容器中的 centos 升级到最新版本。

[root@e4599ea3ee0b /]#  dnf update

8、在 desktop 的部署容器中安装 EPEL 的 YUM 源。

[root@e4599ea3ee0b /]#  dnf install epel-release

9、在 desktop 的部署容器中安装和配置 xfce 桌面系统。

1)搜索 xfce 程序组名称和安装状态:

[root@e4599ea3ee0b /]#  dnf grouplist
Last metadata expiration check: 0:46:01 ago on Tue Nov 10 07:56:44 2020.
Available Environment Groups:
   Server with GUI
   Server
   Minimal Install
   Workstation
   KDE Plasma Workspaces
   Virtualization Host
   Custom Operating System
Available Groups:
   Container Management
   .NET Core Development
   RPM Development Tools
   Development Tools
   Graphical Administration Tools
   Headless Management
   Legacy UNIX Compatibility
   Network Servers
   Scientific Support
   Security Tools
   Smart Card Support
   System Tools
   Fedora Packager
   Xfce

2)安装 xfce 程序组:

[root@e4599ea3ee0b /]#  dnf groupinstall Xfce

10、在 desktop 的部署容器中安装和配置 vnc 远程桌面。

1)安装 vnc 服务器:

[root@e4599ea3ee0b /]#  dnf install tigervnc-server

2)从模板拷贝远程桌面服务实例的配置文件:

[root@e4599ea3ee0b /]#  cp /lib/systemd/system/vncserver@.service /lib/systemd/system/vncserver@:1.service

注意:程桌面服务实例配置文件的命名规则为:vncserver@[:1].service。
其中 [:1] 表示端口号的最后一位,本例对应的服务端口号是 "5901" 和 "6901"。 如为 [:2],则表示监听端口为 "5902" 和 "6902"。

3)编辑远程桌面服务实例的配置文件,将文件中 "<USER>" 占位符替换成远程桌面的访问用户:

使用文本编辑器打开配置文件:

[root@e4599ea3ee0b /]#  vi /lib/systemd/system/vncserver@:1.service

将 "<USER>" 替换成远程桌面的访问用户,本例中应替换为 "root":

[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target

[Service]
Type=simple

# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'
ExecStart=/usr/bin/vncserver_wrapper <USER> %i
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :'

[Install]
WantedBy=multi-user.target

4)设置远程桌面访问用户的 VNC 口令,必须将访问用户作为当前用户执行:

[root@e4599ea3ee0b /]#  vncpasswd

5)启动远程桌面服务实例并设置为开机启动:

[root@e4599ea3ee0b /]#  systemctl daemon-reload
[root@e4599ea3ee0b /]#  systemctl enable vncserver@:1.service --now

6)修改远程桌面访问用户的 VNC 配置:

使用文本编辑器打开配置文件:

[root@e4599ea3ee0b /]#  vi ~/.vnc/xstartup

编写内容如下:

#!/bin/sh
# Uncomment the following two lines for normal desktop:
# unset SESSION_MANAGER
# exec /etc/X11/xinit/xinitrc
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
startxfce4 &

7)重新启动远程桌面服务实例:

[root@e4599ea3ee0b /]#  systemctl restart vncserver@:1.service

11、在客户端通过 vnc-viewer 等远程桌面客户端工具访问。

第一步,输入宿主机地址和暴露端口,服务实例端口 5901 对应用户 "root"

第二步,输入 VNC 服务实例访问用户的口令
第三步,登录成功

附录三 扩展 CentOS 基础镜像

1、使用 centos 镜像创建一个部署容器。

[centos@host ~]$ docker run -tid --name=centos-extra centos /sbin/init
a0fb0201e85d044be88d21f7a3e7882c91078802b6922142712e933192c12e3d

注意:

  1. [COMMAND] 参数指定为 "/sbin/init",否则无法使用 "systemctl" 工具管理开机启动服务。

2、进入部署容器。

[centos@host ~]$ docker exec -it centos-extra /bin/bash

3、设置中国时区。

[root@e4599ea3ee0b /]#  cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@e4599ea3ee0b /]#  echo Asia/Shanghai > /etc/timezone

4、更新系统软件。

[root@e4599ea3ee0b /]#  dnf update

5、安装 EPEL 的 Yum 源。

[root@e4599ea3ee0b /]#  dnf install epel-release

6、安装常用运维工具。

[root@e4599ea3ee0b /]#  dnf install net-tools wget curl telnet  passwd zip unzip openssh-server make gcc java-1.8.0 python3 perl

说明:

  1. net-tools:常用的网络运维工具包,如:ifconfig,netstat,ping 命令等。
  2. wget:一个文件下载命令行工具。
  3. telnet:一个远程主机端口访问工具。
  4. curl:一个的网页浏览命令行工具。
  5. passwd:用于修改系统用户的口令。
  6. zip,unzip:ZIP 文件压缩和解压缩工具。
  7. openssh-server:用于提供 ssh 远程访问服务。
  8. gcc:C/C++编译器,用来将源代码编译成可发布的软件程序。
  9. make:工程管理工具,能够根据 Makefile 中的脚本执行编译、安装流程。
  10. java-1.8.0-openjdk:JAVA 运行环境。
  11. python3:Python3 运行环境。
  12. perl:Perl 运行环境。

7、设置 root 用户的口令(用于通过 ssh 服务登录时使用)。

[root@e4599ea3ee0b /]#  passwd

8、启动 ssh 服务并设置为开机自启动。

[root@e4599ea3ee0b /]# systemctl enable sshd --now

9、创建默认的分离数据挂载目录。

[root@e4599ea3ee0b /]# mkdir /data

10、清除历史命令并退出。

[root@e4599ea3ee0b /]# history -c && exit

11、关闭容器,提交成新的镜像,删除容器(若适用)。

[centos@host ~]$ docker stop centos-extra
[centos@host ~]$ docker commit -m 'centos-extra' -a 'zhangyisoho' centos-extra centos-extra-x86_64
[centos@host ~]$ docker rm centos-extra
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 156,757评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,478评论 1 289
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,540评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,593评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,903评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,329评论 1 210
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,659评论 2 309
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,383评论 0 195
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,055评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,337评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,864评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,227评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,820评论 3 231
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,999评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,750评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,365评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,260评论 2 258

推荐阅读更多精彩内容