简体   繁体   中英

How to bind forwarding port from testcontainers for atmoz/sftp?


I read articles https://github.com/atmoz/sftp , https://www.testcontainers.org/quickstart/junit_4_quickstart/ , https://www.testcontainers.org/quickstart/junit_4_quickstart/ And didn't find relevant info.
I'm trying to test my sftp loader in java. I use Junit 4 and java 11
when I launch image from console I've got this

ilys@ilys-AORUS-5-SE:~/Programming/work$ docker run     -v /tmp/upload:/home/foo/upload     -p 2222:22 -d atmoz/sftp     foo:bar:1001
ilys@ilys-AORUS-5-SE:~/Programming/work$ docker ps
CONTAINER ID   IMAGE        COMMAND                  CREATED        STATUS        PORTS                                   NAMES
0e04d26325ac   atmoz/sftp   "/entrypoint foo:bar…"   1 second ago   Up 1 second   0.0.0.0:2222->22/tcp, :::2222->22/tcp   hopeful_banach

And successfully connect to docker by port 2222

But when i launch from testcontainer whith next code:

        sftp = new GenericContainer("atmoz/sftp:latest")
                .withExposedPorts(2222)
                .withFileSystemBind(
                        "/tmp/upload",
                        "/home/foo/upload",
                        BindMode.READ_WRITE)
                .withCommand("foo:bar:1001:::upload");

I've got next:

ilys@ilys-AORUS-5-SE:~/Programming/work$ docker ps
CONTAINER ID   IMAGE                       COMMAND                  CREATED          STATUS          PORTS                                                 NAMES
b25f9f3b7255   atmoz/sftp:latest           "/entrypoint foo:bar…"   16 seconds ago   Up 16 seconds   22/tcp, 0.0.0.0:49257->2222/tcp, :::49255->2222/tcp   friendly_clarke
9238e8dade0c   testcontainers/ryuk:0.3.4   "/app"                   17 seconds ago   Up 17 seconds   0.0.0.0:49256->8080/tcp, :::49254->8080/tcp           testcontainers-ryuk-f71b562e-c37b-4253-9e5e-f7d308ddfaed

and exception

org.testcontainers.containers.ContainerLaunchException: Container startup failed
    at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:349)
    at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:322)

I tried launch testcontainer with sftp.setPortBindings(Collections.singletonList("2222"));and got similar result.

Is there a way to launch atmoz/sfpt with port forwarding 2222->22 like from console?

Testcontainers provides an example for SFTP. Take a look here

Testcontainers will map port 22 to a random port which as you can see in the example provided you can get by using sftp.getMappedPort(22))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM