FROM 基础镜像,指定一个已经存在的镜像作为模板
MAINTAINER 镜像维护者的姓名和邮箱地址
RUN 容器构建时需要运行的命令 在docker build时运行(SHELL EXEC两种命令)
EXPOSE 当前容器对外暴露的端口
WORKDIR 指定在容器创建后,终端默认登录进来工作目录
USER 指定该镜像以什么样的用户去执行,如果都不指定,默认是root( 一般都不指定,没必要)
ENV 用来在构建镜像过程中设置环境变量
VOLUME 容器卷,用于数据保存持久化
ADD 将宿主机目录下的文件拷贝进镜像且会自动处理URL和解压tar 压缩包
CMD 指定容器启动后要做的事
注意:Dockerfile中可以有多个CMD指令,但只有最后一个生效,CMD会被docker run之后的参数替换
CMD ["catalina.sh","run"]
docker run -it p 8080:8080 tomcat /bin/bash
docker run 后面追加命令后,会把Dockerfile文件内的CMD覆盖掉。
ENTRYPOINT 用来指定一个容器启动时要运行的命令
类似于CMD,但是不会被docker run 后面的命令覆盖,而且这些命令行参数会被当做参数送给ENTRYPOINT指令指定的程序
FROM nginx
ENTRYPOINT ["nginx","-c"] # 定参
CMD ["/etc/nginx/nginx.conf"] # 变参
docker run nginx:test
nginx -c /etc/nginx/nginx.conf # 实际容器内部运行
docker run nginx:test -c /etc/nginx/new.conf
nginx -c /etc/nginx/new.conf # 实际容器内部运行
要求:Centos7镜像具备vim+ifconfig+jdk8
下载jdk
Index of /jdk/ (yangxingzhen.com)
# 拉取centos镜像
docker pull centos:latest
Dockerfile
FROM centos
MAINTAINER admin
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN sed -i 's/mirror.centos.org/vault.centos.org/g' /etc/yum.repos.d/CentOS-Linux-BaseOS.repo
RUN sed -i 's/mirror.centos.org/vault.centos.org/g' /etc/yum.repos.d/CentOS-Linux-AppStream.repo
RUN sed -i 's/mirror.centos.org/vault.centos.org/g' /etc/yum.repos.d/CentOS-Linux-Extras.repo
RUN sed -i 's/mirrorlist/#mirrorlist/' /etc/yum.repos.d/CentOS-Linux-AppStream.repo
RUN sed -i 's/mirrorlist/#mirrorlist/' /etc/yum.repos.d/CentOS-Linux-Extras.repo
RUN sed -i 's/mirrorlist/#mirrorlist/' /etc/yum.repos.d/CentOS-Linux-BaseOS.repo
RUN sed -i 's/mirrorlist/#mirrorlist/' /etc/yum.repos.d/CentOS-Linux-AppStream.repo
RUN sed -i 's/mirrorlist/#mirrorlist/' /etc/yum.repos.d/CentOS-Linux-Extras.repo
RUN sed -i 's/#baseurl/baseurl/g' /etc/yum.repos.d/CentOS-Linux-BaseOS.repo
RUN sed -i 's/#baseurl/baseurl/g' /etc/yum.repos.d/CentOS-Linux-AppStream.repo
RUN sed -i 's/#baseurl/baseurl/g' /etc/yum.repos.d/CentOS-Linux-Extras.repo
# 安装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
# 将宿主机目录下的文件拷贝进镜像且会自动处理URL和解压tar 压缩包
# 把压缩包添加到容器中,安装包必须要和Dockerfile文件在同一位置
ADD jdk-8u192-linux-x64.tar.gz /usr/local/java
# 配置java环境变量
ENV JAVA_HOME /usr/local/java/jdk1.8.0_192
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
CMD echo $MYPATH
CMD echo "success------------------------ok"
CMD /bin/bash
docker build -t java8_own:latest .
docker run -it [容器名] /bin/bash
[root@kgtdev jdk8]# docker run -it java8_own /bin/bash
[root@08b2c21bd675 local]# ifconfig
eth0: flags=4163 mtu 1500
inet 172.17.0.8 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:08 txqueuelen 0 (Ethernet)
RX packets 7 bytes 586 (586.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 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@08b2c21bd675 local]# java -version
java version "1.8.0_192"
Java(TM) SE Runtime Environment (build 1.8.0_192-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.192-b12, mixed mode)