Optimized Docker Base Image Construction for Common Runtimes
Java Runtime Configurations
Containerizing Java applications requires careful selection of base images based on architecture compatibility, memory footprint, and font requirements. The folloiwng templates demonstrate streamilned setups for Alpine, Debian, and specialized JRE distributions.
Alpine-Based Variant
FROM eclipse-temurin:17-jre-alpine
ENV TZ=Asia/Shanghai
ENV LANG=en_US.UTF-8
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
&& apk upgrade --no-cache \
&& apk add --no-cache bash curl unzip procps tzdata fontconfig \
&& cp /usr/share/zoneinfo/${TZ} /etc/localtime \
&& echo ${TZ} > /etc/timezone \
&& apk del tzdata
ADD fonts/NotoSans-Regular.ttf /usr/share/fonts/custom/
RUN fc-cache -f
BellSoft Liberica Variant
FROM bellsoft/liberica-server-container:17-cds
ENV TZ=Asia/Shanghai
ENV LANG=en_US.UTF-8
RUN apt-get update && apt-get install -y --no-install-recommends tzdata curl unzip \
&& ln -fs /usr/share/zoneinfo/${TZ} /etc/localtime \
&& dpkg-reconfigure -f noninteractive tzdata \
&& rm -rf /var/lib/apt/lists/*
COPY fonts/ /usr/local/share/fonts/custom/
Debian Bullseye Compatibility Variant
FROM openjdk:17-bullseye-slim
ENV TZ=Asia/Shanghai
ENV LANG=zh_CN.UTF-8
ENV LC_ALL=zh_CN.UTF-8
RUN sed -i 's|deb.debian.org/debian|mirrors.tuna.tsinghua.edu.cn/debian|g' /etc/apt/sources.list \
&& apt-get update \
&& apt-get install -y --no-install-recommends locales fontconfig procps unzip curl bash tzdata libgomp1 \
&& sed -i 's/# zh_CN.UTF-8/zh_CN.UTF-8/' /etc/locale.gen \
&& locale-gen \
&& ln -fs /usr/share/zoneinfo/${TZ} /etc/localtime \
&& dpkg-reconfigure -f noninteractive tzdata \
&& apt-get install -y fonts-wqy-zenhei \
&& cd /usr/share/fonts/truetype/ && mkfontscale && mkfontdir && fc-cache -v \
&& rm -rf /var/lib/apt/lists/*
COPY fonts/ /usr/share/fonts/truetype/custom/
Mounting Host Timezone and Fonts
docker run -d \
--name app-container \
-e TZ=Asia/Shanghai \
-v /etc/localtime:/etc/localtime:ro \
-v /host/data/fonts:/usr/share/fonts:ro \
registry.example.com/my-app:latest
Frontend and Nginx Deployment
For single-page applications, Nginx serves as an efficient reverse proxy and static file server. The configuration below optimizes compression, routing, and backend proxying.
server {
listen 80;
server_name localhost;
client_max_body_size 150M;
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml;
gzip_disable "MSIE [1-6]\.";
location / {
root /usr/share/nginx/html;
index index.html;
try_files $uri $uri/ /index.html;
add_header Cache-Control "no-cache, no-store, must-revalidate";
}
location /api/v1/ {
proxy_pass http://backend-cluster;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
client_body_timeout 300s;
}
location /files/ {
proxy_pass http://backend-cluster/files/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
FROM nginx:1.25-alpine
RUN rm -f /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY dist/ /usr/share/nginx/html/
RUN ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone
EXPOSE 80
HEALTHCHECK --interval=30s --timeout=3s CMD wget -q --spider http://localhost:80/ || exit 1
MySQL 8.0 Customization
Database images require precise configuration for character encoding, connection pooling, and binary logging. The following setup ensures proper initialization and persistence.
[mysqld]
pid-file=/var/run/mysqld/mysqld.pid
socket=/var/run/mysqld/mysqld.sock
datadir=/var/lib/mysql
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
max_connections=3000
max_allowed_packet=64M
lower_case_table_names=1
default_time_zone='+08:00'
transaction_isolation=READ-COMMITTED
binlog_expire_logs_seconds=2592000
binlog_format=ROW
log_bin=/var/lib/mysql/mysql-bin
server_id=100
symbolic-links=0
FROM mysql:8.0-debian
COPY mysql-custom.cnf /etc/mysql/conf.d/
COPY init-scripts/ /docker-entrypoint-initdb.d/
RUN chmod 444 /etc/mysql/conf.d/mysql-custom.cnf
CREATE DATABASE IF NOT EXISTS application_db DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE application_db;
CREATE TABLE IF NOT EXISTS users (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(100) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);