Home
Networking
登录后可跨设备保存划线和私人笔记登录
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<?php
declare(strict_types=1);
use Testcontainers\Container\GenericContainer;
use Testcontainers\Wait\WaitForHttp;
$container = (new GenericContainer('nginx:alpine'))
->withExposedPorts(80)
->withWait((new WaitForHttp(80))->withPath('/'))
->start();
$url = sprintf('http://%s:%d', $container->getHost(), $container->getMappedPort(80));
echo $url . PHP_EOL;
$container->stop();Use the first mapped port
If a container exposes only one port, use getFirstMappedPort():
1$port = $container->getFirstMappedPort();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:
1docker network create my-test-network 1
2
3
4
5
6
7
8
9
10
11
12
13<?php
declare(strict_types=1);
use Testcontainers\Container\GenericContainer;
$container = (new GenericContainer('alpine'))
->withCommand(['tail', '-f', '/dev/null'])
->withNetwork('my-test-network')
->withAliases(['service-a'])
->start();
$container->stop();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.
评论
登录后参与评论
正在加载评论…
KnowForge