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
|
# 写入如下Dockerfile:
cat - > Dockerfile <<'EOF'
FROM ubuntu:18.04
# MAINTAINER This is a test container
RUN apt update && apt install -y openssh-server net-tools passwd
# RUN echo '123456' | passwd --stdin root
RUN echo 'root:123456' | chpasswd
# 启用密码认证
RUN sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config
RUN sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config
# 禁用严格模式
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
EXPOSE 22
RUN mkdir -p /run/sshd
CMD ["/usr/sbin/sshd","-D"]
EOF
# 执行命令生成镜像
docker build -t sshd:new .
# 清理构建缓存
docker system prune -f
# 如果要停止指定容器
docker stop 容器ID
# 如果停止所有正在运行的容器
docker ps | sed '/ID/d' | awk '{print $1}' | xargs docker stop
# 启动容器并修改root密码
docker run -d -P sshd:new
# 查看运行容器信息,可以看到映射端口
docker ps
# 从本地使用SSH进行登陆
ssh root@localhost -p 32769 # 32769是映射的端口号,每次运行映射的端口都是随机的
|