歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> 學習Linux

CentOS 7 安裝Docker服務

CentOS 7 安裝Docker服務

CentOS 7 安裝Docker服務


Docker 簡介

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

Docker官網:http://www.docker.com/
Docker 官方文檔:https://docs.docker.com/
Docker Github 地址:https://github.com/docker/docker
Docker 倉庫:https://hub.docker.com/
Docker 中文社區:http://www.docker.org.cn/book/docker
Docker 應用場景:https://www.zhihu.com/question/22969309

1.1、什麼是Docker?

Docker是基於Go語言實現的雲開源項目,誕生於2013年初,最初發起者是dotcloud公司。Docker是一個開源的引擎,可以輕松的為任何應用創建一個輕量級的、可移植的、自給自足的容器。開發者在筆記本上編譯測試通過的容器可以批量地在生產環境中部署,包括VMs(虛擬機)、bare metal、OpenStack 集群和其他的基礎應用平台。docker中文社區Docker的主要目標是'Build,Ship and Run Any App Anywhere',即通過對應用組件的封裝(Packing)、分發(Distribution)、部署(Deployment)、運行(Runtime)等生命周期的管理,達到應用組件級別的“一次封裝,多次運行”。這裡的應用組件,既可以是一個Web應用,也可以是一套數據庫服務,甚至是一個操作系統或編輯器。

1.2、為什麼要使用Docker?

Docker在正確的地點,正確的時間順應了正確的趨勢--即高效的構建應用。更快速地的交付和部署。更高效的資源利用更輕松的更新部署更簡單的更新管理Docker與虛擬機比較   Docker容器很快,啟動和停止可以秒級實現,相比傳統的虛擬機方式快很多   Docker容器對系統資源需求很少,一台主機上面可以運行數千個Docker容器   Docker通過類似Git的操作來方便用戶獲取、分布和更新應用鏡像,指令簡單,學習成本較低   Docker通過Dockerfile配置文件來支持靈活的自動化創建和部署機制,提高工作效率

1.3、 虛擬化與Docker

    虛擬化是一個通用的概念,在不通領域有著不同的理解。在計算機領域,一般是指計算虛擬化(Computing Virtualization),或服務器虛擬化。    虛擬化的核心是對資源進行抽象,目標往往是為了在同一主機上運行多個操作系統或應用,從而提高系統資源的利用率,同時帶來降低成本,方便管理和容錯容災。    虛擬化可分為基於硬件的虛擬化和基於軟件的虛擬化。基於軟件的虛擬化又分為應用虛擬化和平台虛擬化。平台虛擬化又細分如下幾個子類:         完全虛擬化。虛擬機模擬完整的底層硬件環境和特權指令的執行過程,客戶操作系統無需進行修改。例如VMware Workstation,VirtulBox,QEMU等         硬件輔助虛擬化。利用硬件輔助支持處理敏感指令來實現完全虛擬化的功能,客戶端操作系統無需更改,例如Xen,KVM等         部分虛擬化。只針對部分硬件資源進行虛擬化,客戶端操作系統需要進行更改。         超虛擬化。部分硬件接口以軟件的形式提供給客戶機操作系統,客戶機操作系統需要進行更改。         操作系統級虛擬化。內核通過創建多個虛擬的操作系統實例來隔離不同的進程,Docker技術。

Docker 安裝

安裝Docker是操作系統級虛擬化工具,它可以在Containers中自動部署應用程序

  • 安裝docker
