From 8c533179b0351611df07b7d712e7152fb4ca8dce Mon Sep 17 00:00:00 2001 From: Brian Neville <29639579+brianneville@users.noreply.github.com> Date: Thu, 18 Jun 2026 15:03:08 +0100 Subject: [PATCH] containers/docker: restart containers instead of recreating rather than removing the container and then recreating it, we can just call StartContainer on the container again. --- containers/docker/container_start.go | 6 +++- containers/docker/container_update.go | 40 ++++----------------------- 2 files changed, 10 insertions(+), 36 deletions(-) diff --git a/containers/docker/container_start.go b/containers/docker/container_start.go index 11eb488..d82b00e 100644 --- a/containers/docker/container_start.go +++ b/containers/docker/container_start.go @@ -34,7 +34,11 @@ func (m *Manager) ContainerRestart(ctx context.Context, instance string, _ ...op return status.Errorf(codes.Aborted, "expected container state to be %q, got %q", container.StateExited, j.State.Status) } - return m.restartContainer(ctx, j, true) + if err := m.client.ContainerStart(ctx, instance, container.StartOptions{}); err != nil { + return status.Errorf(codes.Internal, + "failed when starting container: %v", err) + } + return nil } // ContainerStart starts a container provided the image exists and that the ports requested are not diff --git a/containers/docker/container_update.go b/containers/docker/container_update.go index e6768a7..177f088 100644 --- a/containers/docker/container_update.go +++ b/containers/docker/container_update.go @@ -76,46 +76,16 @@ func (m *Manager) performContainerUpdate(ctx context.Context, instance, image, t // There was some error, let's try to restore previous state. errPfx := fmt.Sprintf("failed to update instance %s due to: %v", instance, err) - if recreateErr := m.restartContainer(ctx, oldCntJSON, - false); recreateErr != nil { - s, ok := status.FromError(recreateErr) - if !ok { - return "", fmt.Errorf( - "programming error: restartContainer"+ - " should return status error code, got %s: %s", - errPfx, recreateErr) - } - return "", status.Errorf(s.Code(), "%s; restoration of previous state %s", errPfx, recreateErr) - } - - return instance, status.Errorf(codes.Internal, "%s; yet, restoration of previous state succeeded", errPfx) -} - -// restartContainer recreates a container given a particular config. -// it is expected that the container in question has been stopped by the point this is called. -func (m *Manager) restartContainer(ctx context.Context, oldCntJSON types.ContainerJSON, - needsRemoval bool) error { - instanceName := strings.Replace(oldCntJSON.Name, "/", "", 1) - if needsRemoval { - if err := m.client.ContainerRemove(ctx, instanceName, - container.RemoveOptions{}); err != nil { - return status.Errorf(codes.Internal, - "failed when removing container: %v", err) - } - } - resp, err := m.client.ContainerCreate(ctx, - oldCntJSON.Config, oldCntJSON.HostConfig, &network.NetworkingConfig{}, - nil, instanceName) + resp, err := m.client.ContainerCreate(ctx, oldCntJSON.Config, oldCntJSON.HostConfig, &network.NetworkingConfig{}, nil, instance) if err != nil { - return status.Errorf(codes.Internal, - "failed when creating container: %v", err) + return "", status.Errorf(codes.Internal, "%s; restoration of previous state failed when creating container: %v", errPfx, err) } if err := m.client.ContainerStart(ctx, resp.ID, container.StartOptions{}); err != nil { - return status.Errorf(codes.Internal, - "failed when starting container: %v", err) + return "", status.Errorf(codes.Internal, "%s; restoration of previous state failed when starting container: %v", errPfx, err) } - return nil + + return instance, status.Errorf(codes.Internal, "%s; yet, restoration of previous state succeeded", errPfx) } func (m *Manager) performContainerUpdatePrechecks(ctx context.Context, instance, imageName, tag, cmd string, async bool, opts ...options.Option) ([]types.Container, error) {