常用 Dockerfile 整理

lance 2022-07-17 AM 250℃ 0条

1.png


用意

本文仅用于记录日常常用的服务(应用)的 Dockerfile 写法,理论上会持续更新。


Nginx

# 指定使用的镜像
FROM centos:7

# 标签
LABEL maintainer www.itwordsweb.com

# 安装基础环境并清理缓存压缩镜像大小
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/*

# 把 nginx 压缩包传输进去,ADD 会自动解包
ADD nginx-1.22.0.tar.gz /

# 编译安装 nginx ,模块按需添加,配置镜像时区
RUN cd nginx-1.22.0 && \
    ./configure --prefix=/usr/local/nginx \
    --with-http_ssl_module \
    --with-http_stub_status_module && \
    make -j 4 && make install && \
    mkdir /usr/local/nginx/conf/vhost && \
    cd / && rm -rf nginx* && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# 设置环境变量,方便直接使用 nginx 命令
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;"]

PHP

# 指定使用的镜像
FROM centos:7

# 标签
LABEL MAINTAINER www.itwordsweb.com

# 安装基础环境并清理缓存压缩镜像大小
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 sqlite-devel oniguruma oniguruma-devel && \
    yum clean all && \
    rm -rf /var/cache/yum/*

# 把 php 压缩包传输进去, ADD 会自动解包
ADD php-8.1.8.tar.gz /

# 编译安装 php,模块安装上需根据实际需求调整,最后设置镜像时区
RUN cd php-8.1.8 && \
    ./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

# 拷贝预先写好的 php.ini 文件
COPY php.ini /usr/local/php/etc/

# 拷贝预先写好的 php-fpm.conf 文件
COPY php-fpm.conf /usr/local/php/etc/

# 设置工作目录
WORKDIR /usr/local/php

# 映射端口
EXPOSE 9000

# 设置启动命令
CMD ["php-fpm"]

Tomcat

# 指定基础镜像
FROM centos:7

# 标签
LABEL MAINTAINER www.itwordsweb.com

# 设置环境变量
ENV VERSION=8.5.43

COPY CentOS-Base.repo /etc/yum.repos.d/

# 安装基础环境并清理缓存
RUN sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo && \
    yum clean all && \
    yum makecache && \
    yum install java-1.8.0-openjdk wget curl unzip iproute net-tools -y && \
    yum clean all && \
    rm -rf /var/cache/yum/*

# 把 Tomcat 包传输到镜像并自动解包
ADD apache-tomcat-${VERSION}.tar.gz /usr/local/

# 剪切 Tomcat 到指定目录,调优,设置时区
RUN mv /usr/local/apache-tomcat-${VERSION} /usr/local/tomcat && \
    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"]

JAR

# 指定基础镜像
FROM java:8-jdk-alpine

# 标签
LABEL maintainer www.itwordsweb.com

# 设置环境变量
ENV JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF8 -Duser.timezone=GMT+08"

# 修改源地址并设置时区
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
    apk add -U tzdata && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# 把 jar 包拷贝到镜像中
COPY Test.jar /

# 暴露端口
EXPOSE 8888

# 设置启动命令
CMD ["/bin/sh", "-c", "java -jar $JAVA_OPTS /hello.jar"]

标签: docker, linux, k8s, dockerfile

非特殊说明,本博所有文章均为博主原创。

觉得文章不错,打赏一点吧,1分也是爱😀

WeChat Pay

微信打赏

Alipay

支付宝打赏

评论啦~