电竞盘口软件使用Dockerfile构建Docker镜像

作者:编程    发布时间:2020-01-12 11:51     浏览次数 :

[返回]

怎样是镜像缓存?

镜像的缓存性格

Docker 会缓存已部分镜像层,创设新的镜像时,借使某层镜像已经存在,就径直行使,不须求创制。
example:

# 1. 修改Dockerfile,添加一行
root@fbo-virtual-machine:~/docker-files# cat Dockerfile 
FROM ubuntu
RUN apt-get update && apt-get install -y vim
COPY testfile /
# 2. 运行docker build,没有全部从新构建镜像,直接调用缓存镜像4b11a920d1d1
root@fbo-virtual-machine:~/docker-files# docker build -t ubuntu-with-vi-2 .
Sending build context to Docker daemon   2.56kB
Step 1/3 : FROM ubuntu
 ---> 8b72bba4485f
Step 2/3 : RUN apt-get update && apt-get install -y vim
 ---> Using cache
 ---> 4b11a920d1d1
Step 3/3 : COPY testfile /
 ---> af5a360ab7a9
Removing intermediate container 4079ecd04817
Successfully built af5a360ab7a9
Successfully tagged ubuntu-with-vi-2:latest

一经大家期望在塑造镜像时不选拔缓存,能够在docker build 命令中加上--no-cache参数。

Dockerfile 中每三个发令都会创设一个镜像层,上层是依据于下层的。无论怎么着时候,只要某后生可畏层产生变化,其上边全数层的缓存都会失效。

也便是说,如若大家转移 Dockerfile 指令的实践各种,也许改过或丰盛指令,都会使缓存失效。如上例子,修正COPY和RUN的说话地点,会全新创设镜像。

而外在塑造镜像时行使缓存,Docker在下载镜像时也会接纳。example:

root@fbo-virtual-machine:~/docker-files# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
# 这里会提示base镜像已经存在,跳过下载
aa18ad1a0d33: Already exists 
2b28e4afdec2: Pull complete 
802b6cd5ed3b: Pull complete 
6f2336b7c318: Pull complete 
d7c441746c9e: Pull complete 
ba7f19f905f9: Pull complete 
5c7522be7faf: Pull complete 
Digest: sha256:a46bd62f5286321ed19875778246f3afe97df5f9e91f0dd369a6ecfa529dbe81
Status: Downloaded newer image for httpd:latest

6. Dockerfile最好执行

  • 使用.dockerignore文件

为了在docker build进度中更加快上传和进一层高效,应该选择贰个.dockerignore文件用来息灭营造镜像时无需的文件或目录。举例,除非.git在创设进度中必要采用,不然你应该将它丰盛到.dockerignore文件中,那样能够节约成千上万光阴。

  • 防止安装不必要的软件包

为了减弱复杂性、信任性、文件大小以至营造时间,应该制止安装额外的或不供给的包。比如,无需在三个数量库镜像中安装一个文件编辑器。

  • 各种容器都跑一个经过

在大多数场合下,二个器皿应该只单独跑四个主次。解耦应用到三个容器使其更易于横向扩充和重用。假使贰个劳务信任其它八个劳务,能够参考 Linking Containers Together。

  • 微小化层

咱俩领略每试行贰个下令,都会有二遍镜像的提交,镜疑似分段的构造,对于 Dockerfile,应该找到可读性和微小化层之间的平衡。

  • 多行参数排序

假使可能,通过字母顺序来排序,那样能够幸免安装包的双重何况更易于更新列表,其余可读性也会越来越强,增多三个空行使用 换行:

RUN apt-get update && apt-get install -y 
  bzr 
  cvs 
  git 
  mercurial 
  subversion
  • 创设缓存

镜像创设进程中会依据 Dockerfile 的各类依次推行,每实施一遍指令 Docker 会找出是还是不是有存在的镜像缓存可复用,若无则开改过的镜像。假使不想利用缓存,则足以在docker build 时增多--no-cache=true选项。

从底工镜像初始就早就在缓存中了,下三个指令会相比较全部的子镜像搜索是还是不是进行同样的指令,若无则缓存失效。在大多景况下只相比较Dockerfile 指令和子镜像就丰裕了。ADD 和 COPY 指令除了那个之外,实施 ADD 和 COPY 时存放到镜像的文本也是内需检讨的,完毕三个文本的校验之后再选取这些校验在缓存中追寻,假使检查实验的文件改动则缓存失效。RUN apt-get -y update命令只检查命令是或不是般配,假设相称就不会再实施更新了。

为了有效地使用缓存,你须要保证您的Dockerfile意气风发致,并且尽量在最终更改。

  • 构建镜像时,给其打上叁个易读的镜像标签有扶持支持了解镜像的魔法,例如:

镜像小结

  • images 展现镜像列表
  • history 彰显镜像布局历史
  • commit 从容器成立新镜像
  • build 从Dockerfile创设镜像
  • tag 给镜像打tag
  • pull 从registry下载镜像
  • push 将镜像上传到registry
  • rmi 删除docker host上的镜像,假使一个镜像有七个tag,唯有当所有tag被去除时,镜像才会去除。
  • search 寻觅docker hub中的镜像

目录

  1. 前言
  2. Dockerfile的书写法则及指令使用方法
  3. 创办Dockerfile,创设运行条件
  4. 创设镜像
  5. Dockerfile参照他事他说加以侦查示例
  6. Dockerfile最棒施行

  7. 前言


Dockfile是生龙活虎种被Docker程序解释的脚本,Dockerfile由一条一条的一声令下组成,每条指令对应Linux上边包车型大巴一条命令。Docker程序将这么些Dockerfile指令翻译确实的Linux命令。Dockerfile有友好书写格式和支撑的指令,Docker程序杀绝这么些命令间的依赖关系,形似于Makefile。Docker程序将读取Dockerfile,依据指令生成定制的image。相比较image这种黑盒子,Dockerfile这种分明的台本更便于被使用者选用,它显著的标识image是怎么发生的。有了Dockerfile,当我们必要定制本人额外的需求时,只需在Dockerfile上丰盛可能更改命令,重新生成image就可以,省去了敲命令的难为。

就优质的Linux幼功镜像来讲,大小关系如下:

