{"record":{"id":"d6ee00e378afec24","repo":"testcontainers/testcontainers-java","slug":"could-not-get-a-port-for-servicename-testcontainers-does-not","errorCode":null,"errorMessage":"Could not get a port for '${serviceName}'. Testcontainers does not have an exposed port configured for '${serviceName}'. To fix, please ensure that the service '${serviceName}' has ports exposed using .withExposedService(...)","messagePattern":"Could not get a port for '(.+?)'\\. Testcontainers does not have an exposed port configured for '(.+?)'\\. To fix, please ensure that the service '(.+?)' has ports exposed using \\.withExposedService\\(\\.\\.\\.\\)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/testcontainers/containers/ComposeDelegate.java","lineNumber":348,"sourceCode":"        );\n        waitAllStrategy.withStrategy(waitStrategy);\n    }\n\n    /**\n     * Get the port that an exposed service can be found at, from the host machine\n     * (i.e. should be the machine that's running this Java process).\n     * <p>\n     * The service must have been declared using DockerComposeContainer#withExposedService.\n     *\n     * @param serviceName the name of the service as set in the docker-compose.yml file.\n     * @param servicePort the port exposed by the service container.\n     * @return a port that can be used for accessing the service container.\n     */\n    public Integer getServicePort(String serviceName, Integer servicePort) {\n        Map<Integer, Integer> portMap = this.ambassadorPortMappings.get(getServiceInstanceName(serviceName));\n\n        if (portMap == null) {\n            throw new IllegalArgumentException(\n                \"Could not get a port for '\" +\n                serviceName +\n                \"'. \" +\n                \"Testcontainers does not have an exposed port configured for '\" +\n                serviceName +\n                \"'. \" +\n                \"To fix, please ensure that the service '\" +\n                serviceName +\n                \"' has ports exposed using .withExposedService(...)\"\n            );\n        } else {\n            return ambassadorContainer.getMappedPort(portMap.get(servicePort));\n        }\n    }\n\n    Optional<ContainerState> getContainerByServiceName(String serviceName) {\n        String serviceInstantName = getServiceInstanceName(serviceName);\n        return Optional.ofNullable(serviceInstanceMap.get(serviceInstantName));","sourceCodeStart":330,"sourceCodeEnd":366,"githubUrl":"https://github.com/testcontainers/testcontainers-java/blob/8e549514e3f01c57d70546fbb8599d138f3903e5/core/src/main/java/org/testcontainers/containers/ComposeDelegate.java#L330-L366","documentation":"ComposeDelegate.getServicePort looks up the ambassador/port mapping registered for a compose service. If no port mapping exists for that service it throws IllegalArgumentException explaining that no exposed port was configured for the service.","triggerScenarios":"Calling getServicePort(serviceName, port) on a DockerComposeContainer for a service that was never registered via withExposedService, or with a wrong service name, before the mapping exists.","commonSituations":"Querying a service you forgot to expose with withExposedService; misspelled service name; calling getServicePort for a service started outside the compose container; reading the port before startup finished.","solutions":["Register the service with .withExposedService(serviceName, port) on the DockerComposeContainer before getServicePort","Ensure the serviceName passed to getServicePort matches the one used in withExposedService","Ensure the compose container is started (ports map is populated during start)","Alternatively read the port from the service's wait strategy / container directly"],"exampleFix":"// before\nInteger port = compose.getServicePort(\"redis\", 6379); // never exposed\n// after\nDockerComposeContainer compose = new DockerComposeContainer(new File(\"docker-compose.yml\"))\n    .withExposedService(\"redis_1\", 6379, Wait.defaultWaitStrategy());\ncompose.start();\nInteger port = compose.getServicePort(\"redis_1\", 6379);","handlingStrategy":"validation","validationCode":"if (!exposedServices.containsKey(serviceName)) throw new IllegalStateException(\"Call withExposedService for \" + serviceName + \" before getServicePort\");","typeGuard":null,"tryCatchPattern":"try { return compose.getServicePort(serviceName, port); } catch (IllegalArgumentException e) { throw new IllegalStateException(\"Service not exposed on compose container: \" + serviceName, e); }","preventionTips":["Always pair withExposedService with the corresponding getServicePort call","Use the same identifier string for expose and lookup","Only call getServicePort after start()"],"tags":["docker-compose","port-mapping","exposed-service","configuration"],"backgroundTag":"resource-not-found","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"}