Can';t使用testcontainer通过ssh连接到docker
我的任务通过ssh从测试连接到容器 我有一个文件:(几乎来自) 然后,我通过testcontainer使用此dockerfile:Can';t使用testcontainer通过ssh连接到docker,docker,ssh,testcontainers,Docker,Ssh,Testcontainers,我的任务通过ssh从测试连接到容器 我有一个文件:(几乎来自) 然后,我通过testcontainer使用此dockerfile: public static GenericContainer localServer(@NotNull Integer port, @NotNull String user, @NotNull String password, Path dockerfile) { return new GenericContainer( new ImageFro
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:连接到主机本地主机端口32836:连接被拒绝
我错过了什么细节?谢谢 .withExposedPorts(port)
-您似乎试图将端口外部化,而映像定义了一个静态端口“22”
请改用
。withExposedPorts(22)
。谢谢,但“port”实际上是常量=“22”。然后确保您的映像实际上监听所有接口,而不仅仅是“localhost”。你试过用docker run-p 22 my image运行它吗?是的,我也可以只使用终端运行docker:dockerfile docker build-t image\u name.
docker run-d-P--name container\u name image\u namedocker port test\u sshd 22
sshroot@localhost-p 32768
它可以正常工作,以防我的操作系统是macOSIn在您的docker运行
示例中,您没有映射/home/
文件夹,但我在您提供的示例代码中看到了这一点。请你再确认一下这不是由它引起的好吗?
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 root@localhost -p 49154