Docker安装

Linux上Docker安装


注:Docker CE 支持64位版本CentOS 7,并且要求内核版本不低于

yum安装

  1. 先删除旧的版本(如果没有可以跳过)

    $ sudo yum remove docker \
                      docker-client \
                      docker-client-latest \
                      docker-common \
                      docker-latest \
                      docker-latest-logrotate \
                      docker-logrotate \
                      docker-engine
    
  2. 安装必须的依赖:

    $ sudo yum install -y yum-utils \
      device-mapper-persistent-data \
      lvm2
    

    添加stable的Docker-ce的源:

    $ sudo yum-config-manager \
        --add-repo \
        https://download.docker.com/linux/centos/docker-ce.repo
    

    出错的话可以设置阿里源来完成

    $ sudo yum-config-manager \
        --add-repo \
        https://download.docker.com/linux/centos/docker-ce.repo
    
  3. 安装docker-ce:

    $ sudo yum install docker-ce docker-ce-cli containerd.io
    
  4. 选择指定的安装版本(可选)

    $ yum list docker-ce --showduplicates | sort -r
    
    docker-ce.x86_64  3:18.09.1-3.el7                     docker-ce-stable
    docker-ce.x86_64  3:18.09.0-3.el7                     docker-ce-stable
    docker-ce.x86_64  18.06.1.ce-3.el7                    docker-ce-stable
    docker-ce.x86_64  18.06.0.ce-3.el7                    docker-ce-stable
    

    我们来举个例子,比如我们要安装3:18.09.1-3.el7这个版本,使用如下命令结构:

    $ sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
    

    命令说明:

    第一部分是docker-ce,第二部分是版本号18.09.1,看明白了吗?就是这样子:

    $ sudo yum install -y docker-ce-18.09.1 docker-ce-cli-18.09.1
    
  5. 启动服务并测试一下:

    # 启动服务
    sudo systemctl start docker
    
    # 来一个Hello World吧
    sudo docker run hello-world
    Unable to find image 'hello-world:latest' locally
    latest: Pulling from library/hello-world
    1b930d010525: Pull complete
    Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
    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/
    

    如果看到上面的提示,说明Docker已经成功安装并运行了。

脚本安装

Docker官方为了简化安装流程,提供了一套便捷的安装脚本,CentOS系统上可以使用这套脚本安装

  1. 使用脚本安装

    curl -fsSL http://get.docker.com -o get-docker.sh
    sudo sh get-docker.sh --mirror Aliyun
    
  2. 启动Docker CE

    sudo systemctl enable docker		#设置开机启动
    sudo systemclt start docker
    

建立docker用户组

默认情况下,docker命令会使用Unix socket与Docker引擎通讯。而只有root用户和docker组用户才可以访问Docker引擎的Unix socket。一般Linux系统上不会直接使用root用户进行操作(用户权限太大不安全)。因此需要将docker的用户加入用户组。

  1. 建立docker组

    sudo groupadd docker				#建立docker组
    sudo usermod -aG docker $USER		#将当前用户加入docker组
    
  2. 测试docker是否安装正确

    docker run hello-world		#启动一个基于hello-word镜像的容器
    

升级&删除

升级:

# 更新所有
yum -y update

# 更新指定
yum -y update docker-ce docker-ce-cli containerd.io

删除:

# 删除docker安装包
sudo yum remove docker-ce

# 删除docker镜像
sudo rm -rf /var/lib/docker

镜像加速器

  1. 对于使用systemd的系统,在/etc/docker/daemon.json中写入如些内容(不存在请新建)

    #网易镜像
    {
    	"registry-mirrors":{
    		"http://hub-mirror.c.163.com"
    		}
    }
    
    #阿里云镜像
    {
      "registry-mirrors":["https://tixz0eg6.mirror.aliyuncs.com"]
    }
    
  2. 重启服务

    sudo systemctl daemon-reload
    sudo systemctl restart docker
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值