Dockerfile常用命令

  • FROM - 指定base镜像
  • MAINTAINE凯雷德 - 镜像小编
  • COPY - 将文件从build context复制到镜像。
    • COPY src dest
    • COPY ["src","dest"]
  • ADD - 与COPY肖似,差别的是假如src是归档文件(tar、zip、tgz、xz等),文件会自动解压到dest。
  • ENV - 设置意况变量
  • EXPOSE - 钦命容器中的进度会监听有些端口
  • VOLUME - 将文件或目录证明为volume
  • WO福睿斯KDIOdyssey - 为前面RUN,CDM,ENTHavalYPOINT,ADD或COPY是指镜像中的当前工作目录。
  • RUN - 在容器中运行钦点命令
  • CMD - 容器运营时运转钦点命令,当有四个CMD指令时,唯有最有叁个见到成效。CMD能够被docker run之后的参数替换
  • ENTXC90YPOINT - 设置运维时运营的命令,当有多个ENTRubiconYPOINT指令是,唯有最后三个生效。CMD或docker run 之后的参数会被看成参数字传送递给ENTEvoqueYPOINT。
## 1. 创建Dockerfile
root@fbo-virtual-machine:~/docker-files# cat Dockerfile 
# my dockerfile
FROM busybox
MAINTAINER recoba01@163.com
WORKDIR /testdir
RUN touch tmpfile1
COPY ["testfile","."]
ADD ["fbo.tar.gz", "."]
ENV WELCOME "You are in my container, welcome!"
## 2. 确认工作环境
root@fbo-virtual-machine:~/docker-files# ls
Dockerfile  fbo.tar.gz  testfile
## 3. 构建my-image镜像
root@fbo-virtual-machine:~/docker-files# docker build -t my-image .
Sending build context to Docker daemon  3.584kB
Step 1/7 : FROM busybox
 ---> 54511612f1c4
Step 2/7 : MAINTAINER recoba01@163.com
 ---> Running in 345098f14ab3
 ---> 2bf416443eed
Removing intermediate container 345098f14ab3
Step 3/7 : WORKDIR /testdir
 ---> eca59be9c015
Removing intermediate container 3a343c30b0ff
Step 4/7 : RUN touch tmpfile1
 ---> Running in f9c7f78f2586
 ---> c3ebd76a7ba4
Removing intermediate container f9c7f78f2586
Step 5/7 : COPY testfile .
 ---> 8eb505c80d42
Removing intermediate container 4d22491de73a
Step 6/7 : ADD fbo.tar.gz .
 ---> 5a5e8200f7a4
Removing intermediate container bfe0b9e0d6c4
Step 7/7 : ENV WELCOME "You are in my container, welcome!"
 ---> Running in 83e9ef179ea6
 ---> 809d654b00fc
Removing intermediate container 83e9ef179ea6
Successfully built 809d654b00fc
Successfully tagged my-image:latest
## 检查镜像内容
root@fbo-virtual-machine:~/docker-files# docker run -it my-image
/testdir # pwd
/testdir
/testdir # ls
fbo       testfile  tmpfile1
/testdir # echo $WELCOME
You are in my container, welcome!

4. 创设镜像

依据安插完的dockerfile营造Docker镜像,并运行docker容器。

docker build -t wechat-tomcat.  
docker run -d -p 8090:8080 wechat-tomcat 

暗中同意情形下,tomcat会占用8080端口,所以在运营container的时候,钦赐了 -p 8090:8080,映射到宿主机端口正是8090。

为了使得地应用缓存,须要确认保证 Dockerfile 中指令的 接连几日来大器晚成致,尽量将生机勃勃律指令的一些放在前方,而将有差距性的指令放在后边

营造镜像

Docker提供了两种塑造镜像的点子:

  1. docker commit命令
  2. Dockerfile 创设文件

3. 创造Dockerfile,创设运转蒙受

Dockerfile文件

# 指定基于的基础镜像
FROM ubuntu:13.10  

# 维护者信息
MAINTAINER zhangjiayang "zhangjiayang@sczq.com.cn"  

# 镜像的指令操作
# 获取APT更新的资源列表
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe"> /etc/apt/sources.list
# 更新软件
RUN apt-get update  

# Install curl  
RUN apt-get -y install curl  

# Install JDK 7  
RUN cd /tmp &&  curl -L 'http://download.oracle.com/otn-pub/java/jdk/7u65-b17/jdk-7u65-linux-x64.tar.gz' -H 'Cookie: oraclelicense=accept-securebackup-cookie; gpw_e24=Dockerfile' | tar -xz  
RUN mkdir -p /usr/lib/jvm  
RUN mv /tmp/jdk1.7.0_65/ /usr/lib/jvm/java-7-oracle/  

# Set Oracle JDK 7 as default Java  
RUN update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-7-oracle/bin/java 300     
RUN update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/java-7-oracle/bin/javac 300     

# 设置系统环境
ENV JAVA_HOME /usr/lib/jvm/java-7-oracle/  

# Install tomcat7  
RUN cd /tmp && curl -L 'http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.8/bin/apache-tomcat-7.0.8.tar.gz' | tar -xz  
RUN mv /tmp/apache-tomcat-7.0.8/ /opt/tomcat7/  

ENV CATALINA_HOME /opt/tomcat7  
ENV PATH $PATH:$CATALINA_HOME/bin  

# 复件tomcat7.sh到容器中的目录 
ADD tomcat7.sh /etc/init.d/tomcat7  
RUN chmod 755 /etc/init.d/tomcat7  

# Expose ports.  指定暴露的端口
EXPOSE 8080  

# Define default command.  
ENTRYPOINT service tomcat7 start && tail -f /opt/tomcat7/logs/catalina.out

tomcat7.sh命令文件

export JAVA_HOME=/usr/lib/jvm/java-7-oracle/  
export TOMCAT_HOME=/opt/tomcat7  

case $1 in  
start)  
  sh $TOMCAT_HOME/bin/startup.sh  
;;  
stop)  
  sh $TOMCAT_HOME/bin/shutdown.sh  
;;  
restart)  
  sh $TOMCAT_HOME/bin/shutdown.sh  
  sh $TOMCAT_HOME/bin/startup.sh  
;;  
esac  
exit 0  
FROM debian:latestMAINTAINER codesheep@163.comENTRYPOINT [ "ls", "-l"]CMD ["-a"]

RUN & CMD & ENTRYPOINT

RUN、CMD和ENT奥迪Q7YPOINT功用有些相像,它们有以下分别:

  1. RUN实施命令并创制新的镜像层,RUN常用于安装软件包。
  2. CMD是指容器运转后暗许试行的指令及参数,但是CMD能够被docker run前边跟的命令行参数替换。
  3. ENT奥迪Q3YPOINT配置容器运维时运转的一声令下。

