在 Ubuntu 镜像基础上配置常用的软件以用作通用镜像
通用镜像制作
本文介绍如何在 Ubuntu 镜像基础上配置常用的软件,以用作通用镜像。
带有用户设置的镜像
使用 Dockerfile 快速构建通用镜像:
❕注意替换 YOUR_PUBLIC_KEY❕
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
FROM ubuntu:24.04
# 设置镜像源,安装必要软件,设置 sshd_config,设置时区
RUN sed -i "s@//.*archive.ubuntu.com@//mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list.d/ubuntu.sources \
&& sed -i "s/security.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g" /etc/apt/sources.list.d/ubuntu.sources \
&& apt-get update \
&& apt-get install -y vim git curl wget zsh net-tools openssh-server sudo ca-certificates \
&& apt-get upgrade -y \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
RUN echo "" >> /etc/ssh/sshd_config \
&& echo "# Added by automated configuration" >> /etc/ssh/sshd_config \
&& echo "PermitRootLogin yes" >> /etc/ssh/sshd_config \
&& echo "PubkeyAuthentication yes" >> /etc/ssh/sshd_config \
&& echo "PasswordAuthentication no" >> /etc/ssh/sshd_config \
&& echo "PermitEmptyPasswords no" >> /etc/ssh/sshd_config \
&& echo "GatewayPorts clientspecified" >> /etc/ssh/sshd_config \
&& echo "X11Forwarding no" >> /etc/ssh/sshd_config \
&& service ssh restart \
&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \
&& sed -i 's/http:/https:/g' /etc/apt/sources.list.d/ubuntu.sources
# 安装 Miniconda(使用清华源)
RUN curl -O https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh \
&& bash /Miniconda3-latest-Linux-x86_64.sh -b -p /opt/conda \
&& rm /Miniconda3-latest-Linux-x86_64.sh \
&& /opt/conda/bin/conda init zsh \
&& /opt/conda/bin/conda config --set show_channel_urls yes \
&& /opt/conda/bin/conda clean -afy \
&& /opt/conda/bin/conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main \
&& /opt/conda/bin/conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r \
&& /opt/conda/bin/conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2 \
&& /opt/conda/bin/conda config --set show_channel_urls yes
# 配置用户,可按照当前系统内已有的用户设置 GID:UID
RUN groupadd -g 1019 user \
&& useradd -m -u 1019 -g 1019 -s /usr/bin/zsh user \
&& usermod -aG sudo user \
&& echo "user ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers \
&& chown -R user:user /home/user \
&& chmod 755 /home/user
WORKDIR /home/user
USER user
ENV HOME=/home/user
ENV USER=user
RUN git clone https://mirrors.tuna.tsinghua.edu.cn/git/ohmyzsh.git /home/user/.oh-my-zsh --depth=1 \
&& cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc \
&& sed -i "s/^ZSH_THEME=.*/ZSH_THEME=\"robbyrussell\"/" ~/.zshrc \
&& sed -i "s/^# DISABLE_AUTO_UPDATE=/DISABLE_AUTO_UPDATE=true/" ~/.zshrc
RUN mkdir -p /home/user/.ssh \
&& chmod 700 /home/user/.ssh \
&& echo "ssh-ed25519 YOUR_PUBLIC_KEY" > /home/user/.ssh/authorized_keys \
&& chmod 600 /home/user/.ssh/authorized_keys \
&& /opt/conda/bin/conda init zsh
SHELL ["/usr/bin/zsh", "-c"]
CMD ["/usr/bin/zsh"]
|
Root 用户镜像
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
|
FROM ubuntu:24.04
# 设置镜像源,安装必要软件,设置 sshd_config,设置时区
RUN sed -i "s@//.*archive.ubuntu.com@//mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list.d/ubuntu.sources \
&& sed -i "s/security.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g" /etc/apt/sources.list.d/ubuntu.sources \
&& apt-get update \
&& apt-get install -y vim git curl wget zsh net-tools openssh-server sudo ca-certificates \
&& apt-get upgrade -y \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
RUN echo "" >> /etc/ssh/sshd_config \
&& echo "# Added by automated configuration" >> /etc/ssh/sshd_config \
&& echo "PermitRootLogin yes" >> /etc/ssh/sshd_config \
&& echo "PubkeyAuthentication yes" >> /etc/ssh/sshd_config \
&& echo "PasswordAuthentication no" >> /etc/ssh/sshd_config \
&& echo "PermitEmptyPasswords no" >> /etc/ssh/sshd_config \
&& echo "GatewayPorts clientspecified" >> /etc/ssh/sshd_config \
&& echo "X11Forwarding no" >> /etc/ssh/sshd_config \
&& service ssh restart \
&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \
&& sed -i 's/http:/https:/g' /etc/apt/sources.list.d/ubuntu.sources
# 安装 Miniconda(使用清华源)
RUN curl -O https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh \
&& bash /Miniconda3-latest-Linux-x86_64.sh -b -p /opt/conda \
&& rm /Miniconda3-latest-Linux-x86_64.sh \
&& /opt/conda/bin/conda init zsh \
&& /opt/conda/bin/conda config --set show_channel_urls yes \
&& /opt/conda/bin/conda clean -afy \
&& /opt/conda/bin/conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main \
&& /opt/conda/bin/conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r \
&& /opt/conda/bin/conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2 \
&& /opt/conda/bin/conda config --set show_channel_urls yes
RUN git clone https://mirrors.tuna.tsinghua.edu.cn/git/ohmyzsh.git /root/.oh-my-zsh --depth=1 \
&& cp /root/.oh-my-zsh/templates/zshrc.zsh-template /root/.zshrc \
&& sed -i "s/^ZSH_THEME=.*/ZSH_THEME=\"robbyrussell\"/" /root/.zshrc \
&& sed -i "s/^# DISABLE_AUTO_UPDATE=/DISABLE_AUTO_UPDATE=true/" /root/.zshrc
RUN mkdir -p /root/.ssh \
&& chmod 700 /root/.ssh \
&& echo "ssh-ed25519 YOUR_PUBLIC_KEY" > /root/.ssh/authorized_keys \
&& chmod 600 /root/.ssh/authorized_keys
SHELL ["/usr/bin/zsh", "-c"]
CMD ["/usr/bin/zsh"]
|