Centos7 安裝 docker

docker是什么?

? Docker 是一個開源的應用容器引擎,讓開發者可以打包他們的應用以及依賴包到一個可移植的容器中,然后發布到任何流行的Linux機器上,也可以實現虛擬化,容器是完全使用沙箱機制,相互之間不會有任何接口。

Docker 包括三個基本概念:

  • 鏡像(Image):Docker 鏡像(Image),就相當于是一個 root 文件系統。比如官方鏡像 ubuntu:16.04 就包含了完整的一套 Ubuntu16.04 最小系統的 root 文件系統。
  • 容器(Container):鏡像(Image)和容器(Container)的關系,就像是面向對象程序設計中的類和實例一樣,鏡像是靜態的定義,容器是鏡像運行時的實體。容器可以被創建、啟動、停止、刪除、暫停等。
  • 倉庫(Repository):倉庫可看著一個代碼控制中心,用來保存鏡像。

docker的安裝

  1. 查看內核(建議切換到root用戶進行安裝docker)

Docker 運行在CentOS 7 上,要求系統為64位、系統內核版本為 3.10 以上。

Docker 運行在 CentOS-6.5 或更高的版本的 CentOS 上,要求系統為64位、系統內核版本為 2.6.32-431 或者更高版本。

[root@localhost ~]# uname -r
3.10.0-957.el7.x86_64
[root@localhost ~]# 
  1. 把yum包更新到最新
[root@localhost ~]# yum update
  1. 卸載舊版本(如果安裝過舊版本的話,第一次安裝不用管這一步)

    查看是否安裝了docker
    [root@localhost ~]# yum list installed | grep docker
    
    較舊的 Docker 版本稱為 docker 或 docker-engine 。如果已安裝這些程序,請卸載它們以及相關的依賴項。從 2017 年 3 月開始 docker 在原來的基礎上分為兩個分支版本: Docker CE 和 Docker EE。
    Docker CE 即社區免費版,Docker EE 即企業版,強調安全,但需付費使用。
    [root@localhost ~]#  yum remove docker \
                      docker-client \
                      docker-client-latest \
                      docker-common \
                      docker-latest \
                      docker-latest-logrotate \
                      docker-logrotate \
                      docker-engine
                      
    如果是較新版本采用下面的方式進行卸載
    [root@localhost ~]#  yum remove docker-ce
    
    刪除鏡像文件、容器、掛載目錄、自定義配置文件等
    [root@localhost ~]#  rm -rf /var/lib/docker                  
    
  2. 安裝需要的軟件包, yum-util 提供yum-config-manager功能,另外兩個(device-mapper-persistent-data 和 lvm2)是devicemapper驅動依賴的

[root@localhost ~]#  yum install -y yum-utils device-mapper-persistent-data lvm2
  1. 設置yum源(選一個源設置即可)
4.1 阿里云源
[root@localhost ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

4.1 官方源
[root@localhost ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

說明:上面阿里云源和官方源兩者命令選其一執行即可。不要兩個都執行!
  1. 查看所有倉庫中所有docker版本
[root@localhost ~]# yum list docker-ce --showduplicates | sort -r
docker-ce.x86_64            18.06.3.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.06.2.ce-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
docker-ce.x86_64            18.03.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            18.03.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.09.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.09.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.2.ce-1.el7.centos             docker-ce-stable
  1. 選擇相應版本進行安裝(yun install docker-ce-版本號;下面版本17.12.0.ce是由17.12.0.ce-1.el7.centos得來)

    [root@localhost ~]# yum install docker-ce-17.12.0.ce
    安裝過程選y。
    
    
    當然也可以不指定版本安裝,安裝最新版本
    [root@localhost ~]# yum install docker-ce
    
  1. 啟動docker

    默認安裝后,docker未啟動
    [root@localhost ~]# systemctl status  docker
    ● docker.service - Docker Application Container Engine
       Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
       Active: inactive (dead)
         Docs: https://docs.docker.com
         
    啟動docker
    [root@localhost ~]# systemctl start  docker
    
  2. 查看docker版本(client和server都啟動了則證明安裝成功)

    [root@localhost ~]# docker version
    Client:
     Version:    17.12.0-ce
     API version:    1.35
     Go version: go1.9.2
     Git commit: c97c6d6
     Built:  Wed Dec 27 20:10:14 2017
     OS/Arch:    linux/amd64
    
    Server:
     Engine:
      Version:   17.12.0-ce
      API version:   1.35 (minimum version 1.12)
      Go version:    go1.9.2
      Git commit:    c97c6d6
      Built: Wed Dec 27 20:12:46 2017
      OS/Arch:   linux/amd64
      Experimental:  false
    [root@localhost ~]# 
    
    1. 設置docke開機啟動

      [root@localhost ~]#  systemctl enable docker
      [root@localhost ~]#  chkconfig docker on
      

國內鏡像加速

國內從 DockerHub 拉取鏡像有時會遇到困難,此時可以配置鏡像加速器。Docker 官方和國內很多云服務商都提供了國內加速器服務。

{"registry-mirrors":["https://docker.mirrors.ustc.edu.cn","https://registry.docker-cn.com"]}

之后重新啟動服務(劃重點,很多資料都沒有這一步,當我們新增了daemon.json文件后必須重載docker才能生效):

[root@localhost ~]#   systemctl daemon-reload
[root@localhost ~]#   systemctl restart docker

檢查加速器是否生效:

[root@localhost ~]# docker info

docker info返回內容包含下面信息,則代表鏡像庫設置成功:
Registry Mirrors:
    https://docker.mirrors.ustc.edu.cn/
   https://registry.docker-cn.com/

運行第一個容器 hello-world

第一次執行docker run hello-world會先從本地找鏡像。如果不存在,則會去鏡像中心拉到本地執行。

[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:9572f7cdcee8591948c2963463447a53466950b3fc15a247fcad1917ca215a2f
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/

其他的鏡像可以在鏡像中心找:https://hub.docker.com/

建立docker用戶組

默認情況下,docker 命令會使用 Unix socket 與 Docker 引擎通訊。而只有 root 用戶和 docker 組的用戶才可以訪問 Docker 引擎的 Unix socket。出于安全考慮,一般 Linux 系統上不會直接使用 root 用戶。因此,更好地做法是將需要使用 docker 的用戶加入 docker 用戶組。

授權后,當前用戶不需要每次進行執行docker命令時,都加上sudo。不操作也沒不影響運行。

如果你當前的用戶就是docker組,則不必操作下面2、3等操作了。
1、[docker@localhost ~]$ groups $USER
docker : docker
[docker@localhost ~]$ 

2、[root@localhost ~]#  groupadd docker    --docker為新建組名(新建組名必須是docker)
3、[root@localhost ~]#  gpasswd -a hadoop docker --將當前用戶hadoop加入到docker用戶組里面。用戶名根據你當前的用戶進行進行替換。

4、設置-注銷系統或重啟系統使第二步生效,重新登錄系統后,新開命令行窗口即可;
5、[root@localhost ~]#  systemctl docker start
6、[hadoop@localhost ~] docker run hello-world

參考材料:

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。