@@ -18,17 +18,34 @@ RUN apt-get update && \
1818RUN mkdir -p /var/run/sshd
1919
2020# 设置 root 用户密码 (使用你配置的密码)
21- RUN echo 'root:rt.1qwe2iop' | chpasswd
21+ # RUN echo 'root:rt.1qwe2iop' | chpasswd
2222
2323# 修改 SSH 配置文件,允许 root 登录和密码认证
24- RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config && \
25- sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config
24+ # RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config && \
25+ # sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config
26+
27+ ARG YOUR_NAME=allgoo
28+
29+ # 接收 compose 传来的参数,默认值为 1000
30+ ARG HOST_UID=1000
31+ ARG HOST_GID=1000
32+
33+ # 创建一个和宿主机 UID/GID 一致的用户, 如果有默认用户占用了该 ID,先将其删除,然后再创建我们的目标用户
34+ RUN userdel -r $(getent passwd ${HOST_UID} | cut -d: -f1) 2>/dev/null || true && \
35+ groupdel $(getent group ${HOST_GID} | cut -d: -f1) 2>/dev/null || true && \
36+ groupadd -g ${HOST_GID} ${YOUR_NAME} && \
37+ useradd -u ${HOST_UID} -g ${YOUR_NAME} -m ${YOUR_NAME} -s /bin/bash
38+
39+ # 配置用户 .ssh
40+ RUN mkdir -p /home/${YOUR_NAME}/.ssh && \
41+ chown -R ${HOST_UID}:${HOST_GID} /home/${YOUR_NAME}/.ssh && \
42+ chmod 700 /home/${YOUR_NAME}/.ssh
2643
2744# 设置工作目录
28- WORKDIR /workspace
45+ WORKDIR /home/${YOUR_NAME}/ workspace
2946
3047# 暴露 22 端口
3148EXPOSE 22
3249
3350# 启动 sshd 服务,并以前台模式 (-D) 运行,这能保持容器一直处于运行状态
34- CMD ["/usr/sbin/sshd" , "-D" ]
51+ CMD ["/usr/sbin/sshd" , "-D" ]
0 commit comments