# Docker Compose: service without external port

LLMS index: [llms.txt](/en/llms.txt)

---

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 |

> [!TIP]
> 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.internal` block with `driver: bridge` and `internal: true`
- each service listing `networks: [internal]` without any `ports:` 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:

```bash
docker compose exec web curl -s http://db:5432
```

From the host, attempt a connection to confirm it is not listening on a published port; the command should time out or refuse:

```bash
curl -v http://localhost:5432
```

Inspect the network’s internal flag directly:

```bash
docker network inspect <project>_internal -f '{{json .Internal}}'
```

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.

> [!WARNING]
> 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.
