Skip to content

[Bug]: Reused container is started even when Started = false #3567

Description

@mathieu-lemay

Testcontainers version

0.40.0

Using the latest Testcontainers version?

Yes

Host OS

macOS

Host arch

ARM

Go version

1.26.1

Docker version

Client:
 Version:           28.5.2
 API version:       1.51
 Go version:        go1.25.3
 Git commit:        ecc6942
 Built:             Wed Nov  5 14:42:30 2025
 OS/Arch:           darwin/arm64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          28.5.2
  API version:      1.51 (minimum version 1.24)
  Go version:       go1.24.9
  Git commit:       89c5e8f
  Built:            Wed Nov  5 14:43:35 2025
  OS/Arch:          linux/arm64
  Experimental:     false
 containerd:
  Version:          v2.2.0
  GitCommit:        1c4457e00facac03ce1d75f7b6777a7a851e5c41
 runc:
  Version:          1.3.3
  GitCommit:        d842d7719497cc3b774fd71620278ac9e17710e0
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Docker info

Client:
 Version:    28.5.2
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.29.1
    Path:     /Users/<REDACTED>/.config/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.40.3
    Path:     /Users/<REDACTED>/.config/docker/cli-plugins/docker-compose

Server:
 Containers: 12
  Running: 10
  Paused: 0
  Stopped: 2
 Images: 93
 Server Version: 28.5.2
 Storage Driver: overlay2
  Backing Filesystem: btrfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 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
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 1c4457e00facac03ce1d75f7b6777a7a851e5c41
 runc version: d842d7719497cc3b774fd71620278ac9e17710e0
 init version: de40ad0
 Security Options:
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 6.17.8-orbstack-00308-g8f9c941121b1
 Operating System: OrbStack
 OSType: linux
 Architecture: aarch64
 CPUs: 14
 Total Memory: 15.65GiB
 Name: orbstack
 ID: 2f78f5d9-31ea-4067-9655-86b186beb585
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Username: <REDACTED>
 Experimental: false
 Insecure Registries:
  ::1/128
  127.0.0.0/8
 Live Restore Enabled: false
 Product License: Community Engine
 Default Address Pools:
   Base: 192.168.97.0/24, Size: 24
   Base: 192.168.107.0/24, Size: 24
   Base: 192.168.117.0/24, Size: 24
   Base: 192.168.147.0/24, Size: 24
   Base: 192.168.148.0/24, Size: 24
   Base: 192.168.155.0/24, Size: 24
   Base: 192.168.156.0/24, Size: 24
   Base: 192.168.158.0/24, Size: 24
   Base: 192.168.163.0/24, Size: 24
   Base: 192.168.164.0/24, Size: 24
   Base: 192.168.165.0/24, Size: 24
   Base: 192.168.166.0/24, Size: 24
   Base: 192.168.167.0/24, Size: 24
   Base: 192.168.171.0/24, Size: 24
   Base: 192.168.172.0/24, Size: 24
   Base: 192.168.181.0/24, Size: 24
   Base: 192.168.183.0/24, Size: 24
   Base: 192.168.186.0/24, Size: 24
   Base: 192.168.207.0/24, Size: 24
   Base: 192.168.214.0/24, Size: 24
   Base: 192.168.215.0/24, Size: 24
   Base: 192.168.216.0/24, Size: 24
   Base: 192.168.223.0/24, Size: 24
   Base: 192.168.227.0/24, Size: 24
   Base: 192.168.228.0/24, Size: 24
   Base: 192.168.229.0/24, Size: 24
   Base: 192.168.237.0/24, Size: 24
   Base: 192.168.239.0/24, Size: 24
   Base: 192.168.242.0/24, Size: 24
   Base: 192.168.247.0/24, Size: 24
   Base: fd07:b51a:cc66:d000::/56, Size: 64

What happened?

When a generic container is created with Started set to false and Reuse set to true, it works as expected if the container doesn't already exists. However, if the container already exist and is reused, it will be started, even if Started is set to false.

This code can be used to reproduce the issue. It should print Container is not running, as expected twice, since the container is never explicitly started, but in the second function call, it actually is started.

package main

import (
	"context"
	"fmt"
	"log"
	"time"

	tc "github.com/testcontainers/testcontainers-go"
)

func main() {
	ctx := context.Background()

	req := tc.GenericContainerRequest{
		ContainerRequest: tc.ContainerRequest{
			Name:  "reuse-start-issue",
			Image: "alpine",
			Cmd:   []string{"sleep", "5"},
		},
		Started: false,
		Reuse:   true,
	}

	err := runContainer(ctx, req)
	if err != nil {
		log.Fatalf("%v", err)
	}
	err = runContainer(ctx, req)
	if err != nil {
		log.Fatalf("%v", err)
	}
}

func runContainer(ctx context.Context, req tc.GenericContainerRequest) error {
	c, err := tc.GenericContainer(ctx, req)
	if err != nil {
		return fmt.Errorf("error creating container: %w", err)
	}

	defer func() {
		timeout := time.Second
		err := c.Stop(ctx, &timeout)
		if err != nil {
			log.Printf("error stopping container: %v", err)
		}
	}()

	status, err := c.State(ctx)
	if err != nil {
		return fmt.Errorf("error getting container state: %w", err)
	}

	if status.Running {
		return fmt.Errorf("container is running, but it shouldn't be")
	}

	log.Printf("Container is not running, as expected")

	return nil
}

Relevant log output

Additional information

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugAn issue with the library

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions