ubuntu 安装 docker
在 win10 上安装 docker 比较容易,一键安装即可。自从主机换成ubuntu
后,曾经好几次安装 docker 都失败了。当时也没在意,这次趁着datawhale
团队的组队学习,记录一下如何成功安装 docker。
我的系统版本是:
lulu@lulu-ThinkPad:~$ uname -a Linux lulu-ThinkPad 5.8.0-49-generic #55~20.04.1-Ubuntu SMP Fri Mar 26 01:01:07 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
在安装之前先卸载旧版本,旧版本 Docker 称为 docker
或者 docker-engine
,使用如下命令卸载:
$ sudo apt-get remove docker \
docker-engine \
docker.io
使用 apt 安装
sudo apt-get update
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release
众所周知的原因,docker 属于国外软件,从国内访问比较慢,可以使用国内案例的软件源。
保证下载软件包合法性,添加软件源 GPG
密钥
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
然后,修改 sources.list ,添加 docker 软件源
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null # 所有代码是一个整体
上述代码添加的是稳定版的镜像源,如果想体验新功能可以使用测试版,将 stable
替换为 test
安装 Docker
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
启动 Docker
sudo systemctl enable docker
sudo systemctl start docker
为 Docker 建立用户组
上次就是在这个地方遇到问题,怎么就是不能建立连接查阅文档才知道问题所在
因为在默认情况下,docker
命令会使用 unix socket 与 Docker 引擎通讯。而只有 root 用户和 dokcer 组的用户才可以访问 Docker 引擎的 unix socket。出于安全考虑,一般 linux 系统不允许用户直接使用 root 账户。因此,更好的做好就是将用户添加到 docker 组。
建立 docker 组:
sudo groupadd docker #添加 docker 用户组
sudo gpasswd -a $XXX docker #检测当前用户是否已经在 docker用户组中,其中 XXX 为用户名,例如我的,lulu
sudo gpasswd -a $USER docker #将当前用户添加至docker用户组
newgrp docker #更新docker用户组
测试是否正确安装
lulu@lulu-ThinkPad:~$ docker run --rm hello-world
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/
输出以上信息,证明安装正确。