[root@linuxprobe~]# yum -y install docker[root@linuxprobe~]# systemctl start docker[root@linuxprobe~]# systemctl enable docker [root@linuxprobe ~]# systemctl status docker   # 查看docker狀態● docker.service - Docker Application Container Engine   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)   Active: active (running) since Wed 2016-10-26 19:38:40 CST; 12s ago     Docs: http://docs.docker.com Main PID: 3762 (docker-current)   CGroup: /system.slice/docker.service           └─3762 /usr/bin/docker-current daemon --exec-opt native.cgroupdriver=systemd --selinux-enabled --log-driver=journaldOct 26 19:38:39 linuxprobe.org docker-current[3762]: time="2016-10-26T19:38:39.844803185+08:00" level=info msg="devmapper: Successfully created filesystem xfs on device docker-253:0-104354176-base"Oct 26 19:38:39 linuxprobe.org docker-current[3762]: time="2016-10-26T19:38:39.994708787+08:00" level=info msg="Graph migration to content-addressability took 0.00 seconds"Oct 26 19:38:40 linuxprobe.org docker-current[3762]: time="2016-10-26T19:38:40.018129400+08:00" level=info msg="Firewalld running: true"Oct 26 19:38:40 linuxprobe.org docker-current[3762]: time="2016-10-26T19:38:40.294869786+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip can ...red IP address"Oct 26 19:38:40 linuxprobe.org docker-current[3762]: time="2016-10-26T19:38:40.567994904+08:00" level=info msg="Loading containers: start."Oct 26 19:38:40 linuxprobe.org docker-current[3762]: time="2016-10-26T19:38:40.568039413+08:00" level=info msg="Loading containers: done."Oct 26 19:38:40 linuxprobe.org docker-current[3762]: time="2016-10-26T19:38:40.568047306+08:00" level=info msg="Daemon has completed initialization"Oct 26 19:38:40 linuxprobe.org docker-current[3762]: time="2016-10-26T19:38:40.568058527+08:00" level=info msg="Docker daemon" commit=cb079f6-unsupported execdriver=native-0.2 graphdriver=devicemapper version=1.10.3Oct 26 19:38:40 linuxprobe.org docker-current[3762]: time="2016-10-26T19:38:40.572491688+08:00" level=info msg="API listen on /var/run/docker.sock"Oct 26 19:38:40 linuxprobe.org systemd[1]: Started Docker Application Container Engine.Hint: Some lines were ellipsized, use -l to show in full.
  • 下載官方鏡像並創建一個Container,並在Container中輸出“Welcome to the Docker World”
[root@linuxprobe ~]# docker pull centosUsing default tag: latestTrying to pull repository docker.io/library/centos ... latest: Pulling from docker.io/library/centos08d48e6f1cff: Pull complete Digest: sha256:b2f9d1c0ff5f87a4743104d099a3d561002ac500db1b9bfa02a783a46e0d366cStatus: Downloaded newer image for docker.io/centos:latest[root@linuxprobe ~]# docker run centos /bin/echo "Welcome to the Docker World"Welcome to the Docker World
  • 使用“i”和“t”選項連接到Container的交互會話,如下所示。如果從Container會話退出,則Container的進程完成
[root@linuxprobe ~]# docker run -i -t centos /bin/bash[root@82699d79557c /]# uname -aLinux 82699d79557c 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux[root@82699d79557c /]# exitexit[root@linuxprobe ~]# #back
  • 如果從容器會話中退出並保持容器的進程,請按Ctrl + p和Ctrl + q鍵
[root@linuxprobe ~]# docker run -i -t centos /bin/bash[root@a05c7fd0a54f /]# [root@linuxprobe ~]# [root@linuxprobe ~]# docker psCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMESa05c7fd0a54f        centos              "/bin/bash"         24 seconds ago      Up 23 seconds                           trusting_fermat[root@linuxprobe ~]# docker attach a05c7fd0a54f  # connect docekr process[root@a05c7fd0a54f /]# [root@linuxprobe ~]# docker kill a05c7fd0a54f  # kill docker processa05c7fd0a54f[root@linuxprobe ~]# docker ps # 查看運行的docker服務CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Docker:添加鏡像

  • 在容器中添加鏡像文件
  • 例如,使用更新官方映像安裝httpd,並將其添加為容器的新映像。該容器是在每次執行docker run命令時生成的,因此添加最新執行的容器如下
[root@linuxprobe ~]# docker imagesREPOSITORY              TAG                 IMAGE ID            CREATED                  SIZEdocker.io/centos        latest              0584b3d2cf6d        Less than a second ago   196.5 MB# start a Container and install httpd[root@linuxprobe ~]# docker run centos /bin/bash -c "yum -y update; yum -y install httpd"[root@linuxprobe ~]# docker ps -a | head -2CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMESf36383194ad4        centos              "/bin/bash -c 'yum -y"   2 minutes ago       Exited (0) 45 seconds ago                         jolly_cray                       elegant_wright[root@linuxprobe ~]# docker commit f36383194ad4 my_image/centos_httpd[root@linuxprobe ~]# docker imagesREPOSITORY              TAG                 IMAGE ID            CREATED                  SIZEdocker.io/centos        latest              0584b3d2cf6d        Less than a second ago   196.5 MBmy_image/centos_httpd   latest              b0be2940865a        7 seconds ago            338.3 MB

