Docker Compose: service without external port
In Docker Compose, a service without an external port runs entirely inside the defined network, accessible only to other containers or through internal resolution. This pattern is common for databases, caches, and auxiliary tools that support a primary application but don’t need inbound traffic from the host or internet. Omitting the ports directive reduces attack surface and avoids accidental exposure.
Why need a service without a port
Services declared without a ports: block bind only to the container’s internal networking. This is the default behavior for any Compose service that doesn’t expose a host port. In a self-hosted stack, keeping services internal-only simplifies firewall rules, avoids port collisions across stacks, and follows the principle of least privilege. When a database or cache has no published port, it can only be reached by other containers on the same user-defined network, which is the intended architecture for multi-container applications.
Network scheme and compose file
A typical internal-only service uses a custom bridge network declared at the top level, with the service referencing it via networks:. The internal: true flag on a network entry tells Docker to deny external access from outside the Compose project. Below is a minimal representation of the compose configuration using a markdown table:
| Key | Value | Effect |
|---|---|---|
networks | custom name / default | Defines the network the service joins |
internal: true | true | Blocks external container access to the network |
aliases | hostname | Provides stable service discovery names within the network |
Always pair a custom network with internal: true if the service must not be reachable from the host or other unrelated projects.
The equivalent docker-compose.yml fragment conceptually maps to:
- a top‑level
networks.internalblock withdriver: bridgeandinternal: true - each service listing
networks: [internal]without anyports:entry
Checking functionality
Verify internal‑only reachability from another service container. From a client container started in the same project, resolve the service by name or alias and confirm connectivity:
From the host, attempt a connection to confirm it is not listening on a published port; the command should time out or refuse:
Inspect the network’s internal flag directly:
The JSON output must return "Internal": true. Additionally, docker compose ps should list the service as running, and docker compose logs should show clean startup without bind‑error messages about address already in use.
Typical error and how to fix it
A frequent mistake is adding an accidental ports: - "5432:5432" line to a service that should remain internal. This immediately exposes the port on 0.0.0.0:5432 on the host, contradicting the internal‑only design and potentially causing port conflicts. To restore internal‑only behavior, remove the ports section entirely and ensure the network definition includes internal: true. If the service must be accessible only to specific containers, consider using user‑defined networks without the flag and control access via container‑level firewall rules or Docker’s built‑in ACLs, though the internal flag is the simplest pattern for Compose projects.
Removing ports does not automatically isolate a service if the network is not marked internal: true; otherwise containers on the default bridge can still reach each other on allocated IPs.