【Docker】二、安装

173 阅读2分钟

官方安装文档

以下以Ubuntu系统为例

版本要求 64位系统

  • Ubuntu Impish 21.10
  • Ubuntu Hirsute 21.04
  • Ubuntu Focal 20.04 (LTS)
  • Ubuntu Bionic 18.04 (LTS)

卸载旧版本

sudo apt remove docker docker-engine docker.io containerd runc

设置存储库

  1. 更新apt并允许Docker资源
sudo apt update
sudo apt install \
    ca-certificates \ 
    curl \
    gnupg \
    lsb-release
  1. 加Docker的官方GPG秘钥
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
  1. 使用以下命令设置稳定存储库
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 stable

安装Docker引擎

  1. 更新apt并安装最新版的Docker引擎和容器
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io
  1. 启动Docker
systemctl start docker
  1. 检查Docker是否安装成功,并且已启动
docker version

docker-version.png

  1. 通过运行镜像验证Docker引擎是否已正确安装
sudo docker run hello-world

第一次运行会提示没有该镜像,并去拉去镜像再启动

root@lsMusKVEqm:~# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
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/
  1. 查看已安装的镜像
docker images

Docker镜像

修改阿里云镜像

创建daemon.json 并键入以下内容

vim /etc/docker/daemon.json

内容:

{
  "registry-mirrors": ["https://3laho3y3.mirror.aliyuncs.com"]
}

重启Docker

systemctl daemon-reload 
systemctl restart docker

卸载docker

# 1.卸载软件
apt purge docker-ce docker-ce-cli containerd.io
# 2.删除目录
rm -rf /var/lib/docker
rm -rf /var/lib/containerd