第六节.dockerfile
- dockerfile指令
- FROM
- 构建的镜像来自于哪个镜像_FROM CentOS6
- MAINTAINER
- 镜像维护者的名字
- RUN
- 构建镜像时运行的shell命令 例如:RUN ["yum", "install", "httpd"]_RUN yum install httpd
- CMD
- 运行容器时执行的shell命令 例如:CMD ["-c", "/start.sh"]; CMD ["/usr/sbin/sshd", " -D"]; CMD /.usr/sbin/sshd -D
- EXPOSE
- 声明容器运行的服务端口 例如:EXPOSE 80 443
- ENV
- 设置环境内环境变量 例如:ENV MYSQL_ROOT_PASSWORD 123123
- ADD
- 拷贝文件或目录到镜像,如果是url或压缩包会自动下载或自动解压 例如:ADD
... ;ADD [" ", ..." "]; ADD https://xx.com/html/tar.gzvar/www/html; ADD httl.tar.gz /var/www.html
- 拷贝文件或目录到镜像,如果是url或压缩包会自动下载或自动解压 例如:ADD
- COPY
- 拷贝文件或目录到镜像,用法和add类似 例如:COPY ./start.sh/start.sh
- ENTRYPOINT
- 运行容器是执行的sehll命令 例如:ENTRYPOINT ["/bin/bash", "-c", "/start.sh"]; ENTRYPOINT /bin/bash -c '/start.sh'
- VOLUME
- 指定容器挂载到宿主机自动生成的目录或其他容器 例如:VOLUME ["/var/lib/mysql"]
- USER
- 为RUN,CMD,ENTRYPOINT执行命令指定运行用户 例如:USER
[: ] or USER [: ]; USER xiaohei
- 为RUN,CMD,ENTRYPOINT执行命令指定运行用户 例如:USER
- WORKDIR
- 为RUN,CMD,ENTRYPOINT,COPY和ADD设置工作目录 例如:WORKDIR /data
- HEALTHCHECK
- 健康检查 例如:HEALTHCHECK --interval=5m --timeout=3s --retries=3 CMD curl -f http://localhost/ || eixt 1
- ARG
- 在构建镜像时指定一些参数 例如:
- FROM centos:6
- ARG user #ARGuser=root
- USER $user
-
docker build --build-arg user=xiaohei Dockerfile
- 示例:
- FROM docker.io/centos
- LABEL "auth"="Uncle.joker" \ "mail"="[email protected]"
- ENV TIME_ZOME Asia/Shanghai
- RUN yum install -y gcc gcc-c++ make openssl-devel prce-devel ADD nginx-1.14.2.tar.gz /opt/ RUN cd /opt/nginx-1.14.2 && \ ./configure --prefix=/usr/local/nginx && \ make -j 4 && \ make install
- RUN rm -rf /opt/nginx* && \ yum clean all && \ echo "{TIME_ZOME} /etc/localtime
- COPY nginx.conf /usr/local/nginx/conf/ WORKDIR /usr/local/nginx/
- EXPOSE 80
- CMD ["./sbin/nginx","-g","daemon off;"]
- FROM
- build镜像命令
- docker image build [OPTIONS] PATH |URL| -
- -t --tag list #镜像名称
- -f --file string #指定Dockerfiel文件位置
- 示例:
- cd /root/dockerfile/nginx && docker image build -t mytomcat/nginx:1.5 .
- docker run -itd --name nginx1 --network lnmp -p 8888:80 mytomcat/nginx:1.5
- docker image build [OPTIONS] PATH |URL| -
- 构建PHP网站环境镜像
- 目录结构
- php
- ├── Dockerfile
- └── php-7.2.11.tar.gz
- php
- Dockerfile文件内容
- FROM centos:latest MAINTAINER http://blog.51cto.com/andyxu
- ENV TIME_ZOME Asia/Shanghai ARG PV="php-7.2.11"
- ADD $PV.tar.gz /tmp
- RUN yum -y install gcc gcc-c++ make gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl-devel bison \
- && mkdir /data \
- && cd /tmp/$PV \
- && ./configure --prefix=/data/php \
- --with-config-file-path=/data/php/etc \
- --with-gd --with-mysqli \
- --with-openssl --with-zlib --with-curl \
- --with-jpeg-dir --with-png-dir --with-iconv \
- --enable-fpm --enable-zip --enable-mbstring \
- && make -j 4 \
- && make install \
- && cp /data/php/etc/php-fpm.conf.default /data/php/etc/php-fpm.conf \
- && cp /data/php/etc/php-fpm.d/www.conf.default /data/php/etc/php-fpm.d/www.conf \
- && sed -i '/;daemonize/a\daemonize = no' /data/php/etc/php-fpm.conf \
- && sed -i 's/127.0.0.1/0.0.0.0/g' /data/php/etc/php-fpm.d/www.conf \
- && echo "${TIME_ZOME}" > /etc/timezone \
- && ln -sf /usr/share/zoneinfo/${TIME_ZOME} /etc/localtime \
- && rm -rf /tmp/php* \
- && yum clean all \
- && yum -y remove gcc gcc-c++ make
- WORKDIR /data/php/
- EXPOSE 9000
- CMD ["sbin/php-fpm","-c","etc/php-fpm.conf"]
-
注:daemonize = no表示fpm在前台运行
- 目录结构
- 构建Java网站环境镜像
- 1.首先下载jdk和tomcat对应的tar文件放在主目录下 /usr/local/
- 2.配置dockerfile
-
构建指令,后续的指令都依赖于该指令指定的image
- FROM ubuntu:14.04
-
指定镜像创建者信息
- MAINTAINER gezongyang "[email protected]"
-
更新源
- RUN apt-get update
-
构建指令,将源文件拷贝到容器的目标位置(即把java与Tomcat添加到容器中)
- ADD jdk-8u101-linux-x64.tar.gz /usr/local/java
- ADD apache-tomcat-8.0.43.tar.gz /usr/local/tomcat
-
设置环境变量
- ENV JAVA_HOME /usr/local/java/jdk1.8.0_101
- ENV PATH PATH
- ENV CLASSPATH .:$JAVA_HOME/lib
-
为tomcat启动命令授权
- RUN chmod +x /usr/local/tomcat/apache-tomcat-8.0.43/bin/*.sh
-
指定容器需要映射到宿主机器上的端口
- EXPOSE 8080
-
设置容器启动时执行的操作
- ENTRYPOINT /usr/local/tomcat/apache-tomcat-8.0.43/bin/startup.sh && /bin/bash && tail -f /usr/local/tomcat/apache-tomcat-8.0.43/logs/catalina.out
-
- 3.用dockerfile文件生成镜像
- docker build -t zingdocker/jdk-tomcat .