Skip to content

Executing commands

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:

req := ContainerRequest{
    Image: "alpine",
    WaitingFor: wait.ForAll(
        wait.ForLog("command override!"),
    ),
    Cmd: []string{"echo", "command override!"},
}

Info

If you are using a module, you can use the testcontainers.CustomizeRequest option to add arguments to the command. Check the individual module's pages for more information on their commands.

This option will merge the customized request into the module's request, appending any additional Cmd arguments to the module's command. This can't be used to replace the command, only to append options. Check the individual module's pages for more information on their commands.

Executing a command

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

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

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 interface). For example:

c, reader, err := ctr.Exec(ctx, []string{"ls", path})
require.NoError(t, 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)
require.NoError(t, err)

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

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