訪問容器

root@linuxprobe ~]# docker run -it -p 8081:80 my_image/centos_httpd /bin/bash[root@2f0d06526d42 /]# /usr/sbin/httpd &[1] 14[root@2f0d06526d42 /]# AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message[1]+  Done                    /usr/sbin/httpd[root@2f0d06526d42 /]# echo "httpd on Docker Container" > /var/www/html/index.html # exit with Ctrl+p, Ctrl+q[root@2f0d06526d42 /]# [root@linuxprobe ~]# docker psCONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS              PORTS                  NAMES2f0d06526d42        my_image/centos_httpd   "/bin/bash"         54 seconds ago      Up 52 seconds       0.0.0.0:8081->80/tcp   hopeful_gates[root@linuxprobe ~]# docker ps -a | head -2CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS                         PORTS                  NAMES2f0d06526d42        my_image/centos_httpd   "/bin/bash"              27 minutes ago      Up 27 minutes                  0.0.0.0:8081->80/tcp   hopeful_gates

客戶端浏覽器訪問

Docker:使用Dockerfile

  • 使用Dockerfile並自動創建Docker鏡像

[1] Dockerfile的格式是[INSTRUCTION arguments],請參閱INSTRUCTION的以下說明。 INSTRUCTION說明 MAINTAINER 它設置生成的圖像的作者字段。 RUN 當創建Docker鏡像時,它將執行任何命令。 CMD 它將執行任何命令當Docker容器將被執行。 LABEL 它向圖像添加元數據。 EXPOSE 它通知Docker容器將在運行時偵聽指定的網絡端口。 ADD 它復制新文件,目錄或遠程文件URL。 COPY 它復制新的文件或目錄。 [ADD]的區別是,它不可能指定remore URL,也不會自動提取歸檔文件。 VOLUME 它創建具有指定名稱的裝入點,並將其標記為從本機主機或其他容器保存外部裝入的卷 USER它設置用戶名或UID。 WORKDIR 它設置工作目錄。

  • 例如,創建一個Dockerfile來安裝httpd並添加index.html,並使用80端口啟動httpd
