skip to content
Alvin Lucillo

Generic testcontainer

/ 1 min read

💻 Tech

Testcontainers are great way to spin up containers for integration test. For example, a mongodb container.

go get github.com/testcontainers/testcontainers-go
	ctx := context.Background()
	req := testcontainers.ContainerRequest{
		Image:        "mongo:6.0",
		ExposedPorts: []string{"27017/tcp"},
		WaitingFor:   wait.ForListeningPort("27017/tcp"),
	}

	var err error
	mongoContainer, err = testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
		ContainerRequest: req,
		Started:          true,
	})
	if err != nil {
		log.Fatalf("Failed to start MongoDB container: %v", err)
	}