[Bug]: Container with port binding that exits during startup throws TimeoutException after 15 s instead of ContainerNotRunningException

Open
#1,819 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@seragAhmed is already working on this.

Since Nov 17, 2026.

  • #1825 by @seragAhmed — open

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
csharp, docker
Domain
devtools, testing

Research direction

Read DockerContainer.cs around the internal WaitUntilPortBindingsMapped strategy and ThrowIfContainerNotRunningAsync, then inspect the related changes in #1550. Reproduce with the provided Alpine container and port binding, and check existing wait strategy tests for coverage. Done when an early container exit consistently raises ContainerNotRunningException with exit code and logs instead of timing out.

Written by the indexing model from the issue text.

Description

bug
Testcontainers version

4.16.0

Using the latest Testcontainers version?

Yes

Host OS

Windows 11 (Docker Desktop, WSL2)

Host arch

x86_64

.NET version

10.0.201

Docker version
Client:
 Version:           29.9.3
 API version:       1.56
 Go version:        go1.26.10
 Git commit:        5457199
 Built:             Tue Nov  3 21:46:10 2026
 OS/Arch:           windows/amd64
 Context:           desktop-linux

Server: Docker Desktop 4.99.0 (245822)
 Engine:
  Version:          29.9.3
  API version:      1.56 (minimum version 1.40)
  Go version:       go1.26.10
  Git commit:       70efc0e
  Built:            Tue Nov  3 21:41:42 2026
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v2.4.2
  GitCommit:        b7a25261e029d68978bbf290df0a62fc819b3c51
 runc:
  Version:          1.5.2
  GitCommit:        v1.5.2-0-g29dd3dc2
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
Docker info
Server:
 Containers: 11
  Running: 4
  Paused: 0
  Stopped: 7
 Images: 23
 Server Version: 29.9.3
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: local
 Cgroup Driver: cgroupfs
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 CDI spec directories:
  /etc/cdi
  /var/run/cdi
 Discovered Devices:
  cdi: docker.com/gpu=webgpu
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 nvidia runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: b7a25261e029d68978bbf290df0a62fc819b3c51
 runc version: v1.5.2-0-g29dd3dc2
 init version: de40ad0
 Security Options:
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 6.6.114.1-microsoft-standard-WSL2
 Operating System: Docker Desktop
 OSType: linux
 Architecture: x86_64
 CPUs: 12
 Total Memory: 15.57GiB
 Name: docker-desktop
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 HTTP Proxy: http.docker.internal:3128
 HTTPS Proxy: http.docker.internal:3128
 No Proxy: hubproxy.docker.internal
 Labels:
  com.docker.desktop.address=npipe://\\.\pipe\docker_cli
 Experimental: false
 Insecure Registries:
  hubproxy.docker.internal:5555
  127.0.0.0/8
  ::1/128
 Live Restore Enabled: false
 Firewall Backend: iptables
What happened?

When a container has a port binding (WithPortBinding) and exits during startup, StartAsync waits 15 s and throws TimeoutException instead of ContainerNotRunningException (introduced in #1550).

Minimal repro:

await using var container = new ContainerBuilder("alpine:3.20")
    .WithCommand("sh", "-c", "echo boom >&2; exit 1")
    .WithPortBinding(80, true)
    .WithWaitStrategy(Wait.ForUnixContainer().UntilInternalTcpPortIsAvailable(80))
    .Build();

await container.StartAsync(); // TimeoutException after ~15 s, container.State == Exited

Results:

Configuration Result
with WithPortBinding(80, true) System.TimeoutException after ~15 s, State = Exited (every run)
without port binding ContainerNotRunningException after 2.5 s: exited with code 1

Cause: UnsafeStartAsync always runs the internal WaitUntilPortBindingsMapped strategy before the user's wait strategies (DockerContainer.cs#L641). That strategy uses WaitStrategyMode.OneShot (L898), and ThrowIfContainerNotRunningAsync only throws in Running mode (L877). An exited container has no host port bindings, so the strategy never succeeds and runs into its 15 s timeout.

If the container exits a bit later (after the ports are mapped), the user's Running wait strategy sees Exited and correctly throws ContainerNotRunningException. So the outcome depends on timing. We hit it as a flaky test in CI: a Firebird container with a broken init script sometimes threw ContainerNotRunningException, and sometimes TimeoutException on a re-run of the same commit.

Expected: ContainerNotRunningException with exit code and logs, no matter how fast the container exits.

Possible fix: let WaitUntilPortBindingsMapped fail fast when the container has exited. For example, give it Running mode, or check TestcontainersStates.Exited in that step.

Relevant log output
with WithPortBinding(80, true):
run 1: System.TimeoutException after 17,3s, state=Exited: The operation has timed out.
run 2: System.TimeoutException after 15,4s, state=Exited: The operation has timed out.
run 3: System.TimeoutException after 15,5s, state=Exited: The operation has timed out.

without WithPortBinding:
run 1: DotNet.Testcontainers.Containers.ContainerNotRunningException after 2,5s, state=Exited: Container 5e76c513e607... exited with code 1.

Original CI failure (Testcontainers 4.13.0):
Expected a <DotNet.Testcontainers.Containers.ContainerNotRunningException> to be thrown,
but found <System.TimeoutException>: The operation has timed out.
   at DotNet.Testcontainers.Configurations.WaitStrategy.WaitUntilAsync(...)
   at DotNet.Testcontainers.Containers.DockerContainer.CheckReadinessAsync(...)
   at DotNet.Testcontainers.Containers.DockerContainer.UnsafeStartAsync(...)
Additional information

Related: #1445, #1550 (introduced WaitStrategyMode and ContainerNotRunningException; the internal port-binding check was left in OneShot mode).
Reproduced on Testcontainers 4.13.0 and 4.16.0; the relevant code is unchanged on develop.
Also reproduced on Docker Desktop 4.96.0 (Engine 29.9.1) and 4.99.0 (Engine 29.9.3) with the same result.

Dominant language
C#
Stars
4.4k
Forks
366
Avg merge
2d 6h
Merged PRs (30d)
19

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from testcontainers/testcontainers-dotnet

All issues in testcontainers/testcontainers-dotnet

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.