Ubuntu系统安装Docker并更换镜像源
# 简介
Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源。
Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。
容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。
# 1.操作系统要求
支持以下 Ubuntu 系统版本:
- Eoan 19.10
- Bionic 18.04 (LTS)
- Xenial 16.04 (LTS)
# 2.卸载旧版本
安装前,卸载旧版本 Docker,执行命令 sudo apt-get remove docker docker-engine docker.io containerd runc
。
# 3.安装Docker
Docker 支持多种方式进行安装,这里我选择使用存储库进行安装。
- 更新apt包索引
sudo apt-get update
1
- 安装软件包以允许 apt 通过 HTTPS 使用存储库
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
1
2
3
4
5
6
2
3
4
5
6
- 添加 Docker 的官方 GPG 密钥
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
1
- 验证是否拥有带有指纹的密钥
sudo apt-key fingerprint 0EBFCD88
pub rsa4096 2017-02-22 [SCEA]
9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
uid [ 未知 ] Docker Release (CE deb) <docker@docker.com>
sub rsa4096 2017-02-22 [S]
1
2
3
4
5
2
3
4
5
- 设置 Docker 的存储库
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
1
2
3
4
2
3
4
- 安装 Docker
sudo apt-get install docker-ce docker-ce-cli containerd.io
1
- 验证是否安装成功
root@ubuntu:~# sudo docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
1
2
3
2
3
# 4.使用Docker创建一个NGINX容器
- 执行命令
docker run -it nginx
,会下载并运行容器。 - 执行命令
docker ps
,查看运行的 docker 容器。
root@ubuntu:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
165a6fb2ad38 nginx "nginx -g 'daemon of…" 2 hours ago Up 2 hours 80/tcp intelligent_herschel
1
2
3
2
3
- 执行命令
docker inspect 165a6fb2ad38
,查看 nginx 容器详情信息。 - 通过 nginx 容器信息,找到
IPAddress
对应的 IP,执行命令curl 172.17.0.2
测试 nginx 服务是否运行。 - 执行命令
docker exec -it 165a6fb2ad38 bash
,进入 nginx 容器。
上次更新: 2023/09/19, 09:25:06