我的任务通过测试中的 ssh 连接到容器。
我有 dokefile:(几乎来自https://docs.docker.com/engine/examples/running_ssh_service/)
FROM ubuntu:18.04
RUN apt-get update
RUN apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN echo 'root:root' |chpasswd
RUN sed -i 's/^#?PermitRootLogin\s+.*/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -i 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
RUN mkdir /root/.ssh
RUN apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
然后我通过 testcontainer 使用这个 dockerfile:
public static GenericContainer localServer(@NotNull Integer port, @NotNull String user, @NotNull String password, Path dockerfile) {
return new GenericContainer(
new ImageFromDockerfile()
.withFileFromPath("Dockerfile", dockerfile))
.withExposedPorts(port)
.withFileSystemBind(FileUtil.getTempDirectory(), "/home/", BindMode.READ_WRITE);
当我运行测试时,容器成功运行,但我无法通过 ssh 连接。49154 - 值来自sftp.getMappedPort(22)
ssh root@localhost -p 49154
得到:ssh:连接到主机 localhost 端口 32836:连接被拒绝
我错过了什么细节?谢谢!