先决条件
- 系统要求
要安装 Docker 引擎,您需要 CentOS 7 或 8 的维护版本。存档版本不受支持或测试。 必须启用存储库。默认情况下,此存储库处于启用状态,但如果已禁用它,则需要重新启用它。centos-extras 建议使用存储驱动程序。overlay2
- 卸载旧版本
Docker 的较旧版本被调用或 。如果安装了这些组件,请卸载它们以及关联的依赖项。dockerdocker-engine
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
安装方法
- 大多数用户设置 Docker 的存储库并安装这些存储库,以便于安装和升级任务。这是推荐的方法。
- 一些用户下载 RPM 包并手动安装它,并完全手动管理升级。这在无法上网的空隙系统上安装 Docker 时非常有用。
- 在测试和开发环境中,一些用户选择使用自动便利脚本来安装 Docker。
使用存储库
在首次安装 Docker 引擎之前,您需要设置 Docker 存储库。之后,可以从存储库安装和更新 Docker。
- 设置存储库 安装包(提供实用程序)并设置稳定的存储库。yum-utilsyum-config-manager
$ sudo yum install -y yum-utils
$ sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
安装Docker
- 安装最新版本的Docker 引擎和容器,或转到下一步以安装特定版本:
$ sudo yum install docker-ce docker-ce-cli containerd.io
- 要安装特定版本的Docker 引擎,请在存储库中列出可用版本,然后选择并安装: 1.列出和排序存储库中可用的版本。本示例按版本号、从高到低对结果进行排序,并截断:
$ yum list docker-ce --showduplicates | sort -r
docker-ce.x86_64 3:20.10.2-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.0-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.9-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.8-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.7-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.6-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.5-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.4-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.3-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.2-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.14-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.13-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.12-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.11-3.el7 docker-ce-stable
...
...
2.通过完全限定的包名称安装特定版本,即包名称 () 加上版本字符串 (第 2 列),从第一个冒号 ()开始,一开始,一个连字符,用连字符 () 分隔。例如 , 。docker-ce:-docker-ce-18.09.1
$ sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
3.启动 Docker
$ sudo systemctl start docker
4.通过运行映像验证 Docker 引擎安装是否正确。hello-world
$ sudo docker run hello-world
卸载Docker
1.卸载 Docker 引擎、CLI 和容器包:
$ sudo yum remove docker-ce docker-ce-cli containerd.io
2.主机上的图像、容器、卷或自定义配置文件不会自动删除。要删除所有图像、容器和卷:
$ sudo rm -rf /var/lib/docker
Docker删除镜像、容器
- 删除镜像
sudo docker rmi <镜像id或名称>
- 删除容器
sudo docker rmi <容器id或名称>
CentOS7实操
- 安装最新版本
sudo yum install -y docker-ce docker-ce-cli containerd.io
查看版本
# docker version
Client: Docker Engine - Community
Version: 20.10.2
API version: 1.41
Go version: go1.13.15
Git commit: 2291f61
Built: Mon Dec 28 16:17:48 2020
OS/Arch: linux/amd64
Context: default
Experimental: true
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
启动、状态、停止操作
# 启动
sudo systemctl start docker
# 状态
sudo systemctl status docker
docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: active (running) since 三 2021-01-06 15:08:56 CST; 12s ago
Docs: https://docs.docker.com
Main PID: 12965 (dockerd)
Tasks: 15
Memory: 49.8M
CGroup: /system.slice/docker.service
└─12965 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
1月 06 15:08:55 localhost.localdomain dockerd[12965]: time="2021-01-06T15:08:55.976352192+08:00" level=info msg="scheme \"unix\" not registered, fallback to default scheme" module=grpc
1月 06 15:08:55 localhost.localdomain dockerd[12965]: time="2021-01-06T15:08:55.976400676+08:00" level=info msg="ccResolverWrapper: sending update to cc: {[{unix:///run/containerd/containerd.sock <nil> 0...}" module=grpc
1月 06 15:08:55 localhost.localdomain dockerd[12965]: time="2021-01-06T15:08:55.976413942+08:00" level=info msg="ClientConn switching balancer to \"pick_first\"" module=grpc
1月 06 15:08:56 localhost.localdomain dockerd[12965]: time="2021-01-06T15:08:56.014464634+08:00" level=info msg="Loading containers: start."
1月 06 15:08:56 localhost.localdomain dockerd[12965]: time="2021-01-06T15:08:56.376846182+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip ...ed IP address"
1月 06 15:08:56 localhost.localdomain dockerd[12965]: time="2021-01-06T15:08:56.449364176+08:00" level=info msg="Loading containers: done."
1月 06 15:08:56 localhost.localdomain dockerd[12965]: time="2021-01-06T15:08:56.503601105+08:00" level=info msg="Docker daemon" commit=8891c58 graphdriver(s)=overlay2 version=20.10.2
1月 06 15:08:56 localhost.localdomain dockerd[12965]: time="2021-01-06T15:08:56.503830974+08:00" level=info msg="Daemon has completed initialization"
1月 06 15:08:56 localhost.localdomain systemd[1]: Started Docker Application Container Engine.
1月 06 15:08:56 localhost.localdomain dockerd[12965]: time="2021-01-06T15:08:56.533783267+08:00" level=info msg="API listen on /var/run/docker.sock"
Hint: Some lines were ellipsized, use -l to show in full.
# 停止
sudo systemctl stop docker
Warning: Stopping docker.service, but it can still be activated by:docker.socket
Hello-World验证
sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:1a523af650137b8accdaed439c17d684df61ee4d74feac151b5b337bd29e7eec
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/
Docer的镜像、容器
# 拉取镜像
sudo docker pull <镜像名>
# 查看Docker中的所有镜像
sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 12 months ago 13.3kB
# 查看Docker中所有的容器
sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e1428020fb1d hello-world "/hello" 17 minutes ago Exited (0) 17 minutes ago inspiring_kirch
# 查看Docker中运行中的容器(这里没有运行中的)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
拉取镜像
docker search [option] keyword
# docker search --help
Usage: docker search [OPTIONS] TERM
Search the Docker Hub for images
Options:
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print search using a Go template
--limit int Max number of search results (default 25)
--no-trunc Don't truncate output
举个列子,比如我们想搜索官方提供的 mysql 镜像,命令如下:
# docker search --filter is-official=true mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10339 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3832 [OK]
percona Percona Server is a fork of the MySQL relati… 518 [OK]
查看镜像版本需要到DockerHub搜索mysql关键字,查看Tags标签
- Docker 镜像加速
国内从 DockerHub 拉取镜像有时会遇到困难,此时可以配置镜像加速器。Docker 官方和国内很多云服务商都提供了国内加速器服务,例如:
中国区官方:registry.docker-cn.com 阿里云:https://<你的ID>.mirror.aliyuncs.com
Ubuntu16.04+、Debian8+、CentOS7,在 /etc/docker/daemon.json 中写入如下内容(如果文件不存在请新建该文件):
{"registry-mirrors":["https://registry.docker-cn.com"]}
mysql5.7镜像(未配置镜像加速有点慢,但还行)
# sudo docker pull mysql:5.7.32
5.7.32: Pulling from library/mysql
6ec7b7d162b2: Pull complete
fedd960d3481: Pull complete
7ab947313861: Pull complete
64f92f19e638: Pull complete
3e80b17bff96: Pull complete
014e976799f9: Pull complete
59ae84fee1b3: Pull complete
7d1da2a18e2e: Pull complete
301a28b700b9: Pull complete
529dc8dbeaf3: Pull complete
bc9d021dc13f: Pull complete
Digest: sha256:c3a567d3e3ad8b05dfce401ed08f0f6bf3f3b64cc17694979d5f2e5d78e10173
Status: Downloaded newer image for mysql:5.7.32
docker.io/library/mysql:5.7.32
运行mysql
运行:
#运行 Mysql 容器
docker run -d 3306:3306 --name mysql5.7.32 -e MYSQL_ROOT_PASSWORD=123456 mysql5.7.32
#运行 Mysql 容器,映射目录,设置必须 Mysql 参数
docker run -d -p 3306:3306 --name mysql5.7.32 \
-v /home/dockermysql/conf:/etc/mysql \
-v /home/dockermysql/logs:/var/log/mysql \
-v /home/dockermysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
mysql5.7.32 \
--lower_case_table_names=1 \
--max-allowed-packet=1073741824 \
--character_set_server=utf8 \
--innodb_log_file_size=256m
2f63f45453b31fcb61a2708643e77575fb734ad2234084502b46dbf4249fa638
命令说明:
--name mysql5.7.32
:将要创建容器名称
-p 3306:3306
:将容器的3306端口映射到主机的3306端口
-v /home/dockermysql/conf:/etc/mysql
:将主机/home/dockermysql/conf
目录挂载到容器的/etc/mysql
...等等
-e MYSQL_ROOT_PASSWORD=123456
:初始化root用户的密码
-d
:后台运行容器,并返回容器ID
mysql:5.7.32
: mysql镜像,本地没有会从网络上拉取
--lower_case_table_names=1
:设置表名参数等忽略大小写
--max-allowed-packet=1073741824
:设置最大插入和更新数据限制为1G,这里单位:字节
--character_set_server=utf8
:设置utf8字符集
--innodb_log_file_size=256m
:设置日志大小
查看:
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ae1e7049425b mysql:5.7.32 "docker-entrypoint.s…" 54 seconds ago Up 53 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysql5.7.32