Docker合集(二)Docker安装配置

Docker合集(二)Docker安装配置

原创不易,转载请注明来源。有疑问欢迎大佬提出。

随便说点什么

Docker支持很多的操作系统,这篇会详细的说明如何在主流的操作系统中安装Docker引擎,并且配置Docker。

我们可以访问docker的官方网站docker.com去查看安装Docker的方式,以及一些文档。

在下文的安装介绍中,会详细列出经过测试的系统和相关软件版本

安装Docker Engine

MacOS

Docker 官方非常重视其在 Mac 环境下的易用性,对 Mac 用户非常友好。

在 Mac 上我们可以用两种方式来安装 Docker。

  • 使用 brew

    同样的,我们在命令行使用命令。

    brew update \
    && brew install --cask docker
    

    但是由于国内网络原因,使用 brew 安装的时候,经常会很慢,还经常会失败。

    我们也可以选择下面的第二种方式:去官网下载 .dmg 的安装包来安装

  • 下载 .dmg 安装

    访问docker官网的下载地址:https://docs.docker.com/docker-for-mac/install

  • 验证是否安装成功

    # 输入 sudo docker run hello-world
    sudo docker run hello-world
    # 安装成功会有类似以下的数据,失败则会提示 docker 命令不存在
    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/
    

Windows

  • 去官网下载安装:https://www.docker.com/get-started

CentOS

更详细的安装可参考官方文档

在CentOS上我们使用yum安装。

  • 通过测试的环境

    Docker版本Operating SystemKernel Version
    1.13.1CentOS Linux 73.10.0-1160.15.2.el7.x86_64

可以有以下几步:

直接拷贝命令执行即可

  1. 尝试卸载旧版本,旧版本的名称可能叫做:docker、docker-engine、docker.io 其中任何一个。

    sudo yum remove -y docker \
                      docker-client \
                      docker-client-latest \
                      docker-common \
                      docker-latest \
                      docker-latest-logrotate \
                      docker-logrotate \
                      docker-engine
    
  2. 安装docker

    sudo yum -y update \
    && sudo yum -y install docker
    
  3. 启动

    sudo systemctl start docker
    
  4. 验证是否安装成功

    # 输入 sudo docker run hello-world
    sudo docker run hello-world
    # 安装成功会有类似以下的数据,失败则会提示 docker 命令不存在
    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/
    

Ubuntu

更详细的安装可参考官方文档

在Ubuntu上我们使用apt安装。

  • 通过测试的环境

    Docker版本Ubuntu系统版本
    19.03.8Ubuntu 20.04.1 LTS

可以有以下几步:

直接拷贝命令执行即可

  1. 尝试卸载旧版本,旧版本的名称可能叫做:docker、docker-engine、docker.io 其中任何一个。

    sudo apt -y remove docker; \
    sudo apt -y remove docker-engine; \
    sudo apt -y remove docker.io
    
  2. 安装docker

    sudo apt -y update \
    && sudo apt -y install docker.io
    
  3. 验证是否安装成功

    成功安装后即可使用 docker命令

    # 输入 sudo docker run hello-world
    sudo docker run hello-world
    # 安装成功会有类似以下的数据,失败则会提示 docker 命令不存在
    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 服务

无需 sudo 使用docker命令

原理是赋予当前用户操作docker的权限

在《Docker技术入门与实战 第三版》中的描述如下:

但是没有说到关键点和原理,当我们按照书本中的讲解输入命令的时候,可能出现以下情况:

书中的描述是让我们将当前用户加入docker用户组中,这个用户组是安装docker时自动创建的。

而实际情况下,在某些系统安装docker的时候并不会自动创建这个用户组,例如CentOS Linux release 7.9.2009 (Core)

实际上想使用docker命令,就需要对/var/run/docker.sock文件有操作权限。

我们先进入/var/run目录查看docker.sock文件的权限。

我们看到这里对docker.sock文件有权限的用户组是root。

我们可以把需要的用户加入到root用户组来实现免root使用docker,但是这里做并没有意义

我们先看一下安装docker时给我们创建了哪些用户和组。

vim /etc/group

搜索一下,发现跟docker相关的用户组只有dockerroot

那么我们把 docker.sock 的用户组修改为 dockerroot 如何?然后再把当前用户加入 dockerroot 用户组。

# 修改 docker.sock 用户组
chgrp dockerroot docker.sock
# 将用户 hujinwen 加入用户组 dockerroot
usermod -aG dockerroot hujinwen

退出用户登录,再重新登录。发现用户可以免root使用docker命令了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值