[root@linuxprobe ~]# vim Dockerfile # create newFROM centosMAINTAINER linuxprobe <[email protected]>RUN yum -y install httpdRUN echo "Hello LinuxProbe DockerFile" > /var/www/html/index.htmlEXPOSE 80CMD ["-D", "FOREGROUND"]ENTRYPOINT ["/usr/sbin/httpd"][root@linuxprobe ~]# docker build -t web_server:latest . Sending build context to Docker daemon  21.5 kBStep 1 : FROM centos ---> 0584b3d2cf6dStep 2 : MAINTAINER linuxprobe <[email protected]> ---> Running in 8064d0091e44 ---> 940c8fbe4161Removing intermediate container 8064d0091e44Step 3 : RUN yum -y install httpd ---> Running in 3d37e4919fa9Loaded plugins: fastestmirror, ovlDetermining fastest mirrors * base: mirrors.163.com * extras: ftp.sjtu.edu.cn * updates: mirrors.163.comResolving Dependencies--> Running transaction check---> Package httpd.x86_64 0:2.4.6-40.el7.centos.4 will be installed--> Processing Dependency: httpd-tools = 2.4.6-40.el7.centos.4 for package: httpd-2.4.6-40.el7.centos.4.x86_64--> Processing Dependency: system-logos >= 7.92.1-1 for package: httpd-2.4.6-40.el7.centos.4.x86_64--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-40.el7.centos.4.x86_64--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-40.el7.centos.4.x86_64--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-40.el7.centos.4.x86_64--> Running transaction check---> Package apr.x86_64 0:1.4.8-3.el7 will be installed---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed---> Package centos-logos.noarch 0:70.0.6-3.el7.centos will be installed---> Package httpd-tools.x86_64 0:2.4.6-40.el7.centos.4 will be installed---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed--> Finished Dependency ResolutionDependencies Resolved================================================================================ Package           Arch        Version                       Repository    Size================================================================================Installing: httpd             x86_64      2.4.6-40.el7.centos.4         updates      2.7 MInstalling for dependencies: apr               x86_64      1.4.8-3.el7                   base         103 k apr-util          x86_64      1.5.2-6.el7                   base          92 k centos-logos      noarch      70.0.6-3.el7.centos           base          21 M httpd-tools       x86_64      2.4.6-40.el7.centos.4         updates       83 k mailcap           noarch      2.1.41-2.el7                  base          31 kTransaction Summary================================================================================Install  1 Package (+5 Dependent packages)Total download size: 24 MInstalled size: 31 MDownloading packages:warning: /var/cache/yum/x86_64/7/base/packages/apr-util-1.5.2-6.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEYPublic key for apr-util-1.5.2-6.el7.x86_64.rpm is not installedPublic key for httpd-tools-2.4.6-40.el7.centos.4.x86_64.rpm is not installed--------------------------------------------------------------------------------Total                                              382 kB/s |  24 MB  01:05     Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7Importing GPG key 0xF4A80EB5: Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <[email protected]>" Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5 Package    : centos-release-7-2.1511.el7.centos.2.10.x86_64 (@CentOS) From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7Running transaction checkRunning transaction testTransaction test succeededRunning transaction  Installing : apr-1.4.8-3.el7.x86_64                                       1/6   Installing : apr-util-1.5.2-6.el7.x86_64                                  2/6   Installing : httpd-tools-2.4.6-40.el7.centos.4.x86_64                     3/6   Installing : centos-logos-70.0.6-3.el7.centos.noarch                      4/6   Installing : mailcap-2.1.41-2.el7.noarch                                  5/6   Installing : httpd-2.4.6-40.el7.centos.4.x86_64                           6/6   Verifying  : apr-1.4.8-3.el7.x86_64                                       1/6   Verifying  : httpd-tools-2.4.6-40.el7.centos.4.x86_64                     2/6   Verifying  : apr-util-1.5.2-6.el7.x86_64                                  3/6   Verifying  : httpd-2.4.6-40.el7.centos.4.x86_64                           4/6   Verifying  : mailcap-2.1.41-2.el7.noarch                                  5/6   Verifying  : centos-logos-70.0.6-3.el7.centos.noarch                      6/6 Installed:  httpd.x86_64 0:2.4.6-40.el7.centos.4                                          Dependency Installed:  apr.x86_64 0:1.4.8-3.el7                                                        apr-util.x86_64 0:1.5.2-6.el7                                                   centos-logos.noarch 0:70.0.6-3.el7.centos                                       httpd-tools.x86_64 0:2.4.6-40.el7.centos.4                                      mailcap.noarch 0:2.1.41-2.el7                                                 Complete! ---> 3ce9abf4dfeaRemoving intermediate container 3d37e4919fa9Step 4 : RUN echo "Hello LinuxProbe DockerFile" > /var/www/html/index.html ---> Running in 297d8d666c8d ---> 3d185363045bRemoving intermediate container 297d8d666c8dStep 5 : EXPOSE 80 ---> Running in 017db517e06a ---> 5c855e478c3cRemoving intermediate container 017db517e06aStep 6 : CMD -D FOREGROUND ---> Running in 6add13fca3cb ---> 7a219d9fa6e1Removing intermediate container 6add13fca3cbStep 7 : ENTRYPOINT /usr/sbin/httpd ---> Running in da4671709ee1 ---> c0d84e256068Removing intermediate container da4671709ee1Successfully built c0d84e256068[root@linuxprobe ~]# docker imagesREPOSITORY              TAG                 IMAGE ID            CREATED                  SIZEdocker.io/centos        latest              0584b3d2cf6d        Less than a second ago   196.5 MBweb_server              latest              c0d84e256068        36 seconds ago           338.3 MBmy_image/centos_httpd   latest              b0be2940865a        4 hours ago              338.3 MB[root@linuxprobe ~]# docker run -d -p 80:80 web_server c37d25a405a8e0599bf54fe77d78c807a520242a21ccb15b18d6b6ee4d13415b[root@linuxprobe ~]# docker psCONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                  NAMESc37d25a405a8        web_server              "/usr/sbin/httpd -D F"   6 seconds ago       Up 5 seconds        0.0.0.0:80->80/tcp     condescending_knuth2f0d06526d42        my_image/centos_httpd   "/bin/bash"              About an hour ago   Up About an hour    0.0.0.0:8081->80/tcp   hopeful_gat

http://xxxxxx/Linuxjc/1184755.html TechArticle

Copyright © Linux教程網 All Rights Reserved