ubuntu 安装 docker

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 建立用户组

上次就是在这个地方遇到问题,怎么就是不能建立连接查阅文档才知道问题所在

image-20210411113842428
image-20210411113842428

因为在默认情况下,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用户组
image-20210411114054679
image-20210411114054679

测试是否正确安装

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/

输出以上信息,证明安装正确。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值