查看系统发行版本
安装Docker的三种方式
- 设置Docker的存储库并从中安装,以便于安装和升级任务(推荐使用,本文主要是以这种方式进行Docker安装教学)。
- 下载DEB 包并 手动安装,完全手动管理升级。这在诸如在无法访问Internet的系统上安装 Docker 等情况下很有用。
- 在测试和开发环境中,一些用户选择使用自动化 便利脚本来安装 Docker。
使用存储库安装Docker【推荐使用】
在新主机上首次安装 Docker Engine 之前,您需要设置 Docker 存储库。之后,您可以从存储库安装和更新 Docker。
设置存储库
1、更新apt包索引:
注意:如果无法更新,可能是你的软件源指向是国外的服务器,很可能已经被墙。
2、安装必备的软件包以允许apt通过 HTTPS 使用存储库(repository):
sudo apt-get install ca-certificates curl gnupg lsb-release
3、添加Docker官方版本库的GPG密钥:
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
4、使用以下命令设置存储库:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
安装Docker
更新apt包索引,安装最新版本的Docker Engine、containerd 和 Docker Compose:
注意:安装可能需要花费一点时间,请耐心等待(假如安装进度条一直不动,可能需要切换成国内镜像源地址)。
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
使用Docker官方的源你很有可能和我一样很慢很慢一直卡在不动【那么你可以切切换成阿里云镜像源看看】
切换阿里云镜像源安装Docker
1、卸载可能存在的或者为安装成功的Docker版本
sudo apt-get remove docker docker-engine docker-ce docker.io
2、添加阿里云的GPG密钥
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
3、使用以下命令设置存储库
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
4、安装最新版本的Docker(飞速安装)
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
5、验证Docker是否安装成功
-- 查看docker 版本
docker version
启动Docker
1、安装完成后,运行如下命令验证 Docker 服务是否在运行
未运行:
2、运行以下命令启动Docker服务
sudo systemctl start docker
3、设置Docker服务在每次开机时自动启动
sudo systemctl enable docker
4、查看docker运行状态
验证Docker是否运行正常
注意:执行下面的命令会下载一个Docker测试镜像,并在容器中执行一个“hello-world”样例程序。
sudo docker run hello-world
如果你看到类似下方的输出,那么祝贺你,Docker能够正常运行在你的Ubuntu系统中了。
ubuntu@VM-16-10-ubuntu:~$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:53f1bbee2f52c39e41682ee1d388285290c5c8a76cc92b42687eecf38e0af3f0
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
参考文章
Install Docker Engine on Ubuntu
如何在 Ubuntu 22.04 LTS 中安装 Docker 和 Docker Compose