2. Dockerfile的书写法则及指令使用办法

Dockerfile的授命是忽略大小写的,提出采纳大写,使用#用作注释,每风度翩翩行只帮助一条指令,每条指令能够引导五个参数。

Dockerfile的吩咐依照效果与利益能够分成两种,营造指令和设置指令。创设指令用于营造image,其钦命的操作不会在运维image的容器上施行;设置指令用于安装image的属性,其钦赐的操作将在运行image的容器中施行。

(1卡塔尔(قطر‎. FROM(钦命功底image)

营造指令,必得钦命且需求在Dockerfile别的指令的前面。后续的指令都凭仗于该指令钦点的image。FROM指令钦定的根底image能够是法定远程酒馆中的,也得以放在本地旅舍。

该指令有三种格式:

FROM <image>

钦命根基image为该image的末段改正的版本。也许:

FROM <image>:<tag>

点名功底image为该image的贰个tag版本。

(2State of Qatar. MAINTAINE福睿斯(用来钦命镜像创制者信息)

创设指令,用于将image的制作者相关的消息写入到image中。当大家对该image实行docker inspect命令时,输出中有照看的字段记录该音讯。

指令格式:

MAINTAINER <name>

(3卡塔尔(قطر‎. RUN(安装软件用)

营造指令,RUN能够运转任何被底蕴image扶持的授命。如幼功image接纳了ubuntu,那么软件管理有些只可以动用ubuntu的吩咐。

  • RUN命令将要当前image中实行任性合法命令并交由奉行结果。命令实施提交后,就可以自动试行Dockerfile中的下三个下令。
  • 层级 RUN 指令和生成提交是符合Docker宗旨境念的做法。它同意像版本调控那样,在放肆多个点,对image 镜像开展定制化营造。
  • RUN 指令缓存不会在下个指令施行时自动失效。比如 RUN apt-get dist-upgrade -y 的缓存就大概被用来下叁个指令. --no-cache 标识能够被用于强逼收回缓存使用。

一声令下格式:

RUN <command> (the command is run in a shell - /bin/sh -c)
RUN ["executable", "param1", "param2" ... ] (exec form)

(4卡塔尔国. CMD(设置container运转时进行的操作)

安装指令,用于container运行时内定的操作。该操作能够是推行自定义脚本,也足以是实行系统命令。该指令只好在文书中存在三次,如若有多个,则只进行最终一条。

该指令有三种格式:

CMD ["executable","param1","param2"] (like an exec, this is the preferred form)
CMD command param1 param2 (as a shell)

当Dockerfile钦赐了ENTLacrosseYPOINT,那么使用上边包车型大巴格式:

CMD ["param1","param2"] (as default parameters to ENTRYPOINT)

ENTGL450YPOINT钦命的是三个可实施的本子大概程序的路子,该钦定的剧本可能程序将会以param1和param2作为参数实施。所以借使CMD指令使用方面的方式,那么Dockerfile中必必要有配套的ENTWranglerYPOINT。

(5卡塔尔. ENT本田UR-VYPOINT(设置container运行时实行的操作)

设置指令,钦命容器运行时试行的吩咐,能够反复设置,可是唯有最后贰个卓有功效。

两种格式:

ENTRYPOINT ["executable", "param1", "param2"] (like an exec, the preferred form)
ENTRYPOINT command param1 param2 (as a shell)

该指令的施用分为三种情况,大器晚成种是单独使用,另风流洒脱种和CMD指令协作使用。

当独自使用时,借让你还选用了CMD命令且CMD是二个总体的可推行的吩咐,那么CMD指令和ENT汉兰达YPOINT会相互覆盖唯有最终八个CMD只怕ENTTiguanYPOINT有效。

# CMD指令将不会被执行,只有ENTRYPOINT指令被执行  
CMD echo “Hello, World!”  
ENTRYPOINT ls -l  

另生机勃勃种用法和CMD指令协作使用来钦命ENTMuranoYPOINT的默许参数,那个时候CMD指令不是多个完好无损的可实施命令,仅仅是参数部分;ENT奇骏YPOINT指令只可以选取JSON格局钦命实践命令,而不可能钦命参数。

FROM ubuntu  
CMD ["-l"]  
ENTRYPOINT ["/usr/bin/ls"]  

(6卡塔尔. USE冠道(设置container容器的顾客)

安装指令,设置运营容器的客户,私下认可是root客户。

# 指定memcached的运行用户  
ENTRYPOINT ["memcached"]  
USER daemon  
或  
ENTRYPOINT ["memcached", "-u", "daemon"]  

(7State of Qatar. EXPOSE(钦定容器须要映射到宿主机器的端口)

设置指令,该指令会将容器中的端口映射成宿主机器中的有个别端口。当您要求拜见容器的时候,能够不是用容器的IP地址而是接受宿主机器的IP地址和照耀后的端口。要达成全体操作供给三个步骤,首先在Dockerfile使用EXPOSE设置必要映射的容器端口,然后在运作容器的时候钦定-p选项加上EXPOSE设置的端口,那样EXPOSE设置的端口号会被私下映射成宿主机器中的贰个端口号。也得以钦定需求映射到宿主机器的足够端口,这时候要保障宿主机器上的端口号未有被使用。EXPOSE指令能够一回设置多个端口号,相应的周转容器的时候,能够配套的频仍运用-p选项。

指令格式:

EXPOSE <port> [<port>...]

# 映射一个端口  
EXPOSE port1  
# 相应的运行容器使用的命令  
docker run -p port1 image  

# 映射多个端口  
EXPOSE port1 port2 port3  
# 相应的运行容器使用的命令  
docker run -p port1 -p port2 -p port3 image  
# 还可以指定需要映射到宿主机器上的某个端口号  
docker run -p host_port1:port1 -p host_port2:port2 -p host_port3:port3 image  

端口映射是docker特别首要的一个效果与利益,原因在于大家每便运转容器的时候容器的IP地址无法钦点而是在桥接网卡的地址范围内随便生成的。宿主机器的IP地址是稳固的,大家能够将容器的端口的照射到宿主机器上的三个端口,免去每一回访谈容器中的有些服务时都要翻开容器的IP之处。对于三个周转的容器,能够接纳docker port加上容器中须求映射的端口和容器的ID来查看该端口号在宿主机器上的照射端口。

(8卡塔尔(قطر‎. ENV(用于安装景况变量)

  • ENV指令能够用来为docker容器设置遭遇变量
  • ENV设置的情况变量,能够行使docker inspect命令来查看。同一时候还足以动用docker run --env <key>=<value>来校正景况变量。

格式:

ENV <key> <value>

安装了后,后续的RUN命令都足以选取,container运行后,能够因而docker inspect查看那么些意况变量,也能够通过在docker run --env key=value时设置或退换境况变量。

生机勃勃旦你安装了JAVA程序,供给安装JAVA_HOME,那么能够在Dockerfile中如此写:

ENV JAVA_HOME /path/to/java/dirent

(9State of Qatar. ADD(从src复制文件到container的dest路线)

创设指令,全数拷贝到container中的文件和文件夹权限为0755,uid和gid为0;假设是叁个索引,那么会将该目录下的有所文件增加到container中,不包括目录;借使文件是可识别的压缩格式,则docker会援助解压缩(注意压缩格式);若是<src>是文本且<dest>中不使用斜杠结束,则会将<dest>视为文件,<src>的剧情会写入<dest>;假若<src>是文本且<dest>中选拔斜杠截至,则会<src>文件拷贝到<dest>目录下。

格式:

ADD <src> <dest>

  • <src> 是相对被创设的源目录的相对路线,能够是文本或目录的不二等秘书技,也足以是一个长间隔的文件url;
  • <dest> 是container中的相对路线

(10State of Qatar. VOLUME (钦赐挂载点State of Qatar

创办三个能够从地点主机或其余容器挂载的挂载点,平日用来存放数据库和急需保持的数据等。

Volume设置指令,使容器中的贰个索引具备悠久化存款和储蓄数据的效果,该目录可以被容器自个儿使用,也能够分享给其余容器使用。大家通晓容器使用的是AUFS,这种文件系统无法长久化数据,当容器关闭后,全体的转移都会扬弃。当容器中的应用有长久化数据的供给时得以在Dockerfile中选取该指令。

格式:

VOLUME ["<mountpoint>"]

例如:

FROM base  
VOLUME ["/tmp/data"] 

运转通过该Dockerfile生成image的容器,/tmp/data目录中的数据在容器关闭后,里面包车型大巴多少还存在。比如另二个容器也可以有悠久化数据的急需,且想接受方面容器分享的/tmp/data目录,那么能够运作下边包车型大巴下令运转一个容器:

docker run -t -i -rm -volumes-from container1 image2 bash 

证实:container1为第二个容器的ID,image2为第一个容器运营image的名字。

(11卡塔尔. WOMuranoKDI奇骏(切换目录)

安装指令,能够屡次切换(也便是cd命令卡塔尔国,对RUN,CMD,ENTTiggoYPOINT生效。

格式:

WORKDIR /path/to/workdir

示例:

# 在 /p1/p2 下执行 vim a.txt  
WORKDIR /p1 
WORKDIR p2 
RUN vim a.txt  

(12卡塔尔国. ONBUILD(在子镜像中实施)

ONBUILD 钦点的授命在创设镜像时并不实行,而是在它的子镜像中实践。

格式:

ONBUILD <Dockerfile关键字>

(13卡塔尔. COPY(复制本地主机的src文件为container的dest卡塔尔

复制本地主机的src文件(为Dockerfile所在目录的相对路线、文件或目录 )到container的dest。目标路线不设不经常,会活动创制。

格式:

COPY <src> <dest>

当使用本地目录为源目录时,推荐应用COPY

(14State of Qatar. A卡宴G(设置营造镜像时变量卡塔尔国

ALX570G指令在Docker1.9版本才投入的新命令,A凯雷德G 定义的变量只在营造 image 时有效,创立完成后变量就失效消失

格式:

ARG <key>=<value>

(15卡塔尔. LABEL(定义标签State of Qatar

概念多少个 image 标签 Owner,并赋值,其值为变量 Name 的值。

格式:

LABEL Owner=$Name

Dockerfile 能够透过 EXPOSE指令 将容器端口映射到主机端口上,但像这种类型会产生镜像在生龙活虎台主机上仅能开发银行叁个器皿!

Dockerfile常用命令

5. Dockerfile参谋示例

亲自去做1:创设Wordpress + nginx运维条件

# 指定基于的基础镜像
FROM ubuntu:14.04

# 维护者信息
MAINTAINER Eugene Ware <eugene@noblesamurai.com>

# Keep upstart from complaining
RUN dpkg-divert --local --rename --add /sbin/initctl
RUN ln -sf /bin/true /sbin/initctl

# Let the conatiner know that there is no tty
ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update
RUN apt-get -y upgrade

# Basic Requirements
RUN apt-get -y install mysql-server mysql-client nginx php5-fpm php5-mysql php-apc pwgen python-setuptools curl git unzip

# Wordpress Requirements
RUN apt-get -y install php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-sqlite php5-tidy php5-xmlrpc php5-xsl

# mysql config, 配置MySQL运行参数
RUN sed -i -e"s/^bind-addresss*=s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf

# nginx config, 配置Nginx运行参数
RUN sed -i -e"s/keepalive_timeouts*65/keepalive_timeout 2/" /etc/nginx/nginx.conf
RUN sed -i -e"s/keepalive_timeout 2/keepalive_timeout 2;ntclient_max_body_size 100m/" /etc/nginx/nginx.conf
RUN echo "daemon off;" >> /etc/nginx/nginx.conf

# php-fpm config
RUN sed -i -e "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g" /etc/php5/fpm/php.ini
RUN sed -i -e "s/upload_max_filesizes*=s*2M/upload_max_filesize = 100M/g" /etc/php5/fpm/php.ini
RUN sed -i -e "s/post_max_sizes*=s*8M/post_max_size = 100M/g" /etc/php5/fpm/php.ini
RUN sed -i -e "s/;daemonizes*=s*yes/daemonize = no/g" /etc/php5/fpm/php-fpm.conf
RUN sed -i -e "s/;catch_workers_outputs*=s*yes/catch_workers_output = yes/g" /etc/php5/fpm/pool.d/www.conf
RUN find /etc/php5/cli/conf.d/ -name "*.ini" -exec sed -i -re 's/^(s*)#(.*)/1;2/g' {} ;

# nginx site conf,将本地Nginx配置文件复制到容器中的目录
ADD ./nginx-site.conf /etc/nginx/sites-available/default

# Supervisor Config
RUN /usr/bin/easy_install supervisor
RUN /usr/bin/easy_install supervisor-stdout
ADD ./supervisord.conf /etc/supervisord.conf

# Install Wordpress
ADD https://wordpress.org/latest.tar.gz /usr/share/nginx/latest.tar.gz
RUN cd /usr/share/nginx/ && tar xvf latest.tar.gz && rm latest.tar.gz
RUN mv /usr/share/nginx/html/5* /usr/share/nginx/wordpress
RUN rm -rf /usr/share/nginx/www
RUN mv /usr/share/nginx/wordpress /usr/share/nginx/www
RUN chown -R www-data:www-data /usr/share/nginx/www

# Wordpress Initialization and Startup Script
ADD ./start.sh /start.sh
RUN chmod 755 /start.sh

# private expose
EXPOSE 3306
EXPOSE 80

# volume for mysql database and wordpress install
VOLUME ["/var/lib/mysql", "/usr/share/nginx/www"]

# 容器启动时执行命令
CMD ["/bin/bash", "/start.sh"]

示例2:构建Ruby on Rails环境

# 指定基础镜像
FROM fcat/ubuntu-universe:12.04

# development tools
RUN apt-get -qy install git vim tmux

# ruby 1.9.3 and build dependencies
RUN apt-get -qy install ruby1.9.1 ruby1.9.1-dev build-essential libpq-dev libv8-dev libsqlite3-dev

# bundler
RUN gem install bundler

# create a "rails" user
# the Rails application will live in the /rails directory
RUN adduser --disabled-password --home=/rails --gecos "" rails

# copy the Rails app
# we assume we have cloned the "docrails" repository locally
#  and it is clean; see the "prepare" script
ADD docrails/guides/code/getting_started /rails

# Make sure we have rights on the rails folder
RUN chown rails -R /rails

# copy and execute the setup script
# this will run bundler, setup the database, etc.
ADD scripts/setup /setup
RUN su rails -c /setup

# copy the start script
ADD scripts/start /start

EXPOSE 3000

# 创建用户
USER rails

# 设置容器启动命令
CMD /start

示范3: 创设Nginx运维蒙受

# 指定基础镜像
FROM sameersbn/ubuntu:14.04.20161014

# 维护者信息
MAINTAINER sameer@damagehead.com

# 设置环境
ENV RTMP_VERSION=1.1.10 
    NPS_VERSION=1.11.33.4 
    LIBAV_VERSION=11.8 
    NGINX_VERSION=1.10.1 
    NGINX_USER=www-data 
    NGINX_SITECONF_DIR=/etc/nginx/sites-enabled 
    NGINX_LOG_DIR=/var/log/nginx 
    NGINX_TEMP_DIR=/var/lib/nginx 
    NGINX_SETUP_DIR=/var/cache/nginx

# 设置构建时变量,镜像建立完成后就失效
ARG BUILD_LIBAV=false
ARG WITH_DEBUG=false
ARG WITH_PAGESPEED=true
ARG WITH_RTMP=true

# 复制本地文件到容器目录中
COPY setup/ ${NGINX_SETUP_DIR}/
RUN bash ${NGINX_SETUP_DIR}/install.sh

# 复制本地配置文件到容器目录中
COPY nginx.conf /etc/nginx/nginx.conf
COPY entrypoint.sh /sbin/entrypoint.sh

# 运行指令
RUN chmod 755 /sbin/entrypoint.sh

# 允许指定的端口
EXPOSE 80/tcp 443/tcp 1935/tcp

# 指定网站目录挂载点
VOLUME ["${NGINX_SITECONF_DIR}"]

ENTRYPOINT ["/sbin/entrypoint.sh"]
CMD ["/usr/sbin/nginx"]

示例4:构建Postgres镜像

# 指定基础镜像
FROM sameersbn/ubuntu:14.04.20161014

# 维护者信息
MAINTAINER sameer@damagehead.com

# 设置环境变量
ENV PG_APP_HOME="/etc/docker-postgresql"
    PG_VERSION=9.5 
    PG_USER=postgres 
    PG_HOME=/var/lib/postgresql 
    PG_RUNDIR=/run/postgresql 
    PG_LOGDIR=/var/log/postgresql 
    PG_CERTDIR=/etc/postgresql/certs

ENV PG_BINDIR=/usr/lib/postgresql/${PG_VERSION}/bin 
    PG_DATADIR=${PG_HOME}/${PG_VERSION}/main

# 下载PostgreSQL
RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - 
 && echo 'deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main' > /etc/apt/sources.list.d/pgdg.list 
 && apt-get update 
 && DEBIAN_FRONTEND=noninteractive apt-get install -y acl 
      postgresql-${PG_VERSION} postgresql-client-${PG_VERSION} postgresql-contrib-${PG_VERSION} 
 && ln -sf ${PG_DATADIR}/postgresql.conf /etc/postgresql/${PG_VERSION}/main/postgresql.conf 
 && ln -sf ${PG_DATADIR}/pg_hba.conf /etc/postgresql/${PG_VERSION}/main/pg_hba.conf 
 && ln -sf ${PG_DATADIR}/pg_ident.conf /etc/postgresql/${PG_VERSION}/main/pg_ident.conf 
 && rm -rf ${PG_HOME} 
 && rm -rf /var/lib/apt/lists/*

COPY runtime/ ${PG_APP_HOME}/
COPY entrypoint.sh /sbin/entrypoint.sh
RUN chmod 755 /sbin/entrypoint.sh

# 指定端口
EXPOSE 5432/tcp

# 指定数据挂载点
VOLUME ["${PG_HOME}", "${PG_RUNDIR}"]

# 切换目录
WORKDIR ${PG_HOME}

# 设置容器启动时执行命令
ENTRYPOINT ["/sbin/entrypoint.sh"]

实际用例能够参见Github的Docker文件相应的身体力行。

  • Github Docker文件参谋
ADD codesheep.tar.gz /path

docker commit

docker commit包括三个步骤:

  1. 运行容器
  2. 修改容器
  3. 将容器保存为新的镜像
# 1. 运行docker容器
root@fbo-virtual-machine:~# docker run -it ubuntu /bin/bash
# 2. 安装vim
root@533618aeb4a4:/# apt install vim -y
# 3. 保存为新镜像
root@fbo-virtual-machine:~# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
533618aeb4a4        ubuntu              "/bin/bash"         5 minutes ago       Up 5 minutes                            pensive_curran
root@fbo-virtual-machine:~# docker commit pensive_curran ubuntu-with-vim
sha256:63cdf5e493abb21ee80fe1b2509b89d1d9b27cd3d3774eaf793cfbbe6e69f998
root@fbo-virtual-machine:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu-with-vim     latest              63cdf5e493ab        10 seconds ago      217MB
ubuntu              latest              8b72bba4485f        5 days ago          120MB
httpd               latest              6b4e03d65aa3        5 days ago          177MB
centos              latest              328edcd84f1b        6 weeks ago         193MB
# 4. 用新的镜像启动容器
root@fbo-virtual-machine:~# docker run -it ubuntu-with-vim /bin/bash
root@da4b749d2e60:/# which vim
/usr/bin/vim

即便docker commit能够创设新的镜像,可是docker并不建议客商通过这种方式构建镜。
原因如下:

  1. 那是大器晚成种手工业创设镜像的办法,轻便失误,功效低且可再度性弱。
  2. 使用者并不知道镜疑似何等创造的,里面是还是不是有恶意程序。也正是说不只怕对镜像进行审计,存在安全隐患。

参照他事他说加以考查资料:

  • Dockerfile最棒施行(意气风发)
  • Dockerfile最棒执行(二)
  • CentOS Dockerfile实践

作者相关的SpringBt试行作品在此:

Dockerfile布局镜像

# 1. 查看Dockerfile内容及位置
root@fbo-virtual-machine:~/docker-files# cat Dockerfile 
FROM ubuntu
RUN apt-get update && apt-get install -y vim
root@fbo-virtual-machine:~/docker-files# pwd
/root/docker-files
root@fbo-virtual-machine:~/docker-files# ls
Dockerfile
# 2. 运行docker build命令, -t 指定新的镜像的名称 .指当前目录 -f 可以指定Dockerfile文件位置
root@fbo-virtual-machine:~/docker-files# docker build -t ubuntu-with-vim .
# 3. docker将build context发送给Docker daemon。build context为镜像构建提供所需要的文件和目录。此例子中
# /root/docker-files/下的所有文件和子目录都会发送Docker Daemon。
Sending build context to Docker daemon  2.048kB
# 4. 执行from 将ubuntu做为base镜像,ubuntu镜像id为8b72bba4485f
Step 1/2 : FROM ubuntu
 ---> 8b72bba4485f
# 5. 执行RUN 安装vim
Step 2/2 : RUN apt-get update && apt-get install -y vim
# 6. 启动id为5e3a66a39881的临时容器
 ---> Running in 5e3a66a39881
Get:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
Get:3 http://security.ubuntu.com/ubuntu xenial-security/universe Sources [48.9 kB]
Get:4 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [445 kB]
Get:5 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]
Get:6 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]
Get:7 http://archive.ubuntu.com/ubuntu xenial/universe Sources [9802 kB]
Get:8 http://security.ubuntu.com/ubuntu xenial-security/restricted amd64 Packages [12.8 kB]
Get:9 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [208 kB]
Get:10 http://security.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages [2930 B]
Get:11 http://archive.ubuntu.com/ubuntu xenial/main amd64 Packages [1558 kB]
Get:12 http://archive.ubuntu.com/ubuntu xenial/restricted amd64 Packages [14.1 kB]
Get:13 http://archive.ubuntu.com/ubuntu xenial/universe amd64 Packages [9827 kB]
Get:14 http://archive.ubuntu.com/ubuntu xenial/multiverse amd64 Packages [176 kB]
Get:15 http://archive.ubuntu.com/ubuntu xenial-updates/universe Sources [218 kB]
Get:16 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [811 kB]
Get:17 http://archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 Packages [13.6 kB]
Get:18 http://archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [683 kB]
Get:19 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages [17.5 kB]
Get:20 http://archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages [5177 B]
Get:21 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [6236 B]
Fetched 24.4 MB in 39s (611 kB/s)
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
  file libexpat1 libgpm2 libmagic1 libmpdec2 libpython3.5 libpython3.5-minimal
  libpython3.5-stdlib libsqlite3-0 libssl1.0.0 mime-support vim-common
  vim-runtime
Suggested packages:
  gpm ctags vim-doc vim-scripts vim-gnome-py2 | vim-gtk-py2 | vim-gtk3-py2
  | vim-athena-py2 | vim-nox-py2
The following NEW packages will be installed:
  file libexpat1 libgpm2 libmagic1 libmpdec2 libpython3.5 libpython3.5-minimal
  libpython3.5-stdlib libsqlite3-0 libssl1.0.0 mime-support vim vim-common
  vim-runtime
0 upgraded, 14 newly installed, 0 to remove and 2 not upgraded.
Need to get 12.2 MB of archives.
After this operation, 58.3 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu xenial/main amd64 libgpm2 amd64 1.20.4-6.1 [16.5 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial/main amd64 libmagic1 amd64 1:5.25-2ubuntu1 [216 kB]
Get:3 http://archive.ubuntu.com/ubuntu xenial/main amd64 file amd64 1:5.25-2ubuntu1 [21.2 kB]
Get:4 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libexpat1 amd64 2.1.0-7ubuntu0.16.04.3 [71.2 kB]
Get:5 http://archive.ubuntu.com/ubuntu xenial/main amd64 libmpdec2 amd64 2.4.2-1 [82.6 kB]
Get:6 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libssl1.0.0 amd64 1.0.2g-1ubuntu4.8 [1081 kB]
Get:7 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libpython3.5-minimal amd64 3.5.2-2ubuntu0~16.04.1 [526 kB]
Get:8 http://archive.ubuntu.com/ubuntu xenial/main amd64 mime-support all 3.59ubuntu1 [31.0 kB]
Get:9 http://archive.ubuntu.com/ubuntu xenial/main amd64 libsqlite3-0 amd64 3.11.0-1ubuntu1 [396 kB]
Get:10 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libpython3.5-stdlib amd64 3.5.2-2ubuntu0~16.04.1 [2130 kB]
Get:11 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 vim-common amd64 2:7.4.1689-3ubuntu1.2 [103 kB]
Get:12 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 libpython3.5 amd64 3.5.2-2ubuntu0~16.04.1 [1360 kB]
Get:13 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 vim-runtime all 2:7.4.1689-3ubuntu1.2 [5164 kB]
Get:14 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 vim amd64 2:7.4.1689-3ubuntu1.2 [1036 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 12.2 MB in 19s (643 kB/s)
Selecting previously unselected package libgpm2:amd64.
(Reading database ... 4768 files and directories currently installed.)
Preparing to unpack .../libgpm2_1.20.4-6.1_amd64.deb ...
Unpacking libgpm2:amd64 (1.20.4-6.1) ...
Selecting previously unselected package libmagic1:amd64.
Preparing to unpack .../libmagic1_1%3a5.25-2ubuntu1_amd64.deb ...
Unpacking libmagic1:amd64 (1:5.25-2ubuntu1) ...
Selecting previously unselected package file.
Preparing to unpack .../file_1%3a5.25-2ubuntu1_amd64.deb ...
Unpacking file (1:5.25-2ubuntu1) ...
Selecting previously unselected package libexpat1:amd64.
Preparing to unpack .../libexpat1_2.1.0-7ubuntu0.16.04.3_amd64.deb ...
Unpacking libexpat1:amd64 (2.1.0-7ubuntu0.16.04.3) ...
Selecting previously unselected package libmpdec2:amd64.
Preparing to unpack .../libmpdec2_2.4.2-1_amd64.deb ...
Unpacking libmpdec2:amd64 (2.4.2-1) ...
Selecting previously unselected package libssl1.0.0:amd64.
Preparing to unpack .../libssl1.0.0_1.0.2g-1ubuntu4.8_amd64.deb ...
Unpacking libssl1.0.0:amd64 (1.0.2g-1ubuntu4.8) ...
Selecting previously unselected package libpython3.5-minimal:amd64.
Preparing to unpack .../libpython3.5-minimal_3.5.2-2ubuntu0~16.04.1_amd64.deb ...
Unpacking libpython3.5-minimal:amd64 (3.5.2-2ubuntu0~16.04.1) ...
Selecting previously unselected package mime-support.
Preparing to unpack .../mime-support_3.59ubuntu1_all.deb ...
Unpacking mime-support (3.59ubuntu1) ...
Selecting previously unselected package libsqlite3-0:amd64.
Preparing to unpack .../libsqlite3-0_3.11.0-1ubuntu1_amd64.deb ...
Unpacking libsqlite3-0:amd64 (3.11.0-1ubuntu1) ...
Selecting previously unselected package libpython3.5-stdlib:amd64.
Preparing to unpack .../libpython3.5-stdlib_3.5.2-2ubuntu0~16.04.1_amd64.deb ...
Unpacking libpython3.5-stdlib:amd64 (3.5.2-2ubuntu0~16.04.1) ...
Selecting previously unselected package vim-common.
Preparing to unpack .../vim-common_2%3a7.4.1689-3ubuntu1.2_amd64.deb ...
Unpacking vim-common (2:7.4.1689-3ubuntu1.2) ...
Selecting previously unselected package libpython3.5:amd64.
Preparing to unpack .../libpython3.5_3.5.2-2ubuntu0~16.04.1_amd64.deb ...
Unpacking libpython3.5:amd64 (3.5.2-2ubuntu0~16.04.1) ...
Selecting previously unselected package vim-runtime.
Preparing to unpack .../vim-runtime_2%3a7.4.1689-3ubuntu1.2_all.deb ...
Adding 'diversion of /usr/share/vim/vim74/doc/help.txt to /usr/share/vim/vim74/doc/help.txt.vim-tiny by vim-runtime'
Adding 'diversion of /usr/share/vim/vim74/doc/tags to /usr/share/vim/vim74/doc/tags.vim-tiny by vim-runtime'
Unpacking vim-runtime (2:7.4.1689-3ubuntu1.2) ...
Selecting previously unselected package vim.
Preparing to unpack .../vim_2%3a7.4.1689-3ubuntu1.2_amd64.deb ...
Unpacking vim (2:7.4.1689-3ubuntu1.2) ...
Processing triggers for libc-bin (2.23-0ubuntu9) ...
Setting up libgpm2:amd64 (1.20.4-6.1) ...
Setting up libmagic1:amd64 (1:5.25-2ubuntu1) ...
Setting up file (1:5.25-2ubuntu1) ...
Setting up libexpat1:amd64 (2.1.0-7ubuntu0.16.04.3) ...
Setting up libmpdec2:amd64 (2.4.2-1) ...
Setting up libssl1.0.0:amd64 (1.0.2g-1ubuntu4.8) ...
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.22.1 /usr/local/share/perl/5.22.1 /usr/lib/x86_64-linux-gnu/perl5/5.22 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.22 /usr/share/perl/5.22 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base .) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
debconf: falling back to frontend: Teletype
Setting up libpython3.5-minimal:amd64 (3.5.2-2ubuntu0~16.04.1) ...
Setting up mime-support (3.59ubuntu1) ...
Setting up libsqlite3-0:amd64 (3.11.0-1ubuntu1) ...
Setting up libpython3.5-stdlib:amd64 (3.5.2-2ubuntu0~16.04.1) ...
Setting up vim-common (2:7.4.1689-3ubuntu1.2) ...
Setting up libpython3.5:amd64 (3.5.2-2ubuntu0~16.04.1) ...
Setting up vim-runtime (2:7.4.1689-3ubuntu1.2) ...
Setting up vim (2:7.4.1689-3ubuntu1.2) ...
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vim (vim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vimdiff (vimdiff) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rvim (rvim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rview (rview) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vi (vi) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/view (view) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/ex (ex) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in auto mode
Processing triggers for libc-bin (2.23-0ubuntu9) ...
# 7. 安装成功后,将容器保存为id 4b11a920d1d1的镜像
 ---> 4b11a920d1d1
# 8. 删除临时容器
Removing intermediate container 5e3a66a39881
# 9. 构建成功
Successfully built 4b11a920d1d1
Successfully tagged ubuntu-with-vim:latest

docker history博览会示镜像的营造历史,也正是Dockerfile的试行进程。

root@fbo-virtual-machine:~/docker-files# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu-with-vim     latest              4b11a920d1d1        22 minutes ago      217MB
ubuntu              latest              8b72bba4485f        5 days ago          120MB
httpd               latest              6b4e03d65aa3        5 days ago          177MB
centos              latest              328edcd84f1b        6 weeks ago         193MB
root@fbo-virtual-machine:~/docker-files# docker history ubuntu-with-vim
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
4b11a920d1d1        22 minutes ago      /bin/sh -c apt-get update && apt-get insta...   96.5MB              
8b72bba4485f        5 days ago          /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
<missing>           5 days ago          /bin/sh -c mkdir -p /run/systemd && echo '...   7B                  
<missing>           5 days ago          /bin/sh -c sed -i 's/^#s*(deb.*universe...   2.76kB              
<missing>           5 days ago          /bin/sh -c rm -rf /var/lib/apt/lists/*          0B                  
<missing>           5 days ago          /bin/sh -c set -xe   && echo '#!/bin/sh' >...   745B                
<missing>           5 days ago          /bin/sh -c #(nop) ADD file:39d3593ea220e68...   120MB     

也能够窥见施行的是 ls -l -t,即 Dockerfile 中的 CMD 原参数被隐瞒了:

搭建本地 Registry

-d 后台运营容器
-p 将容器的5000端口映射到host的5000端口
-v 将容器/var/lib/registry目录映射到host的/myregistry,用于贮存镜像数据

## 1. 安装本地registry
root@fbo-virtual-machine:~# docker run -d -p 5000:5000 -v /myregistry:/var/lib/registry registry:2
Unable to find image 'registry:2' locally
2: Pulling from library/registry
90f4dba627d6: Pull complete 
b3e11d7b4f5e: Pull complete 
1f032f3c8932: Pull complete 
425585e7aedb: Pull complete 
f45f535a83d2: Pull complete 
Digest: sha256:0f8fe61fa337b8ef02217702ba979b47a7d68717d4628f31592ebff85915f3ba
Status: Downloaded newer image for registry:2
43a490f2c2ba503f02daade785772334ad9e7df6d405681af7a6844b1a74307d
## 2. 打tag
root@fbo-virtual-machine:~# docker tag drfung/ubuntu:v1 fbo-virtual-machine:5000/drfung/ubuntu:v1
## 3. 上传本地镜像
root@fbo-virtual-machine:~# docker push fbo-virtual-machine:5000/drfung/ubuntu:v1
The push refers to a repository [fbo-virtual-machine:5000/drfung/ubuntu]
3996d0debc49: Pushed 
cd1d6655b4e4: Pushed 
a76db6d8fac4: Pushed 
ebf3d6975c70: Pushed 
8aa4fcad5eeb: Pushed 
v1: digest: sha256:c166b406b66c122236d1e0e7f1afb50611cec281827c525f9bf9a67d5c8b003f size: 1357
## 4. 下载本地镜像
root@fbo-virtual-machine:~# docker pull fbo-virtual-machine:5000/drfung/ubuntu:v1
v1: Pulling from drfung/ubuntu
Digest: sha256:c166b406b66c122236d1e0e7f1afb50611cec281827c525f9bf9a67d5c8b003f
Status: Downloaded newer image for fbo-virtual-machine:5000/drfung/ubuntu:v1

据此上边就组成实践来说几条 Dockerfile 的实行经历!

调试Dockerfile

席卷 Dockerfile 在内的此外脚本和顺序都会出错。有错并不骇然,但一定要有方法逐个审查

# 1. 编写Dockerfile
root@fbo-virtual-machine:~/docker-files# cat Dockerfile 
FROM busybox
RUN touch tmpfile
run /bin/bash -c echo "continue to build..."
copy testfile /
# 2. 执行dokcer build报错
root@fbo-virtual-machine:~/docker-files# docker build -t image-debug .
Sending build context to Docker daemon   2.56kB
Step 1/4 : FROM busybox
latest: Pulling from library/busybox
03b1be98f3f9: Pull complete 
Digest: sha256:99ccecf3da28a93c063d5dddcdf69aeed44826d0db219aabc3d5178d47649dfa
Status: Downloaded newer image for busybox:latest
 ---> 54511612f1c4
Step 2/4 : RUN touch tmpfile
 ---> Running in 07f7a9e8f1ea
 ---> 749cc37a2111
Removing intermediate container 07f7a9e8f1ea
Step 3/4 : RUN /bin/bash -c echo "continue to build..."
 ---> Running in 5b7d576cc8f6
/bin/sh: /bin/bash: not found
The command '/bin/sh -c /bin/bash -c echo "continue to build..."' returned a non-zero code: 127
# 3. 进入缓存镜像调试
root@fbo-virtual-machine:~/docker-files# docker run -it 749cc37a2111
/ # /bin/bash -c echo "continue to build..."
sh: /bin/bash: not found
/ # exit

  • 使用exec格式的 ENTGL450YPOINT指令 设置固定的私下认可命令和参数

  • 行使 CMD指令 设置可变的参数

选用国有镜像

## 1. 登录到docker hub
root@fbo-virtual-machine:~# docker login -u drfung
Password: 
Login Succeeded

## 2. 为镜像打上tag
root@fbo-virtual-machine:~# docker tag ubuntu drfung/ubuntu:v1
root@fbo-virtual-machine:~# docker images drfung/ubuntu
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
drfung/ubuntu       v1                  8b72bba4485f        6 days ago          120MB
## 3. 上传镜像到docker hub
root@fbo-virtual-machine:~# docker push drfung/ubuntu
The push refers to a repository [docker.io/drfung/ubuntu]
3996d0debc49: Mounted from library/ubuntu 
cd1d6655b4e4: Mounted from library/ubuntu 
a76db6d8fac4: Mounted from library/ubuntu 
ebf3d6975c70: Mounted from library/ubuntu 
8aa4fcad5eeb: Mounted from library/ubuntu 
v1: digest: sha256:bb5815009271d6f80a64677b4bb60b799c483e839b4d03c2f7723baabaca03df size: 1357

登录 https://hub.docker.com,在Public Repository 中就能够看来上传的镜像,那么些镜像可被其余Docker host下载使用。


Ubuntu > CentOS > Debian
  • 再者,大家也相应在 Dockerfile 的 FROM 指令中一览无遗指明标签 Tag,不要再让 Docker daemon 去猜,如

末段必得说一下,这里的“尽量”是有个度的,适度把握才行。

除此之外,在急需丰富七个文本到镜像中的时候,绝不一遍性凑集增多,而是精选 按需 在须要时 逐个 加多就能够,因为那样便于利用镜像缓存

#尽量避免这种方式EXPOSE 8080:8899#仅仅暴露端口EXPOSE 8080

电竞盘口软件 1ls -l -t

下一篇:没有了