Docker:Ubuntu20.04下的安装


官网教程: https://docker-curriculum.com/.
菜鸟教程: https://www.runoob.com/docker/docker-tutorial.html.

一、什么是Docker

Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源。

Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。

容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。

二、Docker的安装

根据官方文档的说明,Docker的安装有三种方式,我们选择官方推荐的方式进行安装。

1. 卸载旧版本

Docker 的旧版本被称为 docker,docker.io 或 docker-engine 。如果已安装,请卸载它们:

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

2.设置仓库

(1) 更新 apt 包索引

sudo apt-get update

(2) 获取仓库

sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

(3) 添加 Docker 的官方 GPG 密钥

curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

(4) 验证是否拥有带有指纹的密钥

sudo apt-key fingerprint 0EBFCD88

出现以下信息则正确:

pub   rsa4096 2017-02-22 [SCEA]
      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid           [ unknown] Docker Release (CE deb) <docker@docker.com>
sub   rsa4096 2017-02-22 [S]

(5) 设置稳定版仓库

sudo add-apt-repository \
   "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/ \
  $(lsb_release -cs) \
  stable"

3.设置仓库安装 Docker Engine-Community

(1) 更新 apt 包索引

sudo apt-get update

(2) 安装最新版本的 Docker Engine-Community 和 container

sudo apt-get install docker-ce docker-ce-cli containerd.io

4.测试

sudo docker run hello-world

安装正确会如下输出:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:8e3114318a995a1ee497790535e7b88365222a21771ae7e53687ad76563e8e76
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/

三、以非root 用户管理 Docker

1.将用户添加到docker组

 sudo usermod -aG docker $USER

2.激活对组的更改:

 newgrp docker 

3.验证

 docker run hello-world
 

四、Compose 安装

运行以下命令以下载 Docker Compose 的当前稳定版本:

$ sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

将可执行权限应用于二进制文件:

$ sudo chmod +x /usr/local/bin/docker-compose

创建软链:

$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

测试是否安装成功:

$ docker-compose --version

出现版本信息:

docker-compose version 1.29.2, build 5becea4c

五、配置 Docker 镜像加速

国内从 DockerHub 拉取镜像有时会遇到困难,此时可以配置镜像加速器。Docker 官方和国内很多云服务商都提供了国内加速器服务.
阿里云镜像获取地址: https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors.
按照以下命令配置:

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["你自己的加速器地址"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
newgrp docker 

出现以下信息则成功:

 Registry Mirrors:
  https://xmpiiaf3.mirror.aliyuncs.com/

六、配置 Docker 开机启动

大多数当前的 Linux 发行版(RHEL、CentOS、Fedora、Debian、Ubuntu 16.04 及更高版本)用于systemd管理系统启动时启动的服务。在 Debian 和 Ubuntu 上,Docker 服务默认配置为在启动时启动。要在引导时为其他发行版自动启动 Docker 和 Containerd,请使用以下命令:

 sudo systemctl enable docker.service
 sudo systemctl enable containerd.service

要禁用此行为,请改用disable:

 sudo systemctl disable docker.service
 sudo systemctl disable containerd.service

七、Docker的卸载

删除安装包:

sudo apt-get purge docker-ce

删除镜像、容器、配置文件等内容:

sudo rm -rf /var/lib/docker
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值