Docker第一篇:简介和入门安装

来源:互联网 发布:金沙在淘宝的店叫什么 编辑:程序博客网 时间:2024/05/16 10:11

Docker简介

Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源。
Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。
容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。

Docker的应用场景

  • Web 应用的自动化打包和发布。
  • 自动化测试和持续集成、发布。
  • 在服务型环境中部署和调整数据库或其他的后台应用。
  • 从头编译或者扩展现有的OpenShift或Cloud Foundry平台来搭建自己的PaaS环境。

Docker 的优点

  1. 简化程序:
    Docker 让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,便可以实现虚拟化。Docker改变了虚拟化的方式,使开发者可以直接将自己的成果放入Docker中进行管理。方便快捷已经是 Docker的最大优势,过去需要用数天乃至数周的 任务,在Docker容器的处理下,只需要数秒就能完成。
  2. 避免选择恐惧症:
    如果你有选择恐惧症,还是资深患者。Docker 帮你 打包你的纠结!比如 Docker 镜像;Docker 镜像中包含了运行环境和配置,所以 Docker 可以简化部署多种应用实例工作。比如 Web 应用、后台应用、数据库应用、大数据应用比如 Hadoop 集群、消息队列等等都可以打包成一个镜像部署。
  3. 节省开支:
    一方面,云计算时代到来,使开发者不必为了追求效果而配置高额的硬件,Docker 改变了高性能必然高价格的思维定势。Docker 与云的结合,让云空间得到更充分的利用。不仅解决了硬件管理的问题,也改变了虚拟化的方式。

缺点以及安装必备要求

安装docker的先决条件:

  1. 必须是64位CPU架构的计算机,Docker目前不支持32位CPU;
  2. 运行Linux3.8或更高版本内核,CentOS时内核必不小于3.10;
  3. 内核必须支持一种合适的存储驱动,可以是Device Manager、AUFS、vfs、btrfs、以及默认的驱动Device Mapper中的一个;
  4. 内核必须支持并开启cgroup和命名空间namespace功能。

安装常见的情况:
linux安装,window和mac安装可以参考官网的版本安装指导

window最新版本下载[2017-11-9的版本]
https://download.docker.com/win/stable/Docker%20for%20Windows%20Installer.exe

其他容器

容器运行时是执行容器并在节点上管理容器镜像的软件,目前,最广为人知的容器运行时是Docker,但在生态系统中还有其他容器运行时软件,比如Rkt、Containerd和Lxd,Docker实现在于Kubernetes环境中使用。

基础环境准备

VM12.0创建的虚拟机模拟真实环境
操作系统:centos7.0
安装方式:更新yum镜像库使用最新镜像文件安装

centos7安装步骤

一、安装前的准备工作
Docker 需要安装在64位系统的CentOS上,而且linux内核至少在3.10版本以上,这个版本的内核在CenOS7上运行. 所以只要安装一个CenOS7 64位的系统就可以了.作为学习,大家只需安装个虚拟机就OK.
可以通过uname -r 来查看linux内核版本

