()
| 16 | ) |
| 17 | |
| 18 | func ExampleRun() { |
| 19 | ctx := context.Background() |
| 20 | |
| 21 | nw, err := network.New(ctx) |
| 22 | if err != nil { |
| 23 | log.Printf("failed to create network: %s", err) |
| 24 | return |
| 25 | } |
| 26 | defer func() { |
| 27 | if err := nw.Remove(ctx); err != nil { |
| 28 | log.Printf("failed to remove network: %s", err) |
| 29 | } |
| 30 | }() |
| 31 | |
| 32 | testFileContent := "Hello from file!" |
| 33 | |
| 34 | ctr, err := testcontainers.Run( |
| 35 | ctx, |
| 36 | "nginx:alpine", |
| 37 | network.WithNetwork([]string{"nginx-alias"}, nw), |
| 38 | testcontainers.WithFiles(testcontainers.ContainerFile{ |
| 39 | Reader: strings.NewReader(testFileContent), |
| 40 | ContainerFilePath: "/tmp/file.txt", |
| 41 | FileMode: 0o644, |
| 42 | }), |
| 43 | testcontainers.WithTmpfs(map[string]string{ |
| 44 | "/tmp": "rw", |
| 45 | }), |
| 46 | testcontainers.WithLabels(map[string]string{ |
| 47 | "testcontainers.label": "true", |
| 48 | }), |
| 49 | testcontainers.WithEnv(map[string]string{ |
| 50 | "TEST": "true", |
| 51 | }), |
| 52 | testcontainers.WithExposedPorts("80/tcp"), |
| 53 | testcontainers.WithAfterReadyCommand(testcontainers.NewRawCommand([]string{"echo", "hello", "world"})), |
| 54 | testcontainers.WithWaitStrategy(wait.ForListeningPort("80/tcp").WithStartupTimeout(time.Second*5)), |
| 55 | ) |
| 56 | defer func() { |
| 57 | if err := testcontainers.TerminateContainer(ctr); err != nil { |
| 58 | log.Printf("failed to terminate container: %s", err) |
| 59 | } |
| 60 | }() |
| 61 | if err != nil { |
| 62 | log.Printf("failed to start container: %s", err) |
| 63 | return |
| 64 | } |
| 65 | |
| 66 | state, err := ctr.State(ctx) |
| 67 | if err != nil { |
| 68 | log.Printf("failed to get container state: %s", err) |
| 69 | return |
| 70 | } |
| 71 | fmt.Println(state.Running) |
| 72 | |
| 73 | apiClient, err := testcontainers.NewDockerClientWithOpts(ctx) |
| 74 | if err != nil { |
| 75 | log.Printf("failed to create docker client: %s", err) |
nothing calls this directly
no test coverage detected
searching dependent graphs…