Docker安装
官方网址: www.docker.com/
OS系统版本选择:
Docker 目前已经支持多种操作系统的安装运行,比如Ubuntu、CentOS、Redhat、Debian、Fedora,甚至是还支持了Mac和Windows,在linux系统上需要内核版本在3.10或以上
Docker版本选择:
docker版本号之前一直是0.X版本或1.X版本,但是从2017年3月1号开始改为每个季度发布一次稳定版,其版本号规则也统一变更为YY.MM,例如17.09表示是2017年9月份发布的Docker之前没有区分版本,但是2017年推出(将docker更名为)新的项目Moby,github地址: github.com/moby/moby,Moby项目属于Docker项目的全新上游,Docker将是一个隶属于的Moby的子产品,而且之后的版本之后开始区分为 CE(Docker Community Edition,社区版本)和EE(Docker Enterprise Edition,企业收费版),CE社区版本和EE企业版本都是每个季度发布一个新版本,但是EE版本提供后期安全维护1年,而CE版本是4个月,以下为官方原文:
如果要布署到kubernets上,需要查看相关kubernets对docker版本要求的说明,比如:
安装可以参考以下文档
阿里云文档: developer.aliyun.com/mirror/dock…
官方文档 : docs.docker.com/engine/inst…
yum 安装
CentOS 6 因内核太旧,即使支持安装docker,但会有各种问题,不建议安装
CentOS 7 的 extras 源虽然可以安装docker,但包比较旧,建议从官方源或镜像源站点下载安装docker
CentOS 8 有新技术 podman 代替 docker
因此建议在CentOS 7 上安装 docker
#extras 源中包名为docker
[root@C7-1 ~]# yum list docker
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirror.nyist.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
可安装的软件包
docker.x86_64 2:1.13.1-210.git7d71120.el7.centos extras
官方地址下载 rpm包 download.docker.com/linux/cento…
阿里云地址下载 mirrors.aliyun.com/docker-ce/l…
通过yum源安装: 由于官网的yum源太慢,下面使用阿里云的Yum源进行安装
cd /etc/yum.repos.d/*
#CentOS 7 安装docker依赖三个yum源:Base,Extras,docker-ce
yum install -y yum-utils device-mapper-persistent-data lvm2
#安装 依赖工具
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#添加源信息
sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
#修改配置文件
yum makecache fast
yum list docker-ce
#查看最新版本
yum list docker-ce.x86_64 --showduplicates
···
docker-ce.x86_64 3:24.0.8-1.el7 docker-ce-stable
docker-ce.x86_64 3:24.0.9-1.el7 docker-ce-stable
docker-ce.x86_64 3:25.0.0-1.el7 docker-ce-stable
docker-ce.x86_64 3:25.0.1-1.el7 docker-ce-stable
docker-ce.x86_64 3:25.0.2-1.el7 docker-ce-stable
docker-ce.x86_64 3:25.0.3-1.el7 docker-ce-stable
docker-ce.x86_64 3:25.0.4-1.el7 docker-ce-stable
docker-ce.x86_64 3:25.0.5-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.0.0-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.0.1-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.0.2-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.1.0-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.1.1-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.1.2-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.1.3-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.1.4-1.el7 docker-ce-stable
#显示可安装版本
yum -y install docker-ce-26.1.4-1.el7 docker-ce-cli-26.1.4-1.el7
# 安装指定版本的Docker-CE和客户端,注意客户端也要指定,否则或造成不一致
#yum -y install docker-ce-[VERSION]
#如不指定版本默认最新
具体操作
cd /etc/yum.repos.d/
mkdir bak
mv *.repo bak/
修改阿里yum源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum list --showduplicates docker-ce
#查看所有的可用版本
yum list --help
#可以使用帮助查看
yum -y install docker-ce-26.1.4-1.el7 docker-ce-cli-26.1.4-1.el7
systemctl start docker
############验证运行#########
[root@localhost yum.repos.d]#systemctl start docker
[root@localhost yum.repos.d]#docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
719385e32844: Pull complete
Digest: sha256:88ec0acaa3ec199d3b7eaf73588f4518c25f9d34f58ce9a0df68429c5af48e8d
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/
yum安装2024版本
安装依赖包
[root@C7-1 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
| 软件 | 作用 |
|---|---|
| yum-utils: | 提供了 yum-config-manager 工具 |
| device mapper: | 是Linux内核中支持逻辑卷管理的通用设备映射机制,它为实现用于存储资源管理的块设备驱动提供了一个高度模块化的内核架构。 |
| device mapper | 存储驱动程序需要 device-mapper-persistent-data 和 lvm2 |
设置阿里云镜像源
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#下载yum仓库
安装docker
yum install -y docker-ce docker-ce-cli containerd.io
# 此处安装最新版 如果不想安装最新版
yum list --help
#可以使用帮助查看
yum list --showduplicates docker-ce
#查看所有的可用版本
....
docker-ce.x86_64 3:24.0.9-1.el7 docker-ce-stable
docker-ce.x86_64 3:25.0.0-1.el7 docker-ce-stable
docker-ce.x86_64 3:25.0.1-1.el7 docker-ce-stable
docker-ce.x86_64 3:25.0.2-1.el7 docker-ce-stable
docker-ce.x86_64 3:25.0.3-1.el7 docker-ce-stable
docker-ce.x86_64 3:25.0.4-1.el7 docker-ce-stable
docker-ce.x86_64 3:25.0.5-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.0.0-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.0.1-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.0.2-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.1.0-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.1.1-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.1.2-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.1.3-1.el7 docker-ce-stable
docker-ce.x86_64 3:26.1.4-1.el7 docker-ce-stable
yum -y install docker-ce-26.1.4-1.el7 docker-ce-cli-26.1.4-1.el7 containerd.io
| 软件 | 作用 |
|---|---|
| docker-ce(Docker Community Edition) | 这是 Docker 的社区版。Docker 社区版是免费的, 面向开发者、小型团队和个人使用。它包含了 Docker Engine, 这是一个用于构建和运行容器的开源容器运行时。 |
| docker-ce-cli(Docker Command Line Interface) | 这是 Docker 的命令行工具, 它允许用户与 Docker Engine 交互,执行容器相关的操作。 通过 Docker CLI,用户可以构建、运行、管理和发布容器。 |
| containerd.io | 它负责管理容器的生命周期,包括容器的创建、运行、暂停、继续和销毁等 |
apt安装
# step 1: 安装必要的一些系统工具
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# step 2: 安装GPG证书
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# Step 3: 写入软件源信息
sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# Step 4: 更新并安装Docker-CE
sudo apt-get -y update
sudo apt-get -y install docker-ce
# 安装指定版本的Docker-CE:
# Step 1: 查找Docker-CE的版本:
# apt-cache madison docker-ce
# docker-ce | 17.03.1~ce-0~ubuntu-xenial | https://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
# docker-ce | 17.03.0~ce-0~ubuntu-xenial | https://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
# Step 2: 安装指定版本的Docker-CE: (VERSION例如上面的17.03.1~ce-0~ubuntu-xenial)
# sudo apt-get -y install docker-ce=[VERSION]
二进制安装
下载地址
download.docker.com/linux/stati…
mirrors.tuna.tsinghua.edu.cn/docker-ce/l…
wget https://download.docker.com/linux/static/stable/x86_64/docker-26.1.4-1.tgz
tar xzvf
#解压安装包
cp docker/* /usr/bin/
#将二进制文件移动到可执行路径上的目录,例如/usr/bin/
dockerd &
#启动
docker run hello-world
#此命令下载测试映像并在容器中运行它
使用 systemctl 管理
vim /usr/lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start docker.service
#加速 http://cr.console.aliyun.com,
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://23b9pwsw.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
基础操作
docker程序环境
环境配置文件:
/etc/sysconfig/docker-network
/etc/sysconfig/docker-storage
/etc/sysconfig/docker
Unit File:
/usr/lib/systemd/system/docker.service
docker-ce 配置文件
/etc/docker/daemon.json
Docker Registry配置文件
/etc/containers/registries.conf
基础操作
查看docker 版本
docker 信息查看
[root@C7-1 docker]# docker info
Client: Docker Engine - Community
Version: 26.1.4
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc.)
Version: v0.14.1
Path: /usr/libexec/docker/cli-plugins/docker-buildx
compose: Docker Compose (Docker Inc.)
Version: v2.27.1
Path: /usr/libexec/docker/cli-plugins/docker-compose
Server:
Containers: 0 # 容器数量
Running: 0
Paused: 0
Stopped: 0
Images: 1 # 镜像数量
Server Version: 26.1.4 # server 版本
Storage Driver: overlay2 #docker 使用的是 overlay2 文件驱动
Backing Filesystem: xfs # 宿主机上的底层文件系统
Supports d_type: true
Using metacopy: false
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs # Cgroups 驱动
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 runc
Default Runtime: runc
Init Binary: docker-init
containerd version: d2d58213f83a351ca8f528a95fbd145f5654e957
runc version: v1.1.12-0-g51d5e94
init version: de40ad0
Security Options:
seccomp
Profile: builtin
Kernel Version: 3.10.0-693.el7.x86_64 # 宿主机的相关信息
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 8
Total Memory: 3.843GiB
Name: C7-1
ID: 609c3ea2-58eb-45d0-bebd-dac6f7f06429
Docker Root Dir: /var/lib/docker # docker 数据存储目录
Debug Mode: false
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://6ijb8ubo.mirror.aliyuncs.com/ # registry 地址
Live Restore Enabled: false
docker 配置文件
位置:/etc/docker/daemon.json
镜像仓库加速
镜像加速下载
浏览器访问 cr.console.aliyun.com/cn-hangzhou… 获取镜像加速器配置
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://1hxn8okv.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker
镜像管理
- 制作镜像
- 上传镜像
- 下载镜像
- 查找镜像
- 导出镜像
- 导入镜像
- 删除镜像
- 查询镜像
镜像即创建容器的模版,含有启动容器所需要的文件系统及所需要的内容,因此镜像主要用于方便和快速的创建并启动容器
镜像含里面是一层层的文件系统,叫做 Union FS(联合文件系统),联合文件系统,可以将几层目录挂载到一起(就像千层饼,洋葱头,俄罗斯套娃一样),形成一个虚拟文件系统,虚拟文件系统的目录结构就像普通 linux 的目录结构一样,镜像通过这些文件再加上宿主机的内核共同提供了一个 linux 的虚拟环境,每一层文件系统叫做一层 layer,联合文件系统可以对每一层文件系统设置三种权限,只读(readonly)、读写(readwrite)和写出(whiteout-able),但是镜像中每一层文件系统都是只读的,构建镜像的时候,从一个最基本的操作系统开始,每个构建提交的操作都相当于做一层的修改,增加了一层文件系统,一层层往上叠加,上层的修改会覆盖底层该位置的可见性,这也很容易理解,就像上层把底层遮住了一样,当使用镜像的时候,我们只会看到一个完全的整体,不知道里面有几层,实际上也不需要知道里面有几层,结构如下:
一个典型的 Linux文件系统由 bootfs 和 rootfs 两部分组成
bootfs(boot file system) 主要包含bootloader和kernel,bootloader主要用于引导加载 kernel,Linux刚启动时会加载bootfs文件系统,当boot加载完成后,kernel 被加载到内存中后接管系统的控制权,bootfs会被 umount 掉
rootfs (root file system) 包含的就是典型 Linux 系统中的/dev,/proc,/bin,/etc 等标准目录和文件,不同的 linux 发行版(如 ubuntu 和 CentOS ) 主要在 rootfs 这一层会有所区别。
一般的镜像通常都比较小,官方提供的Ubuntu镜像只有60MB多点,而 CentOS 基础镜像也只有200MB左右,一些其他版本的镜像甚至只有几MB,比如: busybox 才1.22MB,alpine镜像也只有5M左右。镜像直接调用宿主机的内核,镜像中只提供 rootfs,也就是只需要包括最基本的命令,配置文件和程序库等相关文件就可以了。
下图就是有两个不同的镜像在一个宿主机内核上实现不同的rootfs。、
命令总结:
| 命令 | 含义 | 例子 |
|---|---|---|
| docker --help | 查看帮助 | |
| docker image --help | 查看镜像帮助 | docker image --help |
| docker search 镜像名称 | 搜索相关镜像 | docker search nginx |
| docker pull 镜像名称:标签 | 下载镜像(不加标签默认使用latest) | docker pull nginx |
| docker images | 查看所有已有镜像 | docker images |
| docker images 镜像名称 | 查看单个镜像 | docker images nginx |
| docker inspect 镜像ID | 查看镜像的详细信息 | docker inspect 56b21e040954 |
| docker tag 旧名称 新名称 | 修改镜像的 标签 | docker tag nginx:latest lucky/nginx:1.25 |
搜索镜像
官方网站进行镜像的搜索
官网: hub.docker.com
镜像加速
阿里云获取加速地址
浏览器打开cr.console.aliyun.com,注册或登录阿里云账号,点击左侧的镜像加速器,将会得到一个专属的加速地址,而且下面有使用配置说明:
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://1hxn8okv.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker
Docker 镜像操作
搜索镜像
#格式
docker search 关键字
docker search nginx
#NAME: 列出了搜索结果中各个镜像的名称。
#DESCRIPTION: 描述了每个镜像的简要说明或介绍。
#STARS: 显示了每个镜像的星级评分或受欢迎程度。星级数量越高表示该镜像的受欢迎程度越高。
#OFFICIAL: 表示是否是官方构建的镜像,OK表示是官方构建的。
#AUTOMATED: 表示是否是自动构建的镜像,这个字段没有值表示不是自动构建的。
下载镜像
从 docker 仓库将镜像下载到本地,命令格式如下:
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
--platform string Set platform if server is multi-platform capable
-q, --quiet Suppress verbose output
#NAME: 是镜像名,一般的形式 仓库服务器:端口/项目名称/镜像名称
#:TAG: 即版本号,如果不指定:TAG,则下载最新版镜像 工作中指定版本号
获取镜像
#格式 docker pull 仓库名称[:标签] #如果下载镜像时不指定标签,则默认会下载仓库中最新版本的镜像,即选择标签为 latest 标签
docker pull nginx
查看镜像信息
镜像下载后存放在 /var/lib/docker 。
Docker 相关的本地资源存放在 /var/lib/docker/ 目录下,其中 containers 目录存放容器信息,image 目录存放镜像信息,overlay2 目录下存放具体的镜像底层文件。
查看下载的镜像文件信息
cat /var/lib/docker/image/overlay2/repositories.json
查看下载到本地的所有镜像
- REPOSITORY: 显示镜像的仓库名称
- TAG: 显示镜像的标签(版本)
- IMAGE ID: 显示镜像的唯一标识符
- CREATED: 显示镜像的创建时间
- SIZE: 显示镜像的大小
获取镜像详细信息
根据镜像的唯一标识 ID 号(IMAGE ID)获取
#格式 docker inspect 镜像ID号
#以上文中的IMAGR ID为例
docker inspect b605c77e624dd
- lowerdir是镜像层,目录或者文件是只读的,其实就是rootfs,image layer可以分很多层,所以对应的lowerdir是可以有多个目录
- upperdir是在lowerdir之上的容器层,这层是可读可写的,在启动一个容器时候会进行创建,所有的对容器数据更改都发生在这里层
- MergedDir是表现层,是容器的挂载点
为本地的镜像添加新的标签
#格式
docker tag 名称:[标签] 新名称:[新标签]
删除镜像
注意:如果该镜像已经被容器使用,正确的做法是先删除依赖该镜像的所有容器,再去删除镜像。
#格式
#方式一
docker rmi 仓库名称:标签 #当一个镜像有多个标签时,只是删除其中指定的标签
#方式二
docker rmi 镜像ID号 #会彻底删除该镜像
#举个例子,删除标签
web docker rmi nginx:
存出和载入镜像
存出镜像
将镜像保存为本地文件。
#格式
docker save -o 存储文件名 存储的镜像
docker save -o nginx nginx:latest #存出镜像命名为nginx存在当前目录下
ls -lh
载入镜像
将镜像文件导入到镜像库中。
#格式
#方式一
docker load < 存出的文件
#方式二
docker load -i 存出的文件
docker load < nginx
总结
docker镜像操作命令
docker search 仓库/镜像的关键词
pull 仓库名/镜像名:标签 #如果不指定标签默认使用 :latest 标签
images #列出本地已有的镜像
tags 旧镜像名:旧标签 新镜像名:新标签 #给镜像添加或修改标签
rmi 镜像名|镜像ID [-f] #删除指定的镜像
inspect 镜像名|镜像ID #查看镜像的详细信息
save -o 镜像文件 镜像名|镜像ID #将镜像保存到文件
load -i|< 镜像文件 #从文件中加载镜像
login #默认登录官方公有仓库
push 你的仓库名/镜像名:标签 #将镜像推送到仓库