Docker快速上手
·
1.安装Docker
删除Docker参照博客
【docker】ubuntu完全卸载docker及再次安装_ubuntu 卸载docker client卸载-CSDN博客
安装Docker参照
【Docker】2025在 Ubuntu 22.04上安装 Docker( 2025.6.24)-CSDN博客
一、准备工作
在开始之前,请确保您的系统是 Ubuntu 版本 22.04。可以使用以下命令来更新系统:
sudo apt update
sudo apt upgrade -y
二、检查系统版本
为了确认您的 Ubuntu 版本,您可以运行以下命令:
lsb_release -a
三、安装 Docker
1. 安装必要的依赖
在安装 Docker 之前,我们需要安装一些必要的依赖包。运行以下命令:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
# 备份并移除可能已损坏的密钥文件
sudo mv /usr/share/keyrings/docker-archive-keyring.gpg /usr/share/keyrings/docker-archive-keyring.gpg.bak 2>/dev/null
# 使用阿里云镜像源下载 GPG 密钥
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# 检查密钥是否正确导入
if [ -f /usr/share/keyrings/docker-archive-keyring.gpg ]; then
echo "GPG 密钥已成功导入"
else
echo "GPG 密钥导入失败,请检查网络连接"
exit 1
fi
# 设置 Docker 阿里云镜像源
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# 更新 apt 包索引并安装 Docker
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
# 验证 Docker 安装
sudo docker run hello-world
第一步是修改 Docker 的配置文件,指定使用一个镜像加速器。我们可以通过编辑 `/etc/docker/daemon.json` 配置文件,来改变 Docker 默认的镜像源。
#打开并编辑配置文件:
vim /etc/docker/daemon.json
添加镜像加速器配置
以下是配置内容:
{
"registry-mirrors": [
"https://docker.1ms.run",
"https://docker.1panel.live"
]
}
#以上是json格式的,对标点符号特别严格,新手在复制的时候注意这个问题。
这两个镜像源是我在多个测试环境中反复验证过的,确保它们能够提供快速且稳定的镜像拉取速度。
以下也可以尝试
https://docker.m.daocloud.io
https://mirror.baidubce.com
https://dytt.online
https://func.ink
https://docker.linkedbus.com
https://lispy.org
/etc/docker/daemon.json配置文件内容
{
#镜像源管理,我增加了阿里镜像源,可以提速
"registry-mirrors" : [
"https://jkfdsf2u.mirror.aliyuncs.com",
"https://registry.docker-cn.com"
],
#docker私服配置,因为不是https链接,所以在此配置。这步我没配置。
"insecure-registries" : [
"docker-registry.zjq.com"
],
#日志最大存储限制,如果不配置它,可能会导致/var/lib/docker/tmp/containers目录中的容器日志过大,挤占系统盘空间。
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "10"
},
#Docker运行时使用的根路径,默认 "/var/lib/docker"。根据自己情况
"data-root": "/data/docker"
}
2.Docker命令
帮助命令
docker version # 显示docker版本呢信息
docker info # 显示docker运行信息
docker 命令 --help # 帮助命令
镜像命令
docker images
查看本机上所有的镜像
/workspace git:(main) docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 74cc54e27dc4 5 months ago 10.1kB
# 解释
REPOSITORY: 项目的镜像源
TAG: 版本
# 可选项参数
Options:
-a, --all # 显示所有镜像
-q, --quiet # 只显示镜像ID
docker search
搜索镜像
# docker search 镜像名
➜ /workspace git:(main) docker search mysql
NAME DESCRIPTION STARS OFFICIAL
mysql MySQL is a widely used, open-source relation… 15838 [OK]
bitnami/mysql Bitnami container image for MySQL 137
docker pull
**下载镜像 **
# 下载镜像 docker pull 镜像名:[tag]
➜ /workspace git:(main) docker pull mysql
Using default tag: latest # 不写tag 默认最新版本
latest: Pulling from library/mysql
ed0a8990cecb: Pull complete # 分层下载
7af7a034b8b9: Pull complete
87129af36c90: Pull complete
10cecf14f09b: Pull complete
028720beb240: Pull complete
bfba8db0f2bd: Pull complete
ead6a62c24e9: Pull complete
d20478bf3bbf: Pull complete
7a26ee8a4dfe: Pull complete
4657dc0fd6ee: Pull complete
Digest: sha256:9a084cc73e7186283c564875e08d8af2c0e5c925333ad0a713f02fb1d826f78a # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址
# 例如可以指定mysql镜像版本
docker pull mysql:5.7
docker rmi
删除镜像
➜ /workspace git:(main) docker rmi 镜像ID # 删除docker镜像
➜ /workspace git:(main) docker rmi -f $(docker images -qa) # 删除所有镜像
# -f 参数代表强制删除
容器命令
只有现有镜像才能创建容器
docker pull centos
新建容器并启动(run)
docker run [可选参数] image
#参数说明
--name=""Name" # 指定容器名称
-d # 后台方式运行
-it # 使用交互方式运行,进入容器查看内容
-p # 指定容器的端口 -р 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-р 容器端ロ
-P #随机指定端口
列出容器(ps)
docker ps
# 参数说明
-a # 全部容器
-q # 只显示容器ID
-n=? # ?可用数字代替 显示最近的容器
退出容器
exit # 直接容器停止并退出
Ctrl + P + Q # 容不停止退出
删除容器(rm)
docker rm 容器ID # 删除容器
docker rm -f $(docker ps -aq) # 强制删除所有容器
# 注意 没有-f 的删除是不能删除正在运行中的容器的
启动和停止容器
docker start 容器ID # 启动容器
docker restart 容器ID # 重启容器
docker stop 容器ID # 停止正在运行的容器
docker kill 容器ID # 强制停止正在运行的容器
其他命令
查看日志信息(logs)
docker logs -t -f -tail 容器ID
# 参数信息
-tf # 查看所有日志信息
-tail 数字 # 查看数字条数部分信息
查看容器中进程信息(top)
docker top 容器ID
查看镜像元数据(inspect)
docker inspect 容器ID
进入容器(exec)
docker exec -it 容器ID # 常用 打开一个新的进程
docker attach 容器ID # 不常用 进入正在运行的容器
更多推荐
所有评论(0)