Home
Executing commands
登录后可跨设备保存划线和私人笔记登录
Executing commands
Container startup command
By default the container will execute whatever command is specified in the image's Dockerfile. To override this, and specify a different command, use withCommand. For example:
Specifying a startup command
public GenericContainer redisWithCustomPort = new GenericContainer(DockerImageName.parse("redis:6-alpine"))
.withCommand("redis-server --port 7777")Executing a command
Your test can execute a command inside a running container, similar to a docker exec call:
Executing a command inside a running container
container.execInContainer("touch", "/somefile.txt");This can be useful for software that has a command line administration tool. You can also get the output (stdout/stderr) and exit code from the command - for example:
Executing a command inside a running container and reading the result
Container.ExecResult lsResult = container.execInContainer("ls", "-al", "/");
String stdout = lsResult.getStdout();
int exitCode = lsResult.getExitCode();
assertThat(stdout).contains("somefile.txt");
assertThat(exitCode).isZero();Environment variables
To add environment variables to the container, use withEnv:
new GenericContainer(...)
.withEnv("API_TOKEN", "foo")评论
登录后参与评论
正在加载评论…
KnowForge