Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more content on how to use Exec function #1970

Merged
merged 3 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1298,21 +1298,34 @@ func TestContainerWithTmpFs(t *testing.T) {

path := "/testtmpfs/test.file"

c, _, err := container.Exec(ctx, []string{"ls", path})
// exec_reader_example {
c, reader, err := container.Exec(ctx, []string{"ls", path})
if err != nil {
t.Fatal(err)
}
if c != 1 {
t.Fatalf("File %s should not have existed, expected return code 1, got %v", path, c)
}

buf := new(strings.Builder)
_, err = io.Copy(buf, reader)
if err != nil {
t.Fatal(err)
}

// See the logs from the command execution.
t.Log(buf.String())
// }

// exec_example {
c, _, err = container.Exec(ctx, []string{"touch", path})
if err != nil {
t.Fatal(err)
}
if c != 0 {
t.Fatalf("File %s should have been created successfully, expected return code 0, got %v", path, c)
}
// }

c, _, err = container.Exec(ctx, []string{"ls", path})
if err != nil {
Expand Down
24 changes: 21 additions & 3 deletions docs/features/override_container_command.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Sending a CMD to a Container
# Executing commands

If you would like to send a CMD (command) to a container, you can pass it in to
the container request via the `Cmd` field...
## Container startup command

By default the container will execute whatever command is specified in the image's Dockerfile. If you would like to send a CMD (command) to a container, you can pass it in to
the container request via the `Cmd` field. For example:

```go
req := ContainerRequest{
Expand All @@ -13,3 +15,19 @@ req := ContainerRequest{
}
```

## Executing a command

You can execute a command inside a running container, similar to a `docker exec` call:

<!--codeinclude-->
[Executing a command](../../docker_test.go) inside_block:exec_example
<!--/codeinclude-->

This can be useful for software that has a command line administration tool. You can also get the logs of the command execution (from an object that implements the [io.Reader](https://pkg.go.dev/io#Reader) interface). For example:


<!--codeinclude-->
[Command logs](../../docker_test.go) inside_block:exec_reader_example
<!--/codeinclude-->

This is done this way, because it brings more flexibility to the user, rather than returning a string.