如何在Linux上安装Docker

29 篇文章 0 订阅
23 篇文章 0 订阅

官方文档

地址:link
可以根据官方教程安装,本文以Ubuntu 18.04.1操作系统为例。

卸载已安装的docker

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

创建仓库

  1. 更新apt索引包
sudo apt-get update
  1. 添加Docker官方GPG key
 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
  1. 设置稳定仓库
echo \
  "deb [arch=amd64 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

安装Docker engine

在安装docker engine之前务必要更新apt包索引,不然后面安装时候会找不到软件:

正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
没有可用的软件包 docker-ce,但是它被其它的软件包引用了。
这可能意味着这个缺失的软件包可能已被废弃,
或者只能在其他发布源中找到

E: 软件包 docker-ce 没有可安装候选
E: 无法定位软件包 docker-ce-cli
E: 无法定位软件包 containerd.io
E: 无法按照 glob ‘containerd.io’ 找到任何软件包
E: 无法按照正则表达式 containerd.io 找到任何软件包

安装Docker Engine and containerd

sudo apt-get update                                                                   //必须再次更新apt索引包
sudo apt-get install docker-ce docker-ce-cli containerd.io

查看Docker 版本

docker version
Client: Docker Engine - Community
 Version:           20.10.8
 API version:       1.41
 Go version:        go1.16.6
 Git commit:        3967b7d
 Built:             Fri Jul 30 19:54:08 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.8
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.6
  Git commit:       75249d8
  Built:            Fri Jul 30 19:52:16 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.9
  GitCommit:        e25210fe30a0a703442421b0f60afac609f950a3
 runc:
  Version:          1.0.1
  GitCommit:        v1.0.1-0-g4144b63
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

仅仅说明安装成功而已,下面进行启动docker和测试。

启动Docker

systemctl start docker

查看Docker状态

systemctl status docker

出现下面这个active状态说明docker已启动

 docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: e
   Active: active (running) since Sat 2021-09-25 21:53:15 CST; 12h ago
     Docs: https://docs.docker.com
 Main PID: 16553 (dockerd)
    Tasks: 30
   CGroup: /system.slice/docker.service
           └─16553 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/contai

运行 hello-world镜像

sudo docker run hello-world

如果出现Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world docker: Get "https://registry-1.docker.io/v2/library/hello-world/manifests/sha256:f54a58bc1aac5ea1a25d796ae155dc228b3f0e11d046ae276b39c4bf2f13d8c4": EOF. See 'docker run --help'.说明终端卡在Unable to find image ‘hello-world:latest’ locally位置 。
docker在本地没有找到hello-world镜像,也没有从docker仓库中拉取镜像,出项这个问题的原因:docker服务器在国外,我们在国内无法正常拉取镜像,所以就需要我们为docker设置国内阿里云的镜像加速器

添加阿里云镜像加速器

  1. 创建daemon.json文件
cd  /etc/docker/
touch /etc/docker/daemon.json
  1. 在daemon.json文件中写入阿里云镜像地址
vim daemon.json       //编辑文件
{ 
"registry-mirrors": ["https://alzgoonw.mirror.aliyuncs.com"] 
}                                           //写入到daemon.json中,最后wq保存
  1. 重新加载和重启docker
systemctl daemon-reload
systemctl restart docker

再次运行hello-world镜像

sudo docker run 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/

查看docker状态

systemctl status docker

查看docker状态

Linux安装Docker的教程结束了,其他的使用方法和问题后期会补充。欢迎大家转载,收藏。

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值