Dockerfile

Dockerfile格式

FROM centos:latest           #依赖的基础镜像

MAINTAINER fage             #制作文件的人

RUN yum install -y gcc       #安装的软件

COPY run.sh /usr/bin         #执行指令

EXPOSE 80                       #声明使用的端口

CMD [ “run.sh”]                  #构建的时候执行的指令

命令是顺序往下执行的

Dockerfile指令

指令

描述

FROM

构建新镜像是基于那个镜像

MAINTAINER

镜像维护中信息

RUN

构建镜像时运行的shell命令

COPY

拷贝文件或目录到镜像中

ENV

设置环境变量

USER

为RUN、CMD、ENTRYPOINT执行命令指定运行用户

EXPOSE

声明容器运行的端口

HEALTHCHECK

容器中服务器健康检查

WORKDIR

为RUN、CMD、ENTRYPOINT、COPY、ADD设置工作目录

ENTRYPOINT

运行容器时执行,如果有多个ENTRYPOINT指令,最后一个生效

CMD

运行容器时执行,如果有多个CMD指令,最后一个生效

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Build镜像

Usage:docker build [OPTIONS] [PATH] | URL | -[flags]

Options:

-t --tag list    #镜像名称

-f --file string  #指定Dockerfile文件位置

常用组合

docker build .

docker build -t shykes/myapp .

docker build -t shykes/myapp -f /path/Dockerfile /path             *****

docker build -t shykes/myapp http://www.example.com/Dockerfile

构建业务基础镜像

1、 构建nginx基础镜像

2、 构建PHP基础镜像

3、 构建Tomcat基础镜像

构建nginx镜像步骤

FROM centos:7
MAINTAINER fage
RUN yum install -y gcc gcc-c++ make \
    openssl-devel pcre-devel gd-devel \
    iproute net-tools telnet wget curl && \
    yum clean all && \
    rm -rf /var/cache/yum/*
RUN wget http://nginx.org/download/nginx-1.15.5.tar.gz && \
    tar zxf nginx-1.15.5.tar.gz && \
    cd nginx-1.15.5 &&\
    ./configure --prefix=/usr/local/nginx \
    --with-http_ssl_module \
    --with-http_stub_status_module && \
    make -j 4 && make install && \
    rm -rf /usr/local/nginx/html/* && \
    echo "ok" >> /usr/local/nginx/html/status.html && \
    cd / && rm -rf nginx-1.12.2* && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

ENV PATH $PATH:/usr/local/nginx/sbin
#COPY nginx.conf /usr/local/nginx/conf/nginx.conf
WORKDIR /usr/local/nginx
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

尽量把无用的内容删除和清空,保证镜像的最小

开始构建

docker build -t nginx:v1 -f Dockerfile-nginx .

 

 

启动构建的容器,并测试访问

docker run -d --name nginx01 -p 88:80 nginx:v1

 

 

 构建PHP镜像

FROM centos:7
MAINTAINER fage
RUN yum install epel-release -y && \
    yum install -y gcc gcc-c++ make gd-devel libxml2-devel \
    libcurl-devel libjpeg-devel libpng-devel openssl-devel \
    libmcrypt-devel libxslt-devel libtidy-devel autoconf \
    iproute net-tools telnet wget curl && \
    yum clean all && \
    rm -rf /var/cache/yum/*

RUN wget http://docs.php.net/distributions/php-5.6.36.tar.gz && \
    tar zxf php-5.6.36.tar.gz && \
    cd php-5.6.36 && \
    ./configure --prefix=/usr/local/php \
    --with-config-file-path=/usr/local/php/etc \
    --enable-fpm --enable-opcache \
    --with-mysql --with-mysqli --with-pdo-mysql \
    --with-openssl --with-zlib --with-curl --with-gd \
    --with-jpeg-dir --with-png-dir --with-freetype-dir \
    --enable-mbstring --with-mcrypt --enable-hash && \
    make -j 4 && make install && \
    cp php.ini-production /usr/local/php/etc/php.ini && \
    cp sapi/fpm/php-fpm.conf /usr/local/php/etc/php-fpm.conf && \
    sed -i "90a \daemonize = no" /usr/local/php/etc/php-fpm.conf && \
    mkdir /usr/local/php/log && \
    cd / && rm -rf php* && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

ENV PATH $PATH:/usr/local/php/sbin:/usr/local/bin:/usr/local/php/bin
COPY php.ini /usr/local/php/etc/
COPY php-fpm.conf /usr/local/php/etc/
WORKDIR /usr/local/php
EXPOSE 9000
CMD ["php-fpm"]

开始构建

docker build -t php:v1 -f Dockerfile-php .

验证

docker run -d --restart=always --name php01 php:v1

docker ps -l

docker exec -it php01 bash

cd /usr/local/php/bin && ./php -v

构建Tomcat镜像

FROM centos:7
MAINTAINER fage

ENV VERSION=8.0.50

RUN yum install java-1.8.0-openjdk wget curl unzip iproute net-tools -y && \
    yum clean all && \
    rm -rf /var/cache/yum/*

#RUN wget http://mirrors.shu.edu.cn/apache/tomcat/tomcat-8/v${VERSION}/bin/apache-tomcat-${VERSION}.tar.gz && \
COPY apache-tomcat-8.0.50.tar.gz /tmp
RUN cd /tmp && \
    tar zxf apache-tomcat-${VERSION}.tar.gz && \
    mv apache-tomcat-${VERSION} /usr/local/tomcat && \
    rm -rf apache-tomcat-${VERSION}.tar.gz /usr/local/tomcat/webapps/* && \
    mkdir /usr/local/tomcat/webapps/test && \
    echo "ok" > /usr/local/tomcat/webapps/test/status.html && \
    sed -i '1a JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom"' /usr/local/tomcat/bin/catalina.sh && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

ENV PATH $PATH:/usr/local/tomcat/bin
WORKDIR /usr/local/tomcat
EXPOSE 8080
CMD ["catalina.sh", "run"]   #前台运行  

开始构建

docker build -t tomcat:v1 -f Dockerfile-tomcat .

构建项目镜像包

cat >Dockerfile <<EOF

FROM tomcat:v1

COPY jenkins.war /usr/local/tomcat/webapps/ROOT.war

EOF

 

docker build -t tomcat:v2 -f Dockerfile .

docker run -d --name tomcat02 -p 8090:8080 tomcat:v2

docker exec -it tomcat02  bash

ls webapps/

 

posted @ 2020-10-03 15:53  缺个好听的昵称  阅读(170)  评论(0编辑  收藏  举报