MCPcopy Index your code
hub / github.com/testcontainers/testcontainers-go / ExampleRun

Function ExampleRun

examples_test.go:18–131  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

16)
17
18func 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)

Callers

nothing calls this directly

Calls 11

NewFunction · 0.92
PrintfFunction · 0.92
WithNetworkFunction · 0.92
ForListeningPortFunction · 0.92
RemoveMethod · 0.65
WithEnvMethod · 0.65
StateMethod · 0.65
GetContainerIDMethod · 0.65
CopyFileFromContainerMethod · 0.65
RunMethod · 0.45
WithStartupTimeoutMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…