How can I set the port for Postgresql when using Testcontainers?
Asked Answered
A

1

11

Sometimes I need to install a port for Postgresql, which I run for tests in a container. But the Test container the developer command ofTestcontainers removed this feature. But somewhere there is a workaround solution, through the settings, but I can't find it. Who has any ideas or information on how to do this ?

public class ContainerConfig {

    private static final PostgreSQLContainer postgresSQLContainer;

    static {
        DockerImageName postgres = DockerImageName.parse("postgres:13.1");

        postgresSQLContainer = (PostgreSQLContainer) new PostgreSQLContainer(postgres)
                .withDatabaseName("test")
                .withUsername("root")
                .withPassword("root")
                .withReuse(true);

        postgresSQLContainer.start();
    }

    @SuppressWarnings("rawtypes")
    private static PostgreSQLContainer getPostgresSQLContainer() {
        return postgresSQLContainer;
    }


    @SuppressWarnings("unused")
    @DynamicPropertySource
   public static void registerPgProperties(DynamicPropertyRegistry propertyRegistry) {

        propertyRegistry.add("integration-tests-db", getPostgresSQLContainer()::getDatabaseName);
        propertyRegistry.add("spring.datasource.username", getPostgresSQLContainer()::getUsername);
        propertyRegistry.add("spring.datasource.password", getPostgresSQLContainer()::getPassword);
        propertyRegistry.add("spring.datasource.url",  getPostgresSQLContainer()::getJdbcUrl);
    }

}

Asleyaslope answered 10/9, 2021 at 13:6 Comment(0)
C
11

Add port binding with withCreateContainerCmdModifier.

static {
    int containerPort = 5432 ;
    int localPort = 5432 ;
    DockerImageName postgres = DockerImageName.parse("postgres:13.1");
    postgreDBContainer = new PostgreSQLContainer<>(postgres)
            .withDatabaseName("test")
            .withUsername("root")
            .withPassword("root")
            .withReuse(true)
            .withExposedPorts(containerPort)
            .withCreateContainerCmdModifier(cmd -> cmd.withHostConfig(
                    new HostConfig().withPortBindings(new PortBinding(Ports.Binding.bindPort(localPort), new ExposedPort(containerPort)))
            ));
    postgreDBContainer.start();
}
Chartography answered 11/9, 2021 at 17:49 Comment(3)
I have gotten an error - If I set port 5437 or another (for example, 32700) : java.lang.IllegalArgumentException: Requested port (5432) is not mapped. If I set port 5432 , an error message comes out that: (48165926feea259777b85edc2a6e9567516841a4121ae924a6836e46d816ffb0): Bind for 0.0.0.0:5432 failed: port is already allocated"}Asleyaslope
I have updated the answer. The container port should be 5432 for postgresql. If changing local port shows error message 'port already allocated', check you may be running another container on the same port. You can view it by docker ps and stop it by docker stop [CONTAINER ID].Chartography
that's the thing, I didn't want to be tied to port 5432 . I would like to allocate the port myself. what I think is necessary at the momentAsleyaslope

© 2022 - 2024 — McMap. All rights reserved.