Skip to content

Automatically map exposed ports defined in Dockerfile #1448

@aguibert

Description

@aguibert

Typically a Docker image will declare what ports it exposes using:
EXPOSE <ports...>

Why is it also necessary to duplicate this information by default for GenericContainer?

Take the quick start example:

@Rule
public GenericContainer redis = new GenericContainer<>("redis:5.0.3-alpine")
                                        .withExposedPorts(6379);

If the user does not explicitly state what ports to expose via withExposedPorts(), could we run DockerClientFactory.instance().client().inspectImageCmd(this.getDockerImageName()).exec(); to run a Docker image inspection and automatically read/set the ports exposed by the Docker image? I know there are situations where a user may not want to expose all (or any) ports of a container, but I think this would be the minority case for Testcontainers users, right?

If we were to do this, the above code example could simply be:

@Rule
public GenericContainer redis = new GenericContainer<>("redis:5.0.3-alpine");

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions