当前位置: 首页 > ops >正文

Linux 环境下 Docker 安装与简单使用指南

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
http://www.xdnf.cn/news/16883.html

相关文章:

  • 百度招黑产溯源安全工程师
  • 《软件测试与质量控制》实验报告二 单元测试
  • MSQL-聚簇索引与非聚簇索引的比较
  • Python编程基础与实践:Python文件处理入门
  • SpringBoot 信用卡检测、OpenAI gym、OCR结合、DICOM图形处理、知识图谱、农业害虫识别实战
  • 【7.5 Unity AssetPostprocessor】
  • 【自动化运维神器Ansible】YAML支持的数据类型详解:构建高效Playbook的基石
  • linux ext4缩容home,扩容根目录
  • Trae + Notion MCP:将你的Notion数据库升级为智能对话机器人
  • 元宇宙重构未来交通新图景
  • 无人机光伏巡检漏检率↓78%!陌讯多模态融合算法实战解析
  • 机试备考笔记 2/31
  • Agentic RAG:自主检索增强生成的范式演进与技术突破
  • 深入 Go 底层原理(二):Channel 的实现剖析
  • 深入 Go 底层原理(十四):timer 的实现与高性能定时器
  • cuda编程笔记(12)--学习cuFFT的简单使用
  • 【机器学习】非线性分类算法(上):KNN(基于距离相似度)与朴素(特征独立)贝叶斯(基于概率统计)
  • Lock 接口及实现类详解:从 ReentrantLock 到并发场景实践
  • Node.js 操作 MongoDB
  • 【LeetCode 热题 100】739. 每日温度——(解法一)单调栈+从右到左
  • 最新Windows11系统镜像,23H2 64位ISO镜像
  • 拉格朗日插值法
  • 【软考中级网络工程师】知识点之堆叠
  • MySQL PostgreSQL JDBC URL 配置允许批量操作
  • 系统思考:超越线性分析
  • openwrt下安装istore(基于pve)
  • Linux网络编程【基于UDP网络通信的字典翻译服务】
  • Effective C++ 条款17:以独立语句将newed对象置入智能指针
  • 农田通量计算方法与应用;高精度感热/潜热通量反演与绘图等;农田蒸散发与能量平衡
  • 50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | QuizApp(交互式在线测验应用组件)