Docker 镜像综合优化示例
该篇内容,主要对前面的单项优化做综合的汇总,并根据基础镜像的不同,给出不同的优化示例。
基于 Alpine 镜像
基础镜像:
FROM alpine:latest
# 镜像源加速
RUN echo "https://mirrors.aliyun.com/alpine/latest-stable/main/" > /etc/apk/repositories
RUN echo "https://mirrors.aliyun.com/alpine/latest-stable/community/" >> /etc/apk/repositories
# 自定义环境变量
ENV TZ=Asia/Shanghai
#RUN ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime && echo ${TZ} > /etc/timezone
RUN apk update \
&& apk add tzdata \
&& ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime \
&& echo "${TZ}" > /etc/timezone \
&& rm /var/cache/apk/*
Nginx 镜像:
FROM nginx:stable-alpine
# 镜像源加速
RUN echo "https://mirrors.aliyun.com/alpine/latest-stable/main/" > /etc/apk/repositories
RUN echo "https://mirrors.aliyun.com/alpine/latest-stable/community/" >> /etc/apk/repositories
# 自定义环境变量
ENV TZ=Asia/Shanghai
# 设置本地系统时间和时区
RUN ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime && echo ${TZ} > /etc/timezone
OpenJDK 镜像:
#FROM openjdk:11-jre-alpine
#FROM openjdk:11-jdk-alpine
#FROM openjdk:8-jre-alpine
#FROM openjdk:8-jdk-alpine
FROM openjdk:8-jre-alpine
# 镜像源加速
RUN echo "https://mirrors.aliyun.com/alpine/v3.9/main/" > /etc/apk/repositories
RUN echo "https://mirrors.aliyun.com/alpine/v3.9/community/" >> /etc/apk/repositories
# 自定义环境变量
ENV TZ=Asia/Shanghai
# 设置本地系统时间和时区
#RUN ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime && echo ${TZ} > /etc/timezone
# 把 Arthas 安装到基础镜像里 https://arthas.aliyun.com/doc/docker.html
COPY --from=hengyunabc/arthas:latest /opt/arthas /opt/arthas
# 添加字体依赖 && 安装 Arthas 会用到的依赖 && 设置本地系统时间和时区
RUN apk update \
&& apk add --no-cache bash curl ttf-dejavu ttf-freefont fontconfig ospd-netstat busybox-extras tini jq tzdata\
&& fc-cache --force \
&& ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime \
&& echo "${TZ}" > /etc/timezone \
&& rm /var/cache/apk/*
Spring Boot 项目 Dockerfile 示例:
#FROM reg.hiwepy.com/base-image/openjdk-pinpont:1
#FROM reg.hiwepy.com/base-image/openjdk-8-shanghai:alpine
#FROM openjdk:11-jre-alpine
#FROM openjdk:11-jre-slim
#FROM openjdk:11-jdk-alpine
#FROM openjdk:11-jdk-slim
#FROM openjdk:11-jdk-oracle
#FROM openjdk:8-jre-alpine
#FROM openjdk:8-jre-slim
#FROM openjdk:8-jdk-alpine
#FROM openjdk:8-jdk-slim
#FROM openjdk:8-jdk-oracle
FROM openjdk:8-jdk-alpine
# 镜像源加速
RUN echo "https://mirrors.aliyun.com/alpine/v3.9/main/" > /etc/apk/repositories
RUN echo "https://mirrors.aliyun.com/alpine/v3.9/community/" >> /etc/apk/repositories
# 自定义环境变量
ENV TZ=Asia/Shanghai
# 设置本地系统时间和时区
#RUN ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime && echo ${TZ} > /etc/timezone
# 把 Arthas 安装到基础镜像里 https://arthas.aliyun.com/doc/docker.html
COPY --from=hengyunabc/arthas:latest /opt/arthas /opt/arthas
# 添加字体依赖 && 添加字体依赖 && 安装 Arthas 会用到的依赖
RUN apk update \
&& apk add --no-cache bash curl ttf-dejavu ttf-freefont fontconfig ospd-netstat busybox-extras tini jq tzdata \
&& fc-cache --force \
&& ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime \
&& echo "${TZ}" > /etc/timezone \
&& rm /var/cache/apk/*
# 申明临时卷
VOLUME /logs
VOLUME /tmp
# 拷贝本地Jar包到根目录
ADD ./target/app-1.0.0-SNAPSHOT.jar /
# 挂着启动脚本
ENTRYPOINT ["/sbin/tini", "--"]
CMD ["sh", "-c", "java -jar app-1.0.0-SNAPSHOT.jar"]
#ENTRYPOINT ["java", "-jar", "app-1.0.0-SNAPSHOT.jar"]
Uni-App 项目 Dockerfile 示例:
FROM nginx:stable-alpine
# 镜像源加速
RUN echo "https://mirrors.aliyun.com/alpine/latest-stable/main/" > /etc/apk/repositories
RUN echo "https://mirrors.aliyun.com/alpine/latest-stable/community/" >> /etc/apk/repositories
# 自定义环境变量
ENV TZ=Asia/Shanghai
# 设置本地系统时间和时区
RUN ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime && echo ${TZ} > /etc/timezone
# 拷贝打包资源
ADD nginx.conf /etc/nginx/nginx.conf
ADD unpackage/dist/build/h5/ /usr/share/nginx/html
# 挂在启动点
ENTRYPOINT ["nginx-debug", "-g", "daemon off;"]
Vue 项目 Dockerfile 示例:
FROM nginx:stable-alpine
# 镜像源加速
RUN echo "https://mirrors.aliyun.com/alpine/latest-stable/main/" > /etc/apk/repositories
RUN echo "https://mirrors.aliyun.com/alpine/latest-stable/community/" >> /etc/apk/repositories
# 自定义环境变量
ENV TZ=Asia/Shanghai
# 设置本地系统时间和时区
RUN ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime && echo ${TZ} > /etc/timezone
# 拷贝打包资源
ADD nginx.conf /etc/nginx/nginx.conf
ADD dist/ /usr/share/nginx/html
# 挂在启动点
ENTRYPOINT ["nginx-debug", "-g", "daemon off;"]
基于 Debian 镜像
基础镜像:
FROM debian:stretch
# 自定义环境变量
ENV TZ=Asia/Shanghai
基于 Ubuntu 镜像
写成 Dockerfile 如下:
FROM ubuntu:bionic
ENV TZ=Asia/Shanghai
RUN echo "${TZ}" > /etc/timezone \
&& ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime \
&& apt update \
&& apt install -y tzdata \
&& rm -rf /var/lib/apt/lists/*
作者:Jeebiz 创建时间:2023-02-12 01:49
最后编辑:Jeebiz 更新时间:2024-11-27 12:52
最后编辑:Jeebiz 更新时间:2024-11-27 12:52