目录
DockerFile是用来构建Docker镜像的文本文件,是由一条条构建镜像所需的指令和参数构成的脚本。通过DockerFile构建镜像,只需如下三个步骤即可:
- 编写DokcerFile文件
- docker build命令构建镜像
- docker run镜像运行容器实例
FROM
指定一个已存在的镜像作为模版,第一条必须是FROM
MAINTAINER
描述镜像维护者的姓名和邮箱地址
RUN
镜像构建的时候需要运行的命令
EXPOSE
当前容器对外暴露的端口
WORKDIR
创建容器后,终端默认进来的工作目录
USER
指定该镜像以什么样的用户去执行,不指定默认为root
ENV
用于在后续构建过程中设置环境变量,可以在后续指令中使用
VOLUME
用于数据保存和持久化工作
ADD
将宿主机目录下的文件拷贝进镜像且会自动处理URL和解压tar压缩包
COPY
拷贝文件和目录到镜像中
COPY src dest
CMD
指定容器启动后要干的事情(在容器启动之后要干的事情,而run时在镜像构建时要运行的命令)
docker run 携带参数的话会覆盖CMD指令
CMD可以指定多个,但只有最后一个生效
ENTRYPOINT
类似于CMD指令,但ENTRYPOINT不会被docker run 后的命令覆盖,而这些命令会被当做参数送给ENTRYPOINT指令指定的程序
可以与CMD一起使用,这里的CMD等于在给ENTRYPOINT传参
自定义镜像Centos,Centos7镜像具备vim+ifconfig+jdk8
?将jdk-8u161-linux-x64.tar.gz文件放到myfile目录下
[root@localhost myfile]# pwd
/root/myfile
[root@localhost myfile]# ll
总用量 4
-rw-r--r-- 1 root root 670 12月 29 15:17 Dockerfile
-rw-r--r-- 1 root root 0 12月 29 15:08 jdk-8u161-linux-x64.tar.gz
?在/root/myfile目录下vim Dockerfile文件
FROM centos:7
ENV MYPATH /usr/local
WORKDIR $MYPATH
#安装vim编辑器
RUN yum -y install vim
#安装ifconfig命令查看网络IP
RUN yum -y install net-tools
#安装java8及lib库
RUN yum -y install glibc.i686
RUN mkdir /usr/local/java
#ADD 是相对路径jar,把jdk-8u161-linux-x64.tar.gz添加到容器中,安装包必须要和Dockerfile文件在同一>
ADD jdk-8u161-linux-x64.tar.gz /usr/local/java/
#配置java环境变量
ENV JAVA_HOME /usr/local/java/jdk1.8.0_161
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH
ENV PATH $JAVA_HOME/bin:$PATH
EXPOSE 80
docker build -t 镜像名称:标签名称 .
?经过等待后运行成功
[root@localhost myfile]# docker build -t mycentos7:1.0 .
[+] Building 15.5s (12/12) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 671B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 15.4s
=> [1/7] FROM docker.io/library/centos:7@sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac31 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 45B 0.0s
=> CACHED [2/7] WORKDIR /usr/local 0.0s
=> CACHED [3/7] RUN yum -y install vim 0.0s
=> CACHED [4/7] RUN yum -y install net-tools 0.0s
=> CACHED [5/7] RUN yum -y install glibc.i686 0.0s
=> CACHED [6/7] RUN mkdir /usr/local/java 0.0s
=> CACHED [7/7] ADD jdk-8u161-linux-x64.tar.gz /usr/local/java/ 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:f1976b20af22d403a40902220dfa741412a3bb1dab65771bf43d79650f9d 0.0s
=> => naming to docker.io/library/mycentos7:1.0 0.0s
检查vim工具,ifconfig工具和jdk环境?
[root@localhost myfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos7 1.0 f1976b20af22 13 minutes ago 585MB
[root@localhost myfile]# docker run -it f1976b20af22 /bin/bash
[root@dde119c40796 local]# dir
bin etc games include java lib lib64 libexec sbin share src
[root@dde119c40796 local]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.5 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:05 txqueuelen 0 (Ethernet)
RX packets 8 bytes 656 (656.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@dde119c40796 local]# vim a.txt
?发现制作的镜像没有问题