{"record":{"id":"68e53702032aa933","repo":"testcontainers/testcontainers-java","slug":"requested-port-originalport-is-not-mapped","errorCode":null,"errorMessage":"Requested port (${originalPort}) is not mapped","messagePattern":"Requested port \\((.+?)\\) is not mapped","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/testcontainers/containers/ContainerState.java","lineNumber":175,"sourceCode":"     * @see #getContainerInfo()\n     * @see #getCurrentContainerInfo()\n     */\n    default Integer getMappedPort(int originalPort) {\n        Preconditions.checkState(\n            this.getContainerId() != null,\n            \"Mapped port can only be obtained after the container is started\"\n        );\n\n        Ports.Binding[] binding = new Ports.Binding[0];\n        final InspectContainerResponse containerInfo = this.getContainerInfo();\n        if (containerInfo != null) {\n            binding = containerInfo.getNetworkSettings().getPorts().getBindings().get(new ExposedPort(originalPort));\n        }\n\n        if (binding != null && binding.length > 0 && binding[0] != null) {\n            return Integer.valueOf(binding[0].getHostPortSpec());\n        } else {\n            throw new IllegalArgumentException(\"Requested port (\" + originalPort + \") is not mapped\");\n        }\n    }\n\n    /**\n     * @return the exposed ports\n     */\n    List<Integer> getExposedPorts();\n\n    /**\n     * @return the port bindings\n     */\n    default List<String> getPortBindings() {\n        List<String> portBindings = new ArrayList<>();\n        final Ports hostPortBindings = this.getContainerInfo().getHostConfig().getPortBindings();\n        for (Map.Entry<ExposedPort, Ports.Binding[]> binding : hostPortBindings.getBindings().entrySet()) {\n            for (Ports.Binding portBinding : binding.getValue()) {\n                portBindings.add(String.format(\"%s:%s\", portBinding.toString(), binding.getKey()));\n            }","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/testcontainers/testcontainers-java/blob/8e549514e3f01c57d70546fbb8599d138f3903e5/core/src/main/java/org/testcontainers/containers/ContainerState.java#L157-L193","documentation":"ContainerState.getMappedPort resolves an exposed container port to its host-mapped port from the Docker port bindings. If no binding exists for the requested container port it throws IllegalArgumentException 'Requested port (N) is not mapped'.","triggerScenarios":"Calling getMappedPort(n) where n was never exposed (no withExposedPorts(n)/ExposedPort in the image), before the container started, or for a container using a non-bridge network without published ports.","commonSituations":"Requesting a port not declared in the image's EXPOSE and not added via withExposedPorts; forgetting addExposedPorts; calling before start(); custom network mode without -p publishing.","solutions":["Expose the port via withExposedPorts(n)/addExposedPorts(n) or ensure the image has EXPOSE and the container publishes it","Call getMappedPort only after container.start() completed","Verify you requested the container-side port, not the host port","For compose containers use getServicePort, which handles service/instance naming"],"exampleFix":"// before\nGenericContainer c = new GenericContainer<>(\"redis:7\").withExposedPorts(6379);\nc.start();\nInteger host = c.getMappedPort(6380); // wrong port\n// after\nInteger host = c.getMappedPort(6379);","handlingStrategy":"validation","validationCode":"if (!container.getExposedPorts().contains(containerPort)) throw new IllegalArgumentException(\"Port \" + containerPort + \" not exposed; use withExposedPorts first\");","typeGuard":null,"tryCatchPattern":"try { return container.getMappedPort(port); } catch (IllegalArgumentException e) { throw new IllegalStateException(\"Port \" + port + \" is not published on \" + container.getContainerInfo().getConfig().getImage(), e); }","preventionTips":["Declare all needed ports in withExposedPorts before start()","Only call getMappedPort after start() returns","Request container-side ports (e.g. 5432), not host ports"],"tags":["port-mapping","docker","exposed-ports","container"],"backgroundTag":"invalid-argument-value","analyzedSha":"8e549514e3f01c57d70546fbb8599d138f3903e5","analyzedAt":"2026-09-12T14:56:41.227Z","contentChangedAt":"2026-09-12T14:56:41.227Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}