本文小编为大家详细介绍“CentOS7安装Docker服务的方法”,内容详细,步骤清晰,细节处理妥当,希望这篇“CentOS7安装Docker服务的方法”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
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=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.
下载官方镜像并创建一个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,并将其添加为容器的新映像。该容器是在每次执行docker run命令时生成的,因此添加最新执行的容器如下
[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
客户端浏览器访问
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 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服务的方法”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。