Skip to content

第一章 安装与基础使用 v1.0 ​

第一部分 安装 ​

一、Windows/Mac ​

Windows与Mac系统推荐安装Docker Desktop

访问 Docker 官网,下载对应系统版本的Docker Desktop安装包安装即可

二、Linux ​

安装Docker Engine

Docker Engine 官方安装教程:CentOS

bash
# add repo
yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
if [ $? -ne 0 ]; then
    echo "add repo fail, exit..."
    exit 1
fi
# install
yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
if [ $? -ne 0 ]; then
    echo "install fail, exit..."
    exit 1
fi
# start docker and set docker to start automatically on boot
systemctl start docker
systemctl enable docker
bash
# add repo
dnf install -y dnf-utils
dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
if [ $? -ne 0 ]; then
    echo "add repo fail, exit..."
    exit 1
fi
# replace $releasever
sed -i 's/\$releasever/9/g' /etc/yum.repos.d/docker-ce.repo
# install
dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
if [ $? -ne 0 ]; then
    echo "install fail, exit..."
    exit 1
fi
# start docker and set docker to start automatically on boot
systemctl start docker
systemctl enable docker

三、卸载旧版本 ​

如果系统中安装过旧版本 Docker,可以先卸载旧版本:

bash
yum remove docker \
           docker-client \
           docker-client-latest \
           docker-common \
           docker-latest \
           docker-latest-logrotate \
           docker-logrotate \
           docker-engine

四、运行Hello World ​

bash
docker run hello-world

第二部分 基础使用 ​

Docker 的核心操作围绕:镜像(Image)和容器(Container)展开。

一、Docker三要素 ​

1.仓库(Repository) ​

存放镜像的地方,可以把镜像发布到仓库中,需要时再从仓库中拉取。

2.镜像(Image) ​

镜像是生成容器实例的模板,本身也是一个文件,称为镜像文件。

3.容器(Container) ​

一个容器通常运行一种服务,需要时通过 Docker 客户端根据镜像创建对应的运行实例。

二、基本使用流程 ​

  1. 拉取镜像:从Docker Hub(官方镜像仓库)或私有仓库下载镜像
  2. 创建容器:基于镜像创建可运行的容器实例
  3. 操作容器:启动、停止、进入容器,或在容器中执行命令
  4. 销毁资源:删除不再使用的容器或镜像,释放存储空间

三、基础使用 ​

示例:Docker使用Python镜像运行容器并执行HelloWorld程序

1、拉取基础镜像 ​

bash
docker pull <image-name>[:<tag>] # tag参数可选,默认为latest,即最新版
bash
docker images
bash
docker pull python:latest  # 拉取最新版本的Python镜像
docker images              # 可看到已拉取的python镜像信息

2、基于镜像创建并运行容器 ​

bash
docker run <image-name>[:<tag>] # tag参数可选,默认为latest,即最新版
bash
docker run --name python-container python:latest  # --name <name>: 指定当前容器名称

3、执行命令 ​

bash
docker exec <id/name> [command]
bash
docker exec -it <id/name> [bash]
bash
docker exec python-container python -c "print('hello world')"

四、镜像加速 ​

使用阿里云容器镜像服务时,可以在容器镜像服务获取镜像加速器地址。

Docker镜像加速配置

也可以编辑Linux系统中的/etc/docker/daemon.json:

json
{
    "registry-mirrors": ["https://xxx.mirror.aliyuncs.com"]
}

编辑后重启Docker:systemctl daemon-reload && systemctl restart docker

快速配置镜像源操作示例
bash
# write images repo
# 当前各大国内镜像站点陆续关停Docker镜像源,以下镜像源为互联网搜集,不一定仍可用
sudo tee /etc/docker/daemon.json <<-'EOF'
{
    "registry-mirrors": [
         "https://docker.m.daocloud.io",
         "https://docker.imgdb.de",
         "https://docker-0.unsee.tech",
         "https://docker.hlmirror.com",
         "https://docker.1ms.run",
         "https://func.ink",
         "https://lispy.org",
         "https://docker.xiaogenban1993.com"
    ]
}
EOF
# restart
systemctl daemon-reload && systemctl restart docker

五、常用服务操作 ​

1.查看Docker资源占用 ​

bash
docker system df

用于查看镜像、容器和数据卷占用的空间。

2.服务操作 ​

bash
# 启动Docker
systemctl start docker
# 停止Docker
systemctl stop docker
# 重启Docker
systemctl restart docker
# 查看Docker状态
systemctl status docker
# 设置开机启动
systemctl enable docker
# 查看Docker概要信息
docker info
# 查看具体Docker命令的帮助文档
docker <具体命令> --help

六、清理容器与镜像 ​

1.停止正在运行的容器 ​

bash
docker stop <id/name>
bash
docker stop python-container

2.删除容器 ​

bash
docker rm <id/name>
bash
docker rm python-container

3.删除镜像 ​

bash
docker rmi <image-name>[:<tag>]
bash
docker rmi python:latest