mirror of
https://github.com/docker/docs.git
synced 2026-04-02 01:08:53 +07:00
## Description Migrate 17 Testcontainers guides from testcontainers.com into the Docker docs site, covering Java (14 guides), .NET (2 guides), and Node.js (1 guide). This follows up on PR #24450 which added the initial Go and Python guides. Each guide is converted from AsciiDoc to Hugo Markdown, split into multi-chapter stepper navigation, updated to the latest Testcontainers API, and verified with passing tests running in containers. Java guides use testcontainers-java 2.0.4 with the new 2.x Maven coordinates and package names (e.g., `testcontainers-postgresql`, `org.testcontainers.postgresql.PostgreSQLContainer`). The Quarkus guide uses Quarkus 3.22.3 with TC 1.x managed by the Quarkus BOM, since no released Quarkus version ships TC 2.x yet. ## How to test All code snippets have been verified by running each guide's source repository tests inside Docker containers with the Docker socket mounted. To re-run the verification, use the `/testcontainers-guides-migrator` skill included in this PR (`.claude/skills/testcontainers-guides-migrator/SKILL.md`). The skill's Step 6 documents the exact container commands and macOS Docker Desktop workarounds (host override, docker-java API version, etc.) needed to run each language's tests: ``` /testcontainers-guides-migrator I want you to verify all the guides in this branch. Do a full review, verifying that all code snippets compile, the code is executable, and ALL the tests pass. Run them as docker containers, never locally. ``` ## Related issues or tickets Supersedes #24450 (expanded from 2 guides to all 19) ## Reviews - [ ] Technical review - [ ] Editorial review - [ ] Product review --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
118 lines
3.9 KiB
Markdown
118 lines
3.9 KiB
Markdown
---
|
|
title: Execute commands inside containers
|
|
linkTitle: Execute commands
|
|
description: Run commands inside running containers to initialize services for testing.
|
|
weight: 20
|
|
---
|
|
|
|
Some Docker containers provide CLI tools for performing actions. You can use
|
|
`container.execInContainer(String...)` to run any available command inside a
|
|
running container.
|
|
|
|
## Example: Create an S3 bucket in LocalStack
|
|
|
|
The [LocalStack](https://localstack.cloud/) module emulates AWS services. To
|
|
test S3 file uploads, create a bucket before running tests:
|
|
|
|
```java
|
|
package com.testcontainers.demo;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
import static org.testcontainers.containers.localstack.LocalStackContainer.Service.S3;
|
|
|
|
import java.io.IOException;
|
|
import java.net.URI;
|
|
import java.util.List;
|
|
import org.junit.jupiter.api.BeforeAll;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.testcontainers.containers.localstack.LocalStackContainer;
|
|
import org.testcontainers.junit.jupiter.Container;
|
|
import org.testcontainers.junit.jupiter.Testcontainers;
|
|
import org.testcontainers.utility.DockerImageName;
|
|
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
|
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
|
import software.amazon.awssdk.regions.Region;
|
|
import software.amazon.awssdk.services.s3.S3Client;
|
|
import software.amazon.awssdk.services.s3.model.Bucket;
|
|
|
|
@Testcontainers
|
|
class LocalStackTest {
|
|
|
|
static final String bucketName = "mybucket";
|
|
|
|
@Container
|
|
static LocalStackContainer localStack = new LocalStackContainer(
|
|
DockerImageName.parse("localstack/localstack:3.4.0")
|
|
);
|
|
|
|
@BeforeAll
|
|
static void beforeAll() throws IOException, InterruptedException {
|
|
localStack.execInContainer("awslocal", "s3", "mb", "s3://" + bucketName);
|
|
|
|
org.testcontainers.containers.Container.ExecResult execResult =
|
|
localStack.execInContainer("awslocal", "s3", "ls");
|
|
String stdout = execResult.getStdout();
|
|
int exitCode = execResult.getExitCode();
|
|
assertTrue(stdout.contains(bucketName));
|
|
assertEquals(0, exitCode);
|
|
}
|
|
|
|
@Test
|
|
void shouldListBuckets() {
|
|
URI s3Endpoint = localStack.getEndpointOverride(S3);
|
|
StaticCredentialsProvider credentialsProvider =
|
|
StaticCredentialsProvider.create(
|
|
AwsBasicCredentials.create(
|
|
localStack.getAccessKey(),
|
|
localStack.getSecretKey()
|
|
)
|
|
);
|
|
S3Client s3 = S3Client
|
|
.builder()
|
|
.endpointOverride(s3Endpoint)
|
|
.credentialsProvider(credentialsProvider)
|
|
.region(Region.of(localStack.getRegion()))
|
|
.build();
|
|
|
|
List<String> s3Buckets = s3
|
|
.listBuckets()
|
|
.buckets()
|
|
.stream()
|
|
.map(Bucket::name)
|
|
.toList();
|
|
|
|
assertTrue(s3Buckets.contains(bucketName));
|
|
}
|
|
}
|
|
```
|
|
|
|
The `execInContainer("awslocal", "s3", "mb", "s3://mybucket")` call runs the
|
|
`awslocal` CLI tool (provided by the LocalStack image) to create an S3 bucket.
|
|
|
|
You can capture the output and exit code from any command:
|
|
|
|
```java
|
|
Container.ExecResult execResult =
|
|
localStack.execInContainer("awslocal", "s3", "ls");
|
|
String stdout = execResult.getStdout();
|
|
int exitCode = execResult.getExitCode();
|
|
```
|
|
|
|
> [!NOTE]
|
|
> The `withCopyFileToContainer()` and `execInContainer()` methods are inherited
|
|
> from `GenericContainer`, so they're available for all Testcontainers modules.
|
|
|
|
## Summary
|
|
|
|
- Use `withCopyFileToContainer()` to place initialization files inside
|
|
containers before they start.
|
|
- Use `execInContainer()` to run commands inside running containers for
|
|
setup tasks like creating buckets, topics, or queues.
|
|
|
|
## Further reading
|
|
|
|
- [Getting started with Testcontainers for Java](/guides/testcontainers-java-getting-started/)
|
|
- [Testcontainers Postgres module](https://java.testcontainers.org/modules/databases/postgres/)
|
|
- [Testcontainers LocalStack module](https://java.testcontainers.org/modules/localstack/)
|