--------------------------------------------------------------------------------Total                                              697 kB/s |  20 MB  00:28Retrieving key from https://yum.dockerproject.org/gpgImporting GPG key 0x2C52609D: Userid     : "Docker Release Tool (releasedocker) <docker@docker.com>" Fingerprint: 5811 8e89 f3a9 1289 7c07 0adb f762 2157 2c52 609d From       : https://yum.dockerproject.org/gpgIs this ok [y/N]: yRunning transaction checkRunning transaction testTransaction test succeededRunning transaction  Installing : libcgroup-0.41-13.el7.x86_64                                1/10  Installing : setools-libs-3.3.8-1.1.el7.x86_64                           2/10  Installing : checkpolicy-2.5-4.el7.x86_64                                3/10  Installing : libseccomp-2.3.1-3.el7.x86_64                               4/10  Installing : audit-libs-python-2.7.6-3.el7.x86_64                        5/10  Installing : python-IPy-0.75-6.el7.noarch                                6/10  Installing : libsemanage-python-2.5-8.el7.x86_64                         7/10  Installing : policycoreutils-python-2.5-17.1.el7.x86_64                  8/10  Installing : docker-engine-selinux-17.05.0.ce-1.el7.centos.noarch        9/10restorecon:  lstat(/var/lib/docker) failed:  No such file or directorywarning: %post(docker-engine-selinux-17.05.0.ce-1.el7.centos.noarch) scriptlet failed, exit status 255Non-fatal POSTIN scriptlet failure in rpm package docker-engine-selinux-17.05.0.ce-1.el7.centos.noarch  Installing : docker-engine-17.05.0.ce-1.el7.centos.x86_64               10/10  Verifying  : libsemanage-python-2.5-8.el7.x86_64                         1/10  Verifying  : python-IPy-0.75-6.el7.noarch                                2/10  Verifying  : audit-libs-python-2.7.6-3.el7.x86_64                        3/10  Verifying  : docker-engine-selinux-17.05.0.ce-1.el7.centos.noarch        4/10  Verifying  : libseccomp-2.3.1-3.el7.x86_64                               5/10  Verifying  : policycoreutils-python-2.5-17.1.el7.x86_64                  6/10  Verifying  : libcgroup-0.41-13.el7.x86_64                                7/10  Verifying  : docker-engine-17.05.0.ce-1.el7.centos.x86_64                8/10  Verifying  : checkpolicy-2.5-4.el7.x86_64                                9/10  Verifying  : setools-libs-3.3.8-1.1.el7.x86_64                          10/10Installed:  docker-engine.x86_64 0:17.05.0.ce-1.el7.centosDependency Installed:  audit-libs-python.x86_64 0:2.7.6-3.el7  checkpolicy.x86_64 0:2.5-4.el7  docker-engine-selinux.noarch 0:17.05.0.ce-1.el7.centos  libcgroup.x86_64 0:0.41-13.el7  libseccomp.x86_64 0:2.3.1-3.el7  libsemanage-python.x86_64 0:2.5-8.el7  policycoreutils-python.x86_64 0:2.5-17.1.el7  python-IPy.noarch 0:0.75-6.el7  setools-libs.x86_64 0:3.3.8-1.1.el7Complete!

启动服务

[root@localhost ~]# sudo service docker startRedirecting to /bin/systemctl start docker.service

run hello-world

安装完成docker后运行 hello-word,如下提示请不要终止命令,它会自动下载该镜像并安装完成,然后输出hello word

[root@localhost ~]# docker run hello-worldUnable to find image 'hello-world:latest' locallylatest: Pulling from library/hello-world9a0669468bf7: Pull completeDigest: sha256:0e06ef5e1945a718b02a8c319e15bae44f47039005530bc617a5d071190ed3fcStatus: Downloaded newer image for hello-world:latestHello 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. 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 bashShare images, automate workflows, and more with a free Docker ID: https://cloud.docker.com/For more examples and ideas, visit: https://docs.docker.com/engine/userguide/

自动启动Docker

若要保证,你开机时,自动启动Docker,可以执行如下的命令:

检查配置是不是开机运行

[root@localhost ~]#  chkconfig docker on  Note: Forwarding request to 'systemctl enable docker.service'.  Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service. 

提示要求设置为系统服务并启用

[root@localhost ~]# systemctl enable docker.service

这样就可以开机启动了

Docker 卸载

  1. 列出你已安装的Docker package.
[root@localhost ~]# yum list installed | grep dockerdocker-engine.x86_64                 17.05.0.ce-1.el7.centos         @dockerrepodocker-engine-selinux.noarch         17.05.0.ce-1.el7.centos         @dockerrepo
  1. 移除这个package
$ sudo yum -y remove docker-engine.x86_64  

这个命令没有移除images、containers、volumes或者你主机上用户创建的配置文件.

[root@localhost ~]# ll /var/lib/docker/total 4drwx------. 5 root root  222 Nov  9 21:18 containersdrwx------. 3 root root   21 Nov  9 20:00 imagedrwxr-x---. 3 root root   19 Nov  9 20:00 networkdrwx------. 9 root root 4096 Nov  9 21:18 overlaydrwx------. 4 root root   32 Nov  9 20:00 pluginsdrwx------. 2 root root    6 Nov  9 20:00 swarmdrwx------. 2 root root    6 Nov  9 21:18 tmpdrwx------. 2 root root    6 Nov  9 20:01 tmp-olddrwx------. 2 root root    6 Nov  9 20:00 trustdrwx------. 2 root root   25 Nov  9 20:00 volumes
  1. 要删除所有的images、containers、volumes,运行如下命令.
$ rm -rf /var/lib/docker  
  1. 查找和删除任何用户创建的配置文件.

参考资料

centos安装docke
Unable to find image ‘hello-world:latest’ locally如何处理

原创粉丝点击