Networking¶
Testcontainers for PHP maps container ports to random host ports by default.
Access a service from your test process¶
Use getHost() and getMappedPort():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
Use the first mapped port¶
If a container exposes only one port, use getFirstMappedPort():
1 | |
Join a Docker network¶
Connect multiple containers to the same existing Docker network and use aliases. Testcontainers for PHP does not create Docker networks, so create the network before starting containers:
1 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
Notes¶
- Do not hardcode localhost ports in tests.
- Always resolve endpoints from
getHost()and mapped ports. - Named networks and aliases are useful for container-to-container communication.
Related docs: containers, configuration, troubleshooting.