Linux 环境下 Docker 安装与简单使用指南
一、Docker 安装步骤
1. 卸载旧版本(如已安装)
sudo apt-get remove docker docker-engine docker.io containerd runc
输出(无旧版本时):
Reading package lists... Done
Building dependency tree
Reading state information... Done
Package 'docker' is not installed, so not removed
Package 'docker-engine' is not installed, so not removed
Package 'docker.io' is not installed, so not removed
Package 'containerd' is not installed, so not removed
Package 'runc' is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
2. 安装依赖包
sudo apt-get update && sudo apt-get install -y ca-certificates curl gnupg lsb-release
输出:
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
...
Fetched 25.3 kB in 1s (32.1 kB/s)
Reading package lists... Done
...
Setting up curl (7.68.0-1ubuntu2.14) ...
...
Processing triggers for man-db (2.9.1-1) ...
3. 添加 Docker 官方 GPG 密钥
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
4. 设置 Docker 稳定版仓库
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
5. 安装 Docker Engine
sudo apt-get update && sudo apt-get install -y docker-ce docker-ce-cli containerd.io
输出:
Hit:1 https://download.docker.com/linux/ubuntu focal InRelease
...
Setting up docker-ce (5:24.0.7-1~ubuntu.20.04~focal) ...
...
Processing triggers for systemd (245.4-4ubuntu3.21) ...
6. 验证安装是否成功
sudo docker --version
输出:
Docker version 24.0.7, build afdd53b
7. 启动 Docker 服务并设置开机自启
sudo systemctl start docker && sudo systemctl enable docker
输出:
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /lib/systemd/system/docker.service.
8. (可选)配置非 root 用户使用 Docker
sudo usermod -aG docker $USER
注意:执行后需重新登录系统生效
二、Docker 简单使用示例
1. 运行 hello-world 镜像(验证 Docker 功能)
sudo docker run hello-world
输出:
Hello from Docker!
This message shows that your installation appears to be working correctly.
...
For more examples and ideas, visit:
https://docs.docker.com/get-started/
2. 拉取 Ubuntu 镜像
sudo docker pull ubuntu:20.04
输出:
20.04: Pulling from library/ubuntu
f7b75fe1f73a: Pull complete
...
Digest: sha256:185fecb0a8305d9b50c489e92b441c0e5a75d40f82f2b0e355d15e56b63e8f
Status: Downloaded newer image for ubuntu:20.04
docker.io/library/ubuntu:20.04
3. 查看本地镜像列表
sudo docker images
输出:
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 20.04 54c9d81cbb44 2 weeks ago 72.8MB
hello-world latest d2c94e258dcb 5 months ago 13.3kB
4. 启动并进入 Ubuntu 容器
sudo docker run -it ubuntu:20.04 /bin/bash
输出(进入容器交互模式):
root@7f9c8a2b3c4d:/#
5. 在容器内执行命令
# 在容器内执行
apt-get update && apt-get install -y vim && vim --version
输出:
...
VIM - Vi IMproved 8.1 (2018 May 18, compiled Apr 15 2020 06:40:31)
Included patches: 1-2269
...
6. 退出容器
# 在容器内执行
exit
输出:
exit
7. 查看正在运行的容器
sudo docker ps
输出(无运行中容器时):
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8. 查看所有容器(包括已停止)
sudo docker ps -a
输出:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7f9c8a2b3c4d ubuntu:20.04 "/bin/bash" 5 minutes ago Exited (0) 2 minutes ago hopeful_montalcini
a1b2c3d4e5f6 hello-world "/hello" 10 minutes ago Exited (0) 10 minutes ago determined_jones
9. 停止容器(如果容器在运行)
sudo docker stop 7f9c8a2b3c4d # 使用容器ID或名称
输出:
7f9c8a2b3c4d
10. 删除容器
sudo docker rm 7f9c8a2b3c4d
输出:
7f9c8a2b3c4d
11. 删除镜像
sudo docker rmi ubuntu:20.04
输出:
Untagged: ubuntu:20.04
Untagged: ubuntu@sha256:185fecb0a8305d9b50c489e92b441c0e5a75d40f82f2b0e355d15e56b63e8f
Deleted: sha256:54c9d81cbb4406c3fb16cbbf51e51a1227a088e06718103e4ea037697a8ac