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

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.dtcms.com/a/313665.html

相关文章:

  • 7.28-8.3周报
  • 控制建模matlab练习10:滞后补偿器
  • OSPF笔记及综合实验报告册
  • 嵌入式 Linux 系统构建的核心组件详解
  • Go 工程化全景:从目录结构到生命周期的完整服务框架
  • 【openlayers框架学习】六:绘制点、圆、文字标注
  • 关于vllm【常见问题解决方案】
  • XtraBackup备份与恢复
  • Python 程序设计讲义(61):Python 的函数——变量的作用域
  • 【运维基础】Linux 硬盘分区管理
  • [Oracle] DUAL数据表
  • [自动化Adapt] 录制引擎 | iframe 穿透 | NTP | AIOSQLite | 数据分片
  • 第二节 YOLOv5参数
  • Python 程序设计讲义(59):Python 的函数——labmda函数(匿名函数)
  • 四、驱动篇-HDF驱动介绍2
  • sublime 乱码问题
  • JavaEE文件泄露与修复方案
  • Linux | i.MX6ULL移植 Gdb+Gdbserver 调试(第十四章)
  • 深入解析 Linux Kernel 中的设备树:使用、修改与实际应用
  • 经典文献阅读之--ViNT(视觉导航的基础模型)
  • 《汇编语言:基于X86处理器》第11章 MS-Windows编程(3)
  • 8.3 Java Web(JavaScript P15-P28)
  • Leetcode——365. 水壶问题
  • 决策树模型知识点整理:从原理到实战(含可视化与调参)
  • [硬件电路-134]:模拟电路 - 运算放大器常见运算:两模拟信号相加、相减、单模拟信号的积分、微分...
  • HTTPS的概念和工作过程
  • Ollama模型库模型下载慢完美解决(全平台)
  • 模型学习系列之参数
  • pytorch深度学习全流程:以简易数据、模型介绍
  • linux火焰图