1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
17 "git.arvados.org/arvados.git/sdk/go/arvados"
18 dockercontainer "github.com/docker/docker/api/types/container"
19 dockerclient "github.com/docker/docker/client"
22 // Docker daemon won't let you set a limit less than ~10 MiB
23 const minDockerRAM = int64(16 * 1024 * 1024)
25 // DockerAPIVersion is the API version we use to communicate with the
26 // docker service. The oldest OS we support is Ubuntu 18.04 (bionic)
27 // which originally shipped docker 1.17.12 / API 1.35 so there is no
28 // reason to use an older API version. See
29 // https://dev.arvados.org/issues/15370#note-38 and
30 // https://docs.docker.com/engine/api/.
31 const DockerAPIVersion = "1.35"
33 // Number of consecutive "inspect container" failures before
34 // concluding Docker is unresponsive, giving up, and cancelling the
36 const dockerWatchdogThreshold = 5
38 type dockerExecutor struct {
40 logf func(string, ...interface{})
41 watchdogInterval time.Duration
42 dockerclient *dockerclient.Client
44 savedIPAddress atomic.Value
49 func newDockerExecutor(containerUUID string, logf func(string, ...interface{}), watchdogInterval time.Duration) (*dockerExecutor, error) {
50 // API version 1.21 corresponds to Docker 1.9, which is
51 // currently the minimum version we want to support.
52 client, err := dockerclient.NewClient(dockerclient.DefaultDockerHost, DockerAPIVersion, nil, nil)
53 if watchdogInterval < 1 {
54 watchdogInterval = time.Minute * 2
56 return &dockerExecutor{
57 containerUUID: containerUUID,
59 watchdogInterval: watchdogInterval,
64 func (e *dockerExecutor) Runtime() string {
65 v, _ := e.dockerclient.ServerVersion(context.Background())
67 for _, cv := range v.Components {
71 info += cv.Name + " " + cv.Version
74 info = "(unknown version)"
76 return "docker " + info
79 func (e *dockerExecutor) LoadImage(imageID string, imageTarballPath string, container arvados.Container, arvMountPoint string,
80 containerClient *arvados.Client) error {
81 _, _, err := e.dockerclient.ImageInspectWithRaw(context.TODO(), imageID)
87 f, err := os.Open(imageTarballPath)
92 resp, err := e.dockerclient.ImageLoad(context.TODO(), f, true)
94 return fmt.Errorf("While loading container image into Docker: %v", err)
96 defer resp.Body.Close()
97 buf, _ := ioutil.ReadAll(resp.Body)
98 e.logf("loaded image: response %s", buf)
102 func (e *dockerExecutor) config(spec containerSpec) (dockercontainer.Config, dockercontainer.HostConfig) {
103 e.logf("Creating Docker container")
104 cfg := dockercontainer.Config{
107 WorkingDir: spec.WorkingDir,
108 Volumes: map[string]struct{}{},
109 OpenStdin: spec.Stdin != nil,
110 StdinOnce: spec.Stdin != nil,
111 AttachStdin: spec.Stdin != nil,
115 if cfg.WorkingDir == "." {
118 for k, v := range spec.Env {
119 cfg.Env = append(cfg.Env, k+"="+v)
121 if spec.RAM > 0 && spec.RAM < minDockerRAM {
122 spec.RAM = minDockerRAM
124 hostCfg := dockercontainer.HostConfig{
125 LogConfig: dockercontainer.LogConfig{
128 NetworkMode: dockercontainer.NetworkMode("none"),
129 Resources: dockercontainer.Resources{
130 CgroupParent: spec.CgroupParent,
131 NanoCPUs: int64(spec.VCPUs) * 1000000000,
132 Memory: spec.RAM, // RAM
133 MemorySwap: spec.RAM, // RAM+swap
134 KernelMemory: spec.RAM, // kernel portion
137 if spec.CUDADeviceCount != 0 {
138 var deviceIds []string
139 if cudaVisibleDevices := os.Getenv("CUDA_VISIBLE_DEVICES"); cudaVisibleDevices != "" {
140 // If a resource manager such as slurm or LSF told
141 // us to select specific devices we need to propagate that.
142 deviceIds = strings.Split(cudaVisibleDevices, ",")
145 deviceCount := spec.CUDADeviceCount
146 if len(deviceIds) > 0 {
147 // Docker won't accept both non-empty
148 // DeviceIDs and a non-zero Count
150 // (it turns out "Count" is a dumb fallback
151 // that just allocates device 0, 1, 2, ...,
156 // Capabilities are confusing. The driver has generic
157 // capabilities "gpu" and "nvidia" but then there's
158 // additional capabilities "compute" and "utility"
159 // that are passed to nvidia-container-cli.
161 // "compute" means include the CUDA libraries and
162 // "utility" means include the CUDA utility programs
163 // (like nvidia-smi).
165 // https://github.com/moby/moby/blob/7b9275c0da707b030e62c96b679a976f31f929d3/daemon/nvidia_linux.go#L37
166 // https://github.com/containerd/containerd/blob/main/contrib/nvidia/nvidia.go
167 hostCfg.Resources.DeviceRequests = append(hostCfg.Resources.DeviceRequests, dockercontainer.DeviceRequest{
170 DeviceIDs: deviceIds,
171 Capabilities: [][]string{[]string{"gpu", "nvidia", "compute", "utility"}},
174 for path, mount := range spec.BindMounts {
175 bind := mount.HostPath + ":" + path
179 hostCfg.Binds = append(hostCfg.Binds, bind)
181 if spec.EnableNetwork {
182 hostCfg.NetworkMode = dockercontainer.NetworkMode(spec.NetworkMode)
187 func (e *dockerExecutor) Create(spec containerSpec) error {
188 cfg, hostCfg := e.config(spec)
189 created, err := e.dockerclient.ContainerCreate(context.TODO(), &cfg, &hostCfg, nil, nil, e.containerUUID)
191 return fmt.Errorf("While creating container: %v", err)
193 e.containerID = created.ID
194 return e.startIO(spec.Stdin, spec.Stdout, spec.Stderr)
197 func (e *dockerExecutor) Pid() int {
198 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Second))
200 ctr, err := e.dockerclient.ContainerInspect(ctx, e.containerID)
201 if err == nil && ctr.State != nil {
208 func (e *dockerExecutor) Start() error {
209 return e.dockerclient.ContainerStart(context.TODO(), e.containerID, dockercontainer.StartOptions{})
212 func (e *dockerExecutor) Stop() error {
213 err := e.dockerclient.ContainerRemove(context.TODO(), e.containerID, dockercontainer.RemoveOptions{Force: true})
214 if err != nil && strings.Contains(err.Error(), "No such container: "+e.containerID) {
220 // Wait for the container to terminate, capture the exit code, and
221 // wait for stdout/stderr logging to finish.
222 func (e *dockerExecutor) Wait(ctx context.Context) (int, error) {
223 ctx, cancel := context.WithCancel(ctx)
225 watchdogErr := make(chan error, 1)
227 ticker := time.NewTicker(e.watchdogInterval)
230 dctx, dcancel := context.WithDeadline(ctx, time.Now().Add(e.watchdogInterval))
231 ctr, err := e.dockerclient.ContainerInspect(dctx, e.containerID)
233 if ctx.Err() != nil {
234 // Either the container already
235 // exited, or our caller is trying to
238 } else if err != nil {
239 watchdogErr <- fmt.Errorf("error inspecting container: %s", err)
240 } else if ctr.State == nil || !(ctr.State.Running || ctr.State.Status == "created") {
241 watchdogErr <- fmt.Errorf("container is not running: State=%v", ctr.State)
248 waitOk, waitErr := e.dockerclient.ContainerWait(ctx, e.containerID, dockercontainer.WaitConditionNotRunning)
252 case waitBody := <-waitOk:
253 // wait for stdout/stderr to complete
255 return int(waitBody.StatusCode), nil
257 case err := <-waitErr:
258 return -1, fmt.Errorf("container wait: %v", err)
263 case err := <-watchdogErr:
267 e.logf("docker watchdog: %s", err)
269 if errors >= dockerWatchdogThreshold {
270 e.logf("docker watchdog: giving up")
278 func (e *dockerExecutor) startIO(stdin io.Reader, stdout, stderr io.Writer) error {
279 resp, err := e.dockerclient.ContainerAttach(context.TODO(), e.containerID, dockercontainer.AttachOptions{
286 return fmt.Errorf("error attaching container stdin/stdout/stderr streams: %v", err)
291 errStdin = e.handleStdin(stdin, resp.Conn, resp.CloseWrite)
294 e.doneIO = make(chan struct{})
296 e.errIO = e.handleStdoutStderr(stdout, stderr, resp.Reader)
297 if e.errIO == nil && errStdin != nil {
305 func (e *dockerExecutor) handleStdin(stdin io.Reader, conn io.Writer, closeConn func() error) error {
307 _, err := io.Copy(conn, stdin)
309 return fmt.Errorf("While writing to docker container on stdin: %v", err)
314 // Handle docker log protocol; see
315 // https://docs.docker.com/engine/reference/api/docker_remote_api_v1.15/#attach-to-a-container
316 func (e *dockerExecutor) handleStdoutStderr(stdout, stderr io.Writer, reader io.Reader) error {
317 header := make([]byte, 8)
320 _, err = io.ReadAtLeast(reader, header, 8)
327 readsize := int64(header[7]) | (int64(header[6]) << 8) | (int64(header[5]) << 16) | (int64(header[4]) << 24)
329 _, err = io.CopyN(stdout, reader, readsize)
332 _, err = io.CopyN(stderr, reader, readsize)
336 return fmt.Errorf("error copying stdout/stderr from docker: %v", err)
341 func (e *dockerExecutor) Close() {
342 e.dockerclient.ContainerRemove(context.TODO(), e.containerID, dockercontainer.RemoveOptions{Force: true})
345 func (e *dockerExecutor) InjectCommand(ctx context.Context, detachKeys, username string, usingTTY bool, injectcmd []string) (*exec.Cmd, error) {
346 cmd := exec.CommandContext(ctx, "docker", "exec", "-i", "--detach-keys="+detachKeys, "--user="+username)
348 cmd.Args = append(cmd.Args, "-t")
350 cmd.Args = append(cmd.Args, e.containerID)
351 cmd.Args = append(cmd.Args, injectcmd...)
355 func (e *dockerExecutor) IPAddress() (string, error) {
356 if ip, ok := e.savedIPAddress.Load().(*string); ok {
359 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Minute))
361 ctr, err := e.dockerclient.ContainerInspect(ctx, e.containerID)
363 return "", fmt.Errorf("cannot get docker container info: %s", err)
365 ip := ctr.NetworkSettings.IPAddress
367 // TODO: try to enable networking if it wasn't
368 // already enabled when the container was
370 return "", fmt.Errorf("container has no IP address")
372 e.savedIPAddress.Store(&ip)