当前位置: 首页 > 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.lryc.cn/news/608976.html

相关文章:

  • ubuntu syslog中appindicator报错解决
  • 扩散模型(一)——综述
  • Rust: 获取 MAC 地址方法大全
  • 【MySQL进阶】------MySQL程序
  • 机器学习第三课之逻辑回归(三)LogisticRegression
  • 2025H1具身智能产业十大数据
  • Python训练营打卡 Day27
  • 【网络安全】日志文件格式
  • Linux 系统调用 stat 完全用例
  • Web前端文件上传安全与敏感数据安全处理
  • HiveMQ核心架构思维导图2024.9(Community Edition)
  • 反向代理+网关部署架构
  • 动态置信度调优实战:YOLOv11多目标追踪精度跃迁方案(附完整代码)
  • 关于corn
  • Android 之 图片加载(Fresco/Picasso/Glide)
  • 禁闭求生2 免安 中文 离线运行版
  • 【数据结构与算法】数据结构初阶:排序内容加餐(二)——文件归并排序思路详解(附代码实现)
  • 【LeetCode 热题 100】84. 柱状图中最大的矩形——(解法一)单调栈+三次遍历
  • 二叉树的锯齿形层次遍历
  • 9.苹果ios逆向-FridaHook-ios中的算法(CCCrypt)
  • CCF-GESP 等级考试 2025年6月认证C++一级真题解析
  • wordpress登陆前登陆后显示不同的顶部菜单
  • 最简单的零基础软件测试学习路线
  • Libevent(5)之使用教程(4)工具
  • k8s黑马教程笔记
  • 快速搭建一个非生产k8s环境
  • 【运维基础】Linux 硬盘分区管理
  • k8s+isulad 国产化技术栈云原生技术栈搭建4-添加worker节点
  • Hyper-V + Centos stream 9 搭建K8s集群(二)
  • k8s+isulad 国产化技术栈云原生技术栈搭建3-master节点安装