溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

CentOS7安裝Docker服務的方法

發(fā)布時間:2022-05-26 15:33:17 來源:億速云 閱讀:190 作者:iii 欄目:大數(shù)據(jù)

本文小編為大家詳細介紹“CentOS7安裝Docker服務的方法”,內(nèi)容詳細,步驟清晰,細節(jié)處理妥當,希望這篇“CentOS7安裝Docker服務的方法”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。

1.1、什么是docker?

docker是基于go語言實現(xiàn)的云開源項目,誕生于2013年初,最初發(fā)起者是dotcloud公司。
docker是一個開源的引擎,可以輕松的為任何應用創(chuàng)建一個輕量級的、可移植的、自給自足的容器。開發(fā)者在筆記本上編譯測試通過的容器可以批量地在生產(chǎn)環(huán)境中部署,包括vms(虛擬機)、bare metal、openstack 集群和其他的基礎應用平臺。
docker中文社區(qū)
docker的主要目標是'build,ship and run any app anywhere',即通過對應用組件的封裝(packing)、分發(fā)(distribution)、部署(deployment)、運行(runtime)等生命周期的管理,達到應用組件級別的“一次封裝,多次運行”。這里的應用組件,既可以是一個web應用,也可以是一套數(shù)據(jù)庫服務,甚至是一個操作系統(tǒng)或編輯器。

1.2、為什么要使用docker?

docker在正確的地點,正確的時間順應了正確的趨勢--即高效的構建應用。

更快速地的交付和部署。

更高效的資源利用

更輕松的更新部署

更簡單的更新管理

docker與虛擬機比較

   docker容器很快,啟動和停止可以秒級實現(xiàn),相比傳統(tǒng)的虛擬機方式快很多
   docker容器對系統(tǒng)資源需求很少,一臺主機上面可以運行數(shù)千個docker容器
   docker通過類似git的操作來方便用戶獲取、分布和更新應用鏡像,指令簡單,學習成本較低
   docker通過dockerfile配置文件來支持靈活的自動化創(chuàng)建和部署機制,提高工作效率

1.3、虛擬化與docker

    虛擬化是一個通用的概念,在不通領域有著不同的理解。在計算機領域,一般是指計算虛擬化(computing virtualization),或服務器虛擬化。

    虛擬化的核心是對資源進行抽象,目標往往是為了在同一主機上運行多個操作系統(tǒng)或應用,從而提高系統(tǒng)資源的利用率,同時帶來降低成本,方便管理和容錯容災。

    虛擬化可分為基于硬件的虛擬化和基于軟件的虛擬化。基于軟件的虛擬化又分為應用虛擬化和平臺虛擬化。平臺虛擬化又細分如下幾個子類:

         完全虛擬化。虛擬機模擬完整的底層硬件環(huán)境和特權指令的執(zhí)行過程,客戶操作系統(tǒng)無需進行修改。例如vmware workstation,virtulbox,qemu等
         硬件輔助虛擬化。利用硬件輔助支持處理敏感指令來實現(xiàn)完全虛擬化的功能,客戶端操作系統(tǒng)無需更改,例如xen,kvm等
         部分虛擬化。只針對部分硬件資源進行虛擬化,客戶端操作系統(tǒng)需要進行更改。
         超虛擬化。部分硬件接口以軟件的形式提供給客戶機操作系統(tǒng),客戶機操作系統(tǒng)需要進行更改。
         操作系統(tǒng)級虛擬化。內(nèi)核通過創(chuàng)建多個虛擬的操作系統(tǒng)實例來隔離不同的進程,docker技術。

docker 安裝

安裝docker是操作系統(tǒng)級虛擬化工具,它可以在containers中自動部署應用程序
安裝docker

[root@linuxprobe~]# yum -y install docker
[root@linuxprobe~]# systemctl start docker
[root@linuxprobe~]# systemctl enable docker 
[root@linuxprobe ~]# systemctl status docker # 查看docker狀態(tài)
● 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=journald

oct 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.3
oct 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.

下載官方鏡像并創(chuàng)建一個container,并在container中輸出“welcome to the docker world”

[root@linuxprobe ~]# docker pull centos
using default tag: latest
trying to pull repository docker.io/library/centos ... 
latest: pulling from docker.io/library/centos
08d48e6f1cff: pull complete 
digest: sha256:b2f9d1c0ff5f87a4743104d099a3d561002ac500db1b9bfa02a783a46e0d366c
status: 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 -a
linux 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 /]# exit
exit
[root@linuxprobe ~]# #back

如果從容器會話中退出并保持容器的進程,請按ctrl + p和ctrl + q鍵

[root@linuxprobe ~]# docker run -i -t centos /bin/bash
[root@a05c7fd0a54f /]# [root@linuxprobe ~]# 
[root@linuxprobe ~]# docker ps
container id  image    command    created    status    ports    names
a05c7fd0a54f  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 process
a05c7fd0a54f
[root@linuxprobe ~]# docker ps # 查看運行的docker服務
container id  image    command    created    status    ports    names

docker:添加鏡像

在容器中添加鏡像文件
例如,使用更新官方映像安裝httpd,并將其添加為容器的新映像。該容器是在每次執(zhí)行docker run命令時生成的,因此添加最新執(zhí)行的容器如下

[root@linuxprobe ~]# docker images
repository    tag     image id   created     size
docker.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 -2
container id  image    command     created    status      ports    names
f36383194ad4  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 images
repository    tag     image id   created       size
docker.io/centos  latest    0584b3d2cf6d  less than a second ago 196.5 mb
my_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 ps
container id  image     command    created    status    ports     names
2f0d06526d42  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 -2
container id  image     command     created    status       ports     names
2f0d06526d42  my_image/centos_httpd "/bin/bash"    27 minutes ago  up 27 minutes     0.0.0.0:8081->80/tcp hopeful_gates

客戶端瀏覽器訪問

CentOS7安裝Docker服務的方法

docker:使用dockerfile

使用dockerfile并自動創(chuàng)建docker鏡像
[1] dockerfile的格式是[instruction arguments],請參閱instruction的以下說明。
instruction  說明
maintainer 它設置生成的圖像的作者字段。
run  當創(chuàng)建docker鏡像時,它將執(zhí)行任何命令。
cmd   它將執(zhí)行任何命令當docker容器將被執(zhí)行。
label 它向圖像添加元數(shù)據(jù)。
expose 它通知docker容器將在運行時偵聽指定的網(wǎng)絡端口。
add 它復制新文件,目錄或遠程文件url。
copy 它復制新的文件或目錄。 [add]的區(qū)別是,它不可能指定remore url,也不會自動提取歸檔文件。
volume 它創(chuàng)建具有指定名稱的裝入點,并將其標記為從本機主機或其他容器保存外部裝入的卷 user它設置用戶名或uid。
workdir 它設置工作目錄。

例如,創(chuàng)建一個dockerfile來安裝httpd并添加index.html,并使用80端口啟動httpd

[root@linuxprobe ~]# vim dockerfile 
# create new
from centos
maintainer linuxprobe <admin@linuxprobe.org>
run yum -y install httpd
run echo "hello linuxprobe dockerfile" > /var/www/html/index.html
expose 80
cmd ["-d", "foreground"]
entrypoint ["/usr/sbin/httpd"]
[root@linuxprobe ~]# docker build -t web_server:latest . 
sending build context to docker daemon 21.5 kb
step 1 : from centos
 ---> 0584b3d2cf6d
step 2 : maintainer linuxprobe <admin@linuxprobe.org>
 ---> running in 8064d0091e44
 ---> 940c8fbe4161
removing intermediate container 8064d0091e44
step 3 : run yum -y install httpd
 ---> running in 3d37e4919fa9
loaded plugins: fastestmirror, ovl
determining fastest mirrors
 * base: mirrors.163.com
 * extras: ftp.sjtu.edu.cn
 * updates: mirrors.163.com
resolving 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 resolution

dependencies resolved

================================================================================
 package   arch  version      repository size
================================================================================
installing:
 httpd    x86_64  2.4.6-40.el7.centos.4   updates  2.7 m
installing 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 k

transaction summary
================================================================================
install 1 package (+5 dependent packages)

total download size: 24 m
installed size: 31 m
downloading 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: nokey
public key for apr-util-1.5.2-6.el7.x86_64.rpm is not installed
public 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-7
importing gpg key 0xf4a80eb5:
 userid  : "centos-7 key (centos 7 official signing key) <security@centos.org>"
 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-7
running transaction check
running transaction test
transaction test succeeded
running 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!
 ---> 3ce9abf4dfea
removing intermediate container 3d37e4919fa9
step 4 : run echo "hello linuxprobe dockerfile" > /var/www/html/index.html
 ---> running in 297d8d666c8d
 ---> 3d185363045b
removing intermediate container 297d8d666c8d
step 5 : expose 80
 ---> running in 017db517e06a
 ---> 5c855e478c3c
removing intermediate container 017db517e06a
step 6 : cmd -d foreground
 ---> running in 6add13fca3cb
 ---> 7a219d9fa6e1
removing intermediate container 6add13fca3cb
step 7 : entrypoint /usr/sbin/httpd
 ---> running in da4671709ee1
 ---> c0d84e256068
removing intermediate container da4671709ee1
successfully built c0d84e256068
[root@linuxprobe ~]# docker images
repository    tag     image id   created     size
docker.io/centos  latest    0584b3d2cf6d  less than a second ago 196.5 mb
web_server    latest    c0d84e256068  36 seconds ago   338.3 mb
my_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 ps
container id  image     command     created    status    ports     names
c37d25a405a8  web_server    "/usr/sbin/httpd -d f" 6 seconds ago  up 5 seconds  0.0.0.0:80->80/tcp  condescending_knuth
2f0d06526d42  my_image/centos_httpd "/bin/bash"    about an hour ago up about an hour 0.0.0.0:8081->80/tcp hopeful_gat

讀到這里,這篇“CentOS7安裝Docker服務的方法”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內(nèi)容的文章,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI