Skip to content
Merged
16 changes: 15 additions & 1 deletion docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,7 @@ func (p *DockerProvider) findContainerByName(ctx context.Context, name string) (

// Note that, 'name' filter will use regex to find the containers
filter := filters.NewArgs(filters.Arg("name", fmt.Sprintf("^%s$", name)))
containers, err := p.client.ContainerList(ctx, container.ListOptions{Filters: filter})
containers, err := p.client.ContainerList(ctx, container.ListOptions{All: true, Filters: filter})
if err != nil {
return nil, fmt.Errorf("container list: %w", err)
}
Expand Down Expand Up @@ -1364,6 +1364,20 @@ func (p *DockerProvider) ReuseOrCreateContainer(ctx context.Context, req Contain
lifecycleHooks: []ContainerLifecycleHooks{combineContainerHooks(defaultHooks, req.LifecycleHooks)},
}

// If a container was stopped programmatically, and Ryuk is enabled, we want to
// ensure the container is running again, but only if it is not paused, as it's
// not possible to start a paused container. The Docker Engine returns the
// "cannot start a paused container, try unpause instead" error.
switch c.State {
case "running", "paused":
// cannot re-start a paused or running container, but we still need
// to call the startup hooks.
default:
if err := dc.Start(ctx); err != nil {
return dc, fmt.Errorf("start container %s in state %s: %w", req.Name, c.State, err)
}
}

err = dc.startedHook(ctx)
if err != nil {
return nil, err
Expand Down
39 changes: 39 additions & 0 deletions reuse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package testcontainers_test

import (
"context"
"testing"

"github.com/stretchr/testify/require"

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

func TestGenericContainer_stop_start_withReuse(t *testing.T) {
containerName := "my-postgres"

req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: nginxAlpineImage,
ExposedPorts: []string{"8080/tcp"},
Name: containerName,
},
Reuse: true,
Started: true,
}

ctr, err := testcontainers.GenericContainer(context.Background(), req)
testcontainers.CleanupContainer(t, ctr)
require.NoError(t, err)
require.NotNil(t, ctr)

err = ctr.Stop(context.Background(), nil)
require.NoError(t, err)

// Run another container with same container name:
// The checks for the exposed ports must not fail when restarting the container.
ctr1, err := testcontainers.GenericContainer(context.Background(), req)
testcontainers.CleanupContainer(t, ctr1)
require.NoError(t, err)
require.NotNil(t, ctr1)
}
Loading