Docker镜像除了是Docker的核心技术以外也是应用发布的标准格式。一个完整的Docker镜像能够支撑一个Docker容器的运行,在Docker的整个使用过程当中,进入一个已经定型的容器以后,就能够在容器中,进行操做,最多见的操做就是在容器中安装应用,若是要把已经安装的服务进行迁移,就须要把环境以及搭建的服务生成新的镜像。html
建立镜像的方法有三种,分别是基于已有镜像、基于本地模版以及基于Dockerfile建立。docker
基于已有的镜像建立主要使用docker commit命令。实质上就是把一个容器运行的程序以及该程序的运行环境打包起来生成新的镜像。apache
下面启动一个已经有的镜像,在里面容器修改后,将修改后的容器提交成为新的镜像。json
[root@master ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/centos latest 5182e96772bf 6 days ago 200 MB
启动一个容器vim
[root@master ~]# docker run -itd 5182e96772bf /bin/bash WARNING: IPv4 forwarding is disabled. Networking will not work. 4faf98f1c6619212c3205b0a7830617ff69d457c0882ed0ca4e71430fc549406
这里提示ipv4的路由功能没有开启,因此,咱们须要开启ipv4的路由转发功能。centos
[root@master ~]# sysctl -p net.ipv4.ip_forward = 1
将已经建立的容器中止而且删除,从新启动一个容器。bash
[root@master ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS 4faf98f1c661 5182e96772bf "/bin/bash" 32 seconds ago Up 30 seconds [root@master ~]# docker stop 4faf98f1c661 4faf98f1c661 [root@master ~]# docker rm 4faf98f1c661 4faf98f1c661
从新建立并启动一个容器服务器
[root@master ~]# docker run -itd 5182e96772bf /bin/bash cbeb511d2b9a7a5a25ad532a1bb8183eb4008248fad3d545eaeb7f4f57f4660a
进入到容器中,查看有没有安装httpd服务。app
[root@master ~]# docker exec -it cbeb511d2b9a /bin/bash [root@cbeb511d2b9a /]# rpm -q httpd package httpd is not installed
能够看到没有安装,咱们使用yum安装httpd服务后,将这个容器里面运行的程序及环境从新打包生成新的镜像。dom
[root@cbeb511d2b9a /]# yum install httpd -y [root@cbeb511d2b9a /]# rpm -q httpd httpd-2.4.6-80.el7.centos.1.x86_64 #显示已经安装好httpd
咱们使用docker commit命令建立一个新的镜像,其中有几个经常使用选项:
[root@master ~]# docker commit -m "nwe" -a "test" cbeb511d2b9a apache:test sha256:96aaf92c7b04b6d91fcaea0d15ad7715d575dac82971458320d54d449eedc174 [root@master ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE apache test 96aaf92c7b04 12 seconds ago 319 MB docker.io/centos latest 5182e96772bf 6 days ago 200 MB
能够看到如今本地有两个镜像了,其中一个就是咱们修改后,从新建立的。咱们测试用这个镜像建立并启动容器,访问容器的apache服务。
[root@master ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5ea867891f2b 96aaf92c7b04 "/bin/bash" 8 seconds ago Up 4 seconds 0.0.0.0:80->80/tcp upbeat_spence cbeb511d2b9a 5182e96772bf "/bin/bash" 39 minutes ago Up 39 minutes inspiring_davinci
[root@master ~]# docker exec -it 5ea867891f2b /bin/bash [root@5ea867891f2b /]# rpm -q httpd httpd-2.4.6-80.el7.centos.1.x86_64 [root@5ea867891f2b /]# /usr/sbin/apachectl -D FOREGROUND Passing arguments to httpd using apachectl is no longer supported. You can only start/stop/restart httpd using this script. If you want to pass extra arguments to httpd, edit the /etc/sysconfig/httpd config file. AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
经过导入操做系统文件能够生成镜像,模版能够从OPENVZ开源项目下载,下面就是使用docker导入命令将下载的debian模版压缩包导入到本地镜像的例子。
[root@master ~]# wget http://download.openvz.org/template/precreated/debian-7.0-x86-minimal.tar.gz --2018-08-13 19:59:32-- http://download.openvz.org/template/precreated/debian-7.0-x86-minimal.tar.gz 正在解析主机 download.openvz.org (download.openvz.org)... 185.231.241.69 正在链接 download.openvz.org (download.openvz.org)|185.231.241.69|:80... 已链接。 已发出 HTTP 请求,正在等待回应... 200 OK 长度:88436521 (84M) [application/x-gzip] [root@master ~]# cat debian-7.0-x86-minimal.tar.gz | docker import - daoke:test sha256:2850c25d855bb1c545ab41de6f194754fbfcd5664edb78773aa7da7acf86ea7c [root@master ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE daoke test 2850c25d855b 11 seconds ago 215 MB apache test 96aaf92c7b04 5 hours ago 319 MB docker.io/centos latest 5182e96772bf 6 days ago 200 MB
导入操做完成后,就会返回镜像的ID信息,查看本地镜像列表能够看到新建立的镜像信息。
除了手动生成Docker镜像以外,还可使用Dockerfile自动生成镜像。Dockerfile是由一组指令组成的文件,其中每条命令对应Linux中的一条命令,Docker程序将读取Dockerfile中的指令生成指定的镜像。
[root@master ~]# vim Dockerfile FROM centos #基于的基础镜像centos,若本地没有,则去仓库下载 MAINTAINER test #维护该镜像的用户信息,随意填写 RUN yum install httpd -y #镜像操做安装apache软件包 EXPOSE 80 #开启80端口 ADD index.html /var/www/html/index.html #复制网页首页文件 ADD run.sh /run.sh #将启动脚本添加到镜像中 RUN chmod 777 /run.sh #修改脚本执行权限 CMD ["/run.sh"] #启动容器时执行脚本 [root@master ~]# vim run.sh #!/bin/bash rm -rf /run/httpd/* exec /usr/sbin/apachectl -D FOREGROUND [root@master ~]# vim index.html this is test [root@master ~]# docker build -t httpd:centos . #在Dockerfile文件目录下,运行命令,注意后面的点,必定要有 [root@master ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE httpd centos ef950765e1bb 2 minutes ago 319 MB daoke test 2850c25d855b 24 minutes ago 215 MB docker.io/centos latest 5182e96772bf 6 days ago 200 MB #能够看到第一个就是咱们刚才建立的镜像
将新建立的镜像加载到容器中运行
[root@master ~]# docker run -d -p 1200:80 httpd:centos 519bf067d1ad0e06aeb030530d3bcb517be1424ed41c0635d6aca28784c80143 [root@master ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 519bf067d1ad httpd:centos "/run.sh" 24 seconds ago Up 23 seconds 0.0.0.0:1200->80/tcp pedantic_elion
能够看到镜像已经在容器中运行,访问虚拟机IP以及端口即http://192.168.58.159:1200,成功访问首页。
随着建立的镜像日益增多,就须要一个保存镜像的地方,这就是仓库。目前有两种仓库:公共仓库和私有仓库。最方便的就是使用公共仓库和下载镜像,下载公共仓库的镜像不须要注册,可是上传镜像到公共仓库须要注册,在填完username和passwaord后就能够上传本身的镜像了。
[root@master ~]# docker login Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one. Username: lightblueyx Password: Login Succeeded [root@master ~]# docker tag httpd:centos lightblueyx/httpd:centos [root@master ~]# docker push lightblueyx/httpd:centos The push refers to a repository [docker.io/lightblueyx/httpd] cdf567d92322: Pushed 619278fd492d: Pushed 7e054e58afc8: Pushed abf29fcf5046: Pushed 1d31b5806ba4: Pushed centos: digest: sha256:6ce997f142e7757853296a314ea4de4309785867988ddc06dae57d5bacc428e6 size: 1362
可使用registry来搭建本地私有仓库,首先须要构建私有仓库的服务器上下载registry镜像。
[root@master ~]# docker pull registry Using default tag: latest Trying to pull repository docker.io/library/registry ... latest: Pulling from docker.io/library/registry 4064ffdc82fe: Pull complete c12c92d1c5a2: Pull complete 4fbc9b6835cc: Pull complete 765973b0f65f: Pull complete 3968771a7c3a: Pull complete Digest: sha256:51bb55f23ef7e25ac9b8313b139a8dd45baa832943c8ad8f7da2ddad6355b3c8 Status: Downloaded newer image for docker.io/registry:latest
以后须要在/etc/docker目录下面建立一个json文件,不然在往自定义的私有仓库中上传镜像的时候会报错,而后从新启动docker服务。
[root@master ~]# vim /etc/docker/daemon.json { "insecure-registries":["192.168.58.159:5000"]} [root@master ~]# systemctl restart docker.service
而后使用下载好的registry镜像启动一个容器,默认状况下仓库存放于容器内的/tmp/registry目录中,使用-v选项能够将本地目录挂载到容器内的/tmp/registry下使用,这样就不怕容器被删除后镜像也随之消失。在本地启动一个私有仓库服务,监听端口和json文件端口一致。
[root@master ~]# docker run -d -p 5000:5000 -v /data/registry:/tmp/registry registry b592e059d495c1498f364d4c4c676b18ee3b8e8468fbc054fc598835b7cb2a5a
使用docker tag命令将要上传的镜像标记为192.168.58.159:5000/daoke.
[root@master ~]# docker tag daoke:test 192.168.58.159:5000/daoke [root@master ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE httpd centos ef950765e1bb 46 minutes ago 319 MB lightblueyx/httpd centos ef950765e1bb 46 minutes ago 319 MB 192.168.58.159:5000/daoke latest 2850c25d855b About an hour ago 215 MB daoke test 2850c25d855b About an hour ago 215 MB docker.io/centos latest 5182e96772bf 6 days ago 200 MB docker.io/registry latest b2b03e9146e1 5 weeks ago 33.3 MB
使用docker push上传标记的镜像。
[root@master ~]# docker push 192.168.58.159:5000/daoke The push refers to a repository [192.168.58.159:5000/daoke] 3a1d67a7fe13: Pushed latest: digest: sha256:7eaeca4042df243bcec277baf4867b04ad6e0b1f112b8324e05655f216dc919f size:
在本地能够查看到上传的镜像。
[root@master ~]# cd /data/ [root@master data]# ls registry