Skip to content

Commit 2c35211

Browse files
goldshtnMisty Stanley-Jones
authored andcommitted
Fix run-container example (docker#5937)
The very first example (https://docs.docker.com/develop/sdk/examples/#run-a-container) has two annoying issues fixed by this patch: 1. The `ImagePull` call does not complete before the subsequent `ContainerCreate` call is issued. As a result, there is a race between pulling the image and starting the container from the image. For locally cached images, `ContainerCreate` will still work, but for images that have to be pulled, it will likely fail. This was [reported on StackOverflow](https://stackoverflow.com/questions/42816132/docker-run-using-golang-api-docker-docs) but not fixed yet. 2. The `ContainerLogs` call doesn't return a string, but rather an intermixed byte buffer with the stdout and stderr from the container. This can be highly confusing. Added `Tty: true` to the `ContainerCreate` call such that the `ContainerLogs` output only includes stdout.
1 parent b7a9862 commit 2c35211

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

develop/sdk/examples.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,16 @@ func main() {
5656
panic(err)
5757
}
5858

59-
_, err = cli.ImagePull(ctx, "docker.io/library/alpine", types.ImagePullOptions{})
59+
reader, err := cli.ImagePull(ctx, "docker.io/library/alpine", types.ImagePullOptions{})
6060
if err != nil {
6161
panic(err)
6262
}
63+
io.Copy(os.Stdout, reader)
6364

6465
resp, err := cli.ContainerCreate(ctx, &container.Config{
6566
Image: "alpine",
6667
Cmd: []string{"echo", "hello world"},
68+
Tty: true,
6769
}, nil, nil, "")
6870
if err != nil {
6971
panic(err)

0 commit comments

Comments
 (0)