定制docker ubuntu镜像

定制化 ubuntu:xenial 镜像,将包仓库替换为阿里云、系统语言设置为中文、系统时区设置为东八区。

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
FROM ubuntu:xenial

MAINTAINER Leon.Gan

ENV TZ=Asia/Shanghai

RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak \
&& echo "deb http://mirrors.aliyun.com/ubuntu/ xenial main" >/etc/apt/sources.list \
&& echo "deb-src http://mirrors.aliyun.com/ubuntu/ xenial main" >>/etc/apt/sources.list \
&& echo "deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main" >>/etc/apt/sources.list \
&& echo "deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main" >>/etc/apt/sources.list \
&& echo "deb http://mirrors.aliyun.com/ubuntu/ xenial universe" >>/etc/apt/sources.list \
&& echo "deb-src http://mirrors.aliyun.com/ubuntu/ xenial universe" >>/etc/apt/sources.list \
&& echo "deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe" >>/etc/apt/sources.list \
&& echo "deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates universe" >>/etc/apt/sources.list \
&& echo "deb http://mirrors.aliyun.com/ubuntu/ xenial-security main" >>/etc/apt/sources.list \
&& echo "deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main" >>/etc/apt/sources.list \
&& echo "deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe" >>/etc/apt/sources.list \
&& echo "deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security universe" >>/etc/apt/sources.list \
&& apt-get update \
&& apt-get install -y locales \
&& locale-gen zh_CN.UTF-8 \
&& apt-get install -y tzdata \
&& ln -s -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& rm -rf /var/lib/apt/lists/*

ENV LC_ALL "zh_CN.UTF-8"
ENV LANG "zh_CN.UTF-8"

CMD ["/bin/bash"]

最开始我使用如下命令调整时区:

1
2
3
apt-get install -y tzdata \
&& echo Asia/Shanghai > /etc/timezone \
&& dpkg-reconfigure -f noninteractive tzdata

但是多次构建镜像都发现时区并没有修改成功,具体原因并没有查到,后来更改为设置环境变量和软连接来解决(点此查看问题详情):

1
2
ENV TZ=Asia/Shanghai
ln -s -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

镜像构建命令:

1
docker build -t ubuntu:xenial-cn .