-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docker: add run (container create+start) method (#4991)
Single-shot method to create and run a container, similar to `docker run`. The container create + container run semantics of the Docker API are more low-level than we care about right now, so it's easier to encapsulate these into a single method. It also has some convenience bits around getting container logs and the execution result (which has quirky semantics).
- Loading branch information
Showing
8 changed files
with
435 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//+build !skipcontainertests | ||
|
||
package docker | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/docker/distribution/reference" | ||
"github.com/stretchr/testify/require" | ||
|
||
wmcontainer "github.com/tilt-dev/tilt/internal/container" | ||
"github.com/tilt-dev/tilt/internal/k8s" | ||
"github.com/tilt-dev/tilt/internal/testutils" | ||
) | ||
|
||
func TestCli_Run(t *testing.T) { | ||
ctx, _, _ := testutils.CtxAndAnalyticsForTest() | ||
dEnv := ProvideClusterEnv(ctx, "gke", k8s.EnvGKE, wmcontainer.RuntimeDocker, k8s.FakeMinikube{}) | ||
cli := NewDockerClient(ctx, Env(dEnv)) | ||
defer func() { | ||
// release any idle connections to avoid out of file errors if running test many times | ||
_ = cli.(*Cli).Close() | ||
}() | ||
|
||
ref, err := reference.ParseNamed("docker.io/library/hello-world") | ||
require.NoError(t, err) | ||
|
||
var stdout bytes.Buffer | ||
r, err := cli.Run(ctx, RunConfig{ | ||
Pull: true, | ||
Image: ref, | ||
Stdout: &stdout, | ||
}) | ||
require.NoError(t, err, "Error during run") | ||
exitCode, err := r.Wait() | ||
require.NoError(t, err, "Error waiting for exit") | ||
require.NoError(t, r.Close(), "Error cleaning up container") | ||
require.Equal(t, int64(0), exitCode, "Non-zero exit code from container") | ||
require.Contains(t, stdout.String(), "Hello from Docker", "Bad stdout") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package docker | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/docker/distribution/reference" | ||
mobycontainer "github.com/docker/docker/api/types/container" | ||
"github.com/docker/docker/api/types/mount" | ||
) | ||
|
||
// RunConfig defines the container to create and start. | ||
type RunConfig struct { | ||
// Image to execute. | ||
// | ||
// If Pull is true, this must be a reference.Named. | ||
Image reference.Reference | ||
// Pull will ensure the image exists locally before running. | ||
// | ||
// If an image will only be used once, this is a convenience to avoid calling ImagePull first. | ||
// If an image will be used multiple times (across containers), prefer explicitly calling ImagePull | ||
// to avoid the overhead of calling the registry API to check if the image is up-to-date every time. | ||
Pull bool | ||
// ContainerName is a unique name for the container. If not specified, Docker will generate a random name. | ||
ContainerName string | ||
// Stdout from the container will be written here if non-nil. | ||
// | ||
// Errors copying the container output are logged but not propagated. | ||
Stdout io.Writer | ||
// Stderr from the container will be written here if non-nil. | ||
// | ||
// Errors copying the container output are logged but not propagated. | ||
Stderr io.Writer | ||
// Cmd to run when starting the container. | ||
Cmd []string | ||
// Mounts to attach to the container. | ||
Mounts []mount.Mount | ||
} | ||
|
||
// RunResult contains information about a container execution. | ||
type RunResult struct { | ||
ContainerID string | ||
|
||
logsErrCh <-chan error | ||
statusRespCh <-chan mobycontainer.ContainerWaitOKBody | ||
statusErrCh <-chan error | ||
tearDown func(containerID string) error | ||
} | ||
|
||
// Wait blocks until stdout and stderr have been fully consumed (if writers were passed via RunConfig) and the | ||
// container has exited. If there is any error consuming stdout/stderr or monitoring the container execution, an | ||
// error will be returned. | ||
func (r *RunResult) Wait() (int64, error) { | ||
select { | ||
case err := <-r.statusErrCh: | ||
return -1, err | ||
case statusResp := <-r.statusRespCh: | ||
if statusResp.Error != nil { | ||
return -1, fmt.Errorf("error waiting on container: %s", statusResp.Error.Message) | ||
} | ||
logsErr := <-r.logsErrCh | ||
if logsErr != nil { | ||
// error is | ||
return statusResp.StatusCode, fmt.Errorf("error reading container logs: %w", logsErr) | ||
} | ||
return statusResp.StatusCode, nil | ||
} | ||
} | ||
|
||
// Close removes the container (forcibly if it's still running). | ||
func (r *RunResult) Close() error { | ||
if r.tearDown == nil { | ||
return nil | ||
} | ||
err := r.tearDown(r.ContainerID) | ||
if err != nil { | ||
return err | ||
} | ||
r.tearDown = nil | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.