docker构建ubuntu各版本容器及解决中文乱码

docker构建ubuntu各版本容器及解决中文乱码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
set -e  # 出错立即停止

versions="16.04 18.04 20.04 22.04 24.04"
# versions="16.04"

for version in $versions; do
  echo "构建 ubuntu_${version}..."
  
  docker build \
    --build-arg UBUNTU_VERSION=$version \
    --build-arg DEBIAN_FRONTEND=noninteractive \
    -t ubuntu_${version} \
    -f - . <<'EOF'
ARG UBUNTU_VERSION
FROM ubuntu:${UBUNTU_VERSION}

ARG DEBIAN_FRONTEND=noninteractive

# 使用清华源加速
RUN sed -i 's/archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
    sed -i 's/security.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
    apt-get update && apt-get install -y \
    git emacs vim less curl wget iputils-ping tree findutils locales software-properties-common && \
    rm -rf /var/lib/apt/lists/*

# 配置 UTF-8 locale(修复中文乱码)
RUN locale-gen en_US.UTF-8 && \
    update-locale LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 && \
    echo 'LANG="en_US.UTF-8"' > /etc/default/locale && \
    echo 'LC_ALL="en_US.UTF-8"' >> /etc/default/locale

# 设置环境变量(运行时生效)
ENV LANG=en_US.UTF-8 \
    LC_ALL=en_US.UTF-8 \
    LANGUAGE=en_US:en \
    LESSCHARSET=utf-8

WORKDIR /root
EOF
  
  echo "ubuntu_${version} 构建完成"
done

# 清理悬空镜像
docker system prune -f

echo "所有镜像构建完成!"
docker images | grep ubuntu_

如果要一键删除多个容器命名含ubuntu的镜像

1
docker rmi $(docker images | grep ubuntu_.* | awk '{print $2}')

各版本运行

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# ubuntu 16.04
# 运行
mkdir -p /opt/test; \
  cd /opt/test; \
  docker run -it --network host --mount type=bind,src=./,dst=/root ubuntu_16.04;

# ubuntu 18.04
# 运行
mkdir -p /opt/test; \
  cd /opt/test;
  docker run -it --network host --mount type=bind,src=./,dst=/root ubuntu_18.04

# ubuntu 20.04
# 运行
mkdir -p /opt/test; \
  cd /opt/test;
  docker run -it --network host --mount type=bind,src=./,dst=/root ubuntu_20.04

# ubuntu 22.04
# 运行
mkdir -p /opt/test; \
  cd /opt/test;
  docker run -it --network host --mount type=bind,src=./,dst=/root ubuntu_22.04

# ubuntu 24.04
# 运行
mkdir -p /opt/test; \
  cd /opt/test;
  docker run -it --network host --mount type=bind,src=./,dst=/root ubuntu_24.04
comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计