Skip to content

Commit

Permalink
Copy network, pid and uts mode only if target container is running
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecruz91 committed Nov 8, 2022
1 parent a4a756a commit e0929f7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ Sometimes it's useful to change the entrypoint and/or command for a container, f
To simulate a crashing application, use docker run to create a container that immediately exits:

```shell
docker run busybox:1.28 /bin/sh -c "false"
docker run --name crashing-container busybox:1.28 /bin/sh -c "false"
```

You can use `debug-ctr debug` with `--entrypoint` and/or `--cmd` to create a copy of this container with the command changed to an interactive shell:

```shell
debug-ctr debug --image=docker.io/alpine:latest --target=my-distroless --copy-to=my-distroless-copy --entrypoint="/.debugger/sleep" --cmd="365d"
debug-ctr debug --image=docker.io/alpine:latest --target=crashing-container --copy-to=crashing-container-copy --entrypoint="/.debugger/sleep" --cmd="365d"
```

Now you have an interactive shell that you can use to perform tasks like checking filesystem paths or running a container command manually.
Expand Down
22 changes: 14 additions & 8 deletions cmd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,19 @@ func createCopyContainer(ctx context.Context, debugImage, targetContainer, copyC
log.Printf("containerCmd: %+v", containerCmd)

target := "container:" + targetContainer

hostConfig := &container.HostConfig{
Binds: []string{
volume + ":" + "/.debugger",
},
}

if inspect.State.Running {
hostConfig.NetworkMode = container.NetworkMode(target)
hostConfig.PidMode = container.PidMode(target)
hostConfig.UTSMode = container.UTSMode(target)
}

copyContainerCreateResp, err := cli.ContainerCreate(ctx, &container.Config{
Image: inspect.Image,
User: inspect.Config.User,
Expand All @@ -241,14 +254,7 @@ func createCopyContainer(ctx context.Context, debugImage, targetContainer, copyC
Cmd: containerCmd,
WorkingDir: inspect.Config.WorkingDir,
Labels: inspect.Config.Labels,
}, &container.HostConfig{
Binds: []string{
volume + ":" + "/.debugger",
},
NetworkMode: container.NetworkMode(target),
PidMode: container.PidMode(target),
UTSMode: container.UTSMode(target),
}, nil, nil, copyContainerName)
}, hostConfig, nil, nil, copyContainerName)
if err != nil {
return err
}
Expand Down

0 comments on commit e0929f7

Please sign in to comment.