public interface Container<SELF extends Container<SELF>> extends LinkableContainer, ContainerState
Modifier and Type | Interface and Description |
---|---|
static class |
Container.ExecResult
Class to hold results from a "docker exec" command.
|
STATE_HEALTHY
Modifier and Type | Method and Description |
---|---|
void |
addEnv(java.lang.String key,
java.lang.String value)
Add an environment variable to be passed to the container.
|
void |
addExposedPort(java.lang.Integer port)
Add an exposed port.
|
void |
addExposedPorts(int... ports)
Add exposed ports.
|
default void |
addFileSystemBind(java.lang.String hostPath,
java.lang.String containerPath,
BindMode mode)
Adds a file system binding.
|
void |
addFileSystemBind(java.lang.String hostPath,
java.lang.String containerPath,
BindMode mode,
SelinuxContext selinuxContext)
Adds a file system binding.
|
void |
addLink(LinkableContainer otherContainer,
java.lang.String alias)
|
void |
copyFileFromContainer(java.lang.String containerPath,
java.lang.String destinationPath)
Copies a file which resides inside the container to user defined directory
|
<T> T |
copyFileFromContainer(java.lang.String containerPath,
ThrowingFunction<java.io.InputStream,T> function)
Streams a file which resides inside the container
|
void |
copyFileToContainer(MountableFile mountableFile,
java.lang.String containerPath)
Copies a file or directory to the container.
|
void |
copyFileToContainer(Transferable transferable,
java.lang.String containerPath)
Copies a file to the container.
|
Container.ExecResult |
execInContainer(java.nio.charset.Charset outputCharset,
java.lang.String... command)
Run a command inside a running container, as though using "docker exec".
|
Container.ExecResult |
execInContainer(java.lang.String... command)
Run a command inside a running container, as though using "docker exec", and interpreting
the output as UTF8.
|
com.github.dockerjava.api.model.Info |
fetchDockerDaemonInfo()
Deprecated.
please use
org.testcontainers.DockerClientFactory.instance().client().infoCmd().exec() |
default void |
followOutput(java.util.function.Consumer<OutputFrame> consumer)
Follow container output, sending each frame (usually, line) to a consumer.
|
default void |
followOutput(java.util.function.Consumer<OutputFrame> consumer,
OutputFrame.OutputType... types)
Follow container output, sending each frame (usually, line) to a consumer.
|
java.util.List<com.github.dockerjava.api.model.Bind> |
getBinds() |
java.lang.String[] |
getCommandParts() |
com.github.dockerjava.api.DockerClient |
getDockerClient() |
com.github.dockerjava.api.model.Info |
getDockerDaemonInfo()
Deprecated.
please use
org.testcontainers.DockerClientFactory.instance().client().infoCmd().exec() |
java.lang.String |
getDockerImageName()
Get image name.
|
java.util.List<java.lang.String> |
getEnv()
Deprecated.
use getEnvMap
|
java.util.Map<java.lang.String,java.lang.String> |
getEnvMap() |
java.util.List<java.lang.String> |
getExtraHosts() |
java.util.concurrent.Future<java.lang.String> |
getImage() |
java.util.Map<java.lang.String,LinkableContainer> |
getLinkedContainers()
|
java.util.List<java.lang.String> |
getPortBindings() |
java.lang.String |
getTestHostIpAddress()
Get the IP address that containers (e.g.
|
default SELF |
self() |
void |
setBinds(java.util.List<com.github.dockerjava.api.model.Bind> binds) |
void |
setCommand(java.lang.String... commandParts)
Set the command that should be run in the container.
|
void |
setCommand(java.lang.String command)
Set the command that should be run in the container.
|
void |
setCommandParts(java.lang.String[] commandParts) |
void |
setDockerImageName(java.lang.String dockerImageName)
Resolve Docker image and set it.
|
void |
setEnv(java.util.List<java.lang.String> env) |
void |
setExposedPorts(java.util.List<java.lang.Integer> exposedPorts) |
void |
setExtraHosts(java.util.List<java.lang.String> extraHosts) |
void |
setImage(java.util.concurrent.Future<java.lang.String> image) |
void |
setLinkedContainers(java.util.Map<java.lang.String,LinkableContainer> linkedContainers)
|
void |
setPortBindings(java.util.List<java.lang.String> portBindings) |
void |
setWaitStrategy(WaitStrategy waitStrategy) |
SELF |
waitingFor(WaitStrategy waitStrategy)
Specify the
WaitStrategy to use to determine if the container is ready. |
default SELF |
withClasspathResourceMapping(java.lang.String resourcePath,
java.lang.String containerPath,
BindMode mode)
Map a resource (file or directory) on the classpath to a path inside the container.
|
SELF |
withClasspathResourceMapping(java.lang.String resourcePath,
java.lang.String containerPath,
BindMode mode,
SelinuxContext selinuxContext)
Map a resource (file or directory) on the classpath to a path inside the container.
|
SELF |
withCommand(java.lang.String... commandParts)
Set the command that should be run in the container
|
SELF |
withCommand(java.lang.String cmd)
Set the command that should be run in the container
|
SELF |
withCopyFileToContainer(MountableFile mountableFile,
java.lang.String containerPath)
Set the file to be copied before starting a created container
|
SELF |
withEnv(java.util.Map<java.lang.String,java.lang.String> env)
Add environment variables to be passed to the container.
|
default SELF |
withEnv(java.lang.String key,
java.util.function.Function<java.util.Optional<java.lang.String>,java.lang.String> mapper)
Add an environment variable to be passed to the container.
|
SELF |
withEnv(java.lang.String key,
java.lang.String value)
Add an environment variable to be passed to the container.
|
SELF |
withExposedPorts(java.lang.Integer... ports)
Set the ports that this container listens on
|
SELF |
withExtraHost(java.lang.String hostname,
java.lang.String ipAddress)
Add an extra host entry to be passed to the container
|
default SELF |
withFileSystemBind(java.lang.String hostPath,
java.lang.String containerPath)
Adds a file system binding.
|
SELF |
withFileSystemBind(java.lang.String hostPath,
java.lang.String containerPath,
BindMode mode)
Adds a file system binding.
|
SELF |
withLabel(java.lang.String key,
java.lang.String value)
Add a label to the container.
|
SELF |
withLabels(java.util.Map<java.lang.String,java.lang.String> labels)
Add labels to the container.
|
SELF |
withLogConsumer(java.util.function.Consumer<OutputFrame> consumer)
Attach an output consumer at container startup, enabling stdout and stderr to be followed, waited on, etc.
|
SELF |
withMinimumRunningDuration(java.time.Duration minimumRunningDuration)
Only consider a container to have successfully started if it has been running for this duration.
|
SELF |
withNetwork(Network network)
Set the network for this container, similar to the
--network <name>
option on the docker CLI. |
SELF |
withNetworkAliases(java.lang.String... aliases)
Set the network aliases for this container, similar to the
--network-alias <my-service>
option on the docker CLI. |
SELF |
withNetworkMode(java.lang.String networkMode)
Set the network mode for this container, similar to the
--net <name>
option on the docker CLI. |
SELF |
withPrivilegedMode(boolean mode)
Set the privilegedMode mode for the container
|
SELF |
withStartupCheckStrategy(StartupCheckStrategy strategy)
Set the startup check strategy used for checking whether the container has started.
|
SELF |
withStartupTimeout(java.time.Duration startupTimeout)
Set the duration of waiting time until container treated as started.
|
SELF |
withVolumesFrom(Container container,
BindMode mode)
Adds container volumes.
|
SELF |
withWorkingDirectory(java.lang.String workDir)
Set the working directory that the container should use on startup.
|
getContainerName
getBoundPortNumbers, getContainerId, getContainerInfo, getContainerIpAddress, getCurrentContainerInfo, getExposedPorts, getFirstMappedPort, getMappedPort, isCreated, isHealthy, isRunning
default SELF self()
void setCommand(@NonNull java.lang.String command)
withCommand(String)
for building a container in a fluent style.command
- a command in single string format (will automatically be split on spaces)void setCommand(@NonNull java.lang.String... commandParts)
withCommand(String...)
for building a container in a fluent style.commandParts
- a command as an array of string partsvoid addEnv(java.lang.String key, java.lang.String value)
withEnv(String, String)
for building a container in a fluent style.key
- environment variable keyvalue
- environment variable valuedefault void addFileSystemBind(java.lang.String hostPath, java.lang.String containerPath, BindMode mode)
withFileSystemBind(String, String, BindMode)
for building a container in a fluent style.hostPath
- the file system path on the hostcontainerPath
- the file system path inside the containermode
- the bind modevoid addFileSystemBind(java.lang.String hostPath, java.lang.String containerPath, BindMode mode, SelinuxContext selinuxContext)
withFileSystemBind(String, String, BindMode)
for building a container in a fluent style.hostPath
- the file system path on the hostcontainerPath
- the file system path inside the containermode
- the bind modeselinuxContext
- selinux context argument to use for this file@Deprecated void addLink(LinkableContainer otherContainer, java.lang.String alias)
otherContainer
- the other container object to link toalias
- the alias (for the other container) that this container should be able to usevoid addExposedPort(java.lang.Integer port)
withExposedPorts(Integer...)
for building a container in a fluent style.port
- a TCP portvoid addExposedPorts(int... ports)
withExposedPorts(Integer...)
for building a container in a fluent style.ports
- an array of TCP portsSELF waitingFor(@NonNull WaitStrategy waitStrategy)
WaitStrategy
to use to determine if the container is ready.waitStrategy
- the WaitStrategy to useWait.defaultWaitStrategy()
default SELF withFileSystemBind(java.lang.String hostPath, java.lang.String containerPath)
hostPath
- the file system path on the hostcontainerPath
- the file system path inside the containerSELF withFileSystemBind(java.lang.String hostPath, java.lang.String containerPath, BindMode mode)
hostPath
- the file system path on the hostcontainerPath
- the file system path inside the containermode
- the bind modeSELF withVolumesFrom(Container container, BindMode mode)
container
- the container to add volumes frommode
- the bind modeSELF withExposedPorts(java.lang.Integer... ports)
ports
- an array of TCP portsSELF withCopyFileToContainer(MountableFile mountableFile, java.lang.String containerPath)
mountableFile
- a Mountable file with path of source file / folder on host machinecontainerPath
- a destination path on conatiner to which the files / folders to be copiedSELF withEnv(java.lang.String key, java.lang.String value)
key
- environment variable keyvalue
- environment variable valuedefault SELF withEnv(java.lang.String key, java.util.function.Function<java.util.Optional<java.lang.String>,java.lang.String> mapper)
key
- environment variable keymapper
- environment variable value mapper, accepts old value as an argumentSELF withEnv(java.util.Map<java.lang.String,java.lang.String> env)
env
- map of environment variablesSELF withLabel(java.lang.String key, java.lang.String value)
key
- label keyvalue
- label valueSELF withLabels(java.util.Map<java.lang.String,java.lang.String> labels)
labels
- map of labelsSELF withCommand(java.lang.String cmd)
cmd
- a command in single string format (will automatically be split on spaces)SELF withCommand(java.lang.String... commandParts)
commandParts
- a command as an array of string partsSELF withExtraHost(java.lang.String hostname, java.lang.String ipAddress)
hostname
- hostname to use for this hosts file entryipAddress
- IP address to use for this hosts file entrySELF withNetworkMode(java.lang.String networkMode)
--net <name>
option on the docker CLI.networkMode
- network mode, e.g. including 'host', 'bridge', 'none' or the name of an existing named network.SELF withNetwork(Network network)
--network <name>
option on the docker CLI.network
- the instance of Network
SELF withNetworkAliases(java.lang.String... aliases)
--network-alias <my-service>
option on the docker CLI.aliases
- the list of aliasesdefault SELF withClasspathResourceMapping(java.lang.String resourcePath, java.lang.String containerPath, BindMode mode)
resourcePath
- path to the resource on the classpath (relative to the classpath root; should not start with a leading slash)containerPath
- path this should be mapped to inside the containermode
- access mode for the fileSELF withClasspathResourceMapping(java.lang.String resourcePath, java.lang.String containerPath, BindMode mode, SelinuxContext selinuxContext)
resourcePath
- path to the resource on the classpath (relative to the classpath root; should not start with a leading slash)containerPath
- path this should be mapped to inside the containermode
- access mode for the fileselinuxContext
- selinux context argument to use for this fileSELF withStartupTimeout(java.time.Duration startupTimeout)
startupTimeout
- timeoutWaitStrategy.waitUntilReady(org.testcontainers.containers.wait.strategy.WaitStrategyTarget)
SELF withPrivilegedMode(boolean mode)
mode
- booleanSELF withMinimumRunningDuration(java.time.Duration minimumRunningDuration)
minimumRunningDuration
- duration this container should run for if started successfullySELF withStartupCheckStrategy(StartupCheckStrategy strategy)
strategy
- startup check strategySELF withWorkingDirectory(java.lang.String workDir)
workDir
- path to the working directory inside the containervoid setDockerImageName(@NonNull java.lang.String dockerImageName)
dockerImageName
- image name@NonNull java.lang.String getDockerImageName()
java.lang.String getTestHostIpAddress()
For example, if a web server is running on port 8080 on this local machine, the containerized web driver needs to be pointed at "http://" + getTestHostIpAddress() + ":8080" in order to access it. Trying to hit localhost from inside the container is not going to work, since the container has its own IP address.
default void followOutput(java.util.function.Consumer<OutputFrame> consumer)
consumer
- consumer that the frames should be sent todefault void followOutput(java.util.function.Consumer<OutputFrame> consumer, OutputFrame.OutputType... types)
consumer
- consumer that the frames should be sent totypes
- types that should be followed (one or both of STDOUT, STDERR)SELF withLogConsumer(java.util.function.Consumer<OutputFrame> consumer)
More than one consumer may be registered.
consumer
- consumer that output frames should be sent to@Deprecated com.github.dockerjava.api.model.Info fetchDockerDaemonInfo() throws java.io.IOException
org.testcontainers.DockerClientFactory.instance().client().infoCmd().exec()
java.io.IOException
Container.ExecResult execInContainer(java.lang.String... command) throws java.lang.UnsupportedOperationException, java.io.IOException, java.lang.InterruptedException
java.lang.UnsupportedOperationException
java.io.IOException
java.lang.InterruptedException
ExecInContainerPattern.execInContainer(com.github.dockerjava.api.command.InspectContainerResponse, String...)
Container.ExecResult execInContainer(java.nio.charset.Charset outputCharset, java.lang.String... command) throws java.lang.UnsupportedOperationException, java.io.IOException, java.lang.InterruptedException
java.lang.UnsupportedOperationException
java.io.IOException
java.lang.InterruptedException
ExecInContainerPattern.execInContainer(com.github.dockerjava.api.command.InspectContainerResponse, Charset, String...)
void copyFileToContainer(MountableFile mountableFile, java.lang.String containerPath) throws java.io.IOException, java.lang.InterruptedException
mountableFile
- file or directory which is copied into the containercontainerPath
- destination path inside the containerjava.io.IOException
- if there's an issue communicating with Dockerjava.lang.InterruptedException
- if the thread waiting for the response is interruptedvoid copyFileToContainer(Transferable transferable, java.lang.String containerPath)
transferable
- file which is copied into the containercontainerPath
- destination path inside the containervoid copyFileFromContainer(java.lang.String containerPath, java.lang.String destinationPath) throws java.io.IOException, java.lang.InterruptedException
containerPath
- path to file which is copied from containerdestinationPath
- destination path to which file is copied with file namejava.io.IOException
- if there's an issue communicating with Docker or receiving entry from TarArchiveInputStreamjava.lang.InterruptedException
- if the thread waiting for the response is interrupted<T> T copyFileFromContainer(java.lang.String containerPath, ThrowingFunction<java.io.InputStream,T> function)
containerPath
- path to file which is copied from containerfunction
- function that takes InputStream of the copied filejava.util.List<java.lang.String> getPortBindings()
getPortBindings
in interface ContainerState
java.util.List<java.lang.String> getExtraHosts()
java.util.concurrent.Future<java.lang.String> getImage()
@Deprecated java.util.List<java.lang.String> getEnv()
java.util.Map<java.lang.String,java.lang.String> getEnvMap()
java.lang.String[] getCommandParts()
java.util.List<com.github.dockerjava.api.model.Bind> getBinds()
@Deprecated java.util.Map<java.lang.String,LinkableContainer> getLinkedContainers()
com.github.dockerjava.api.DockerClient getDockerClient()
@Deprecated com.github.dockerjava.api.model.Info getDockerDaemonInfo()
org.testcontainers.DockerClientFactory.instance().client().infoCmd().exec()
void setExposedPorts(java.util.List<java.lang.Integer> exposedPorts)
void setPortBindings(java.util.List<java.lang.String> portBindings)
void setExtraHosts(java.util.List<java.lang.String> extraHosts)
void setImage(java.util.concurrent.Future<java.lang.String> image)
void setEnv(java.util.List<java.lang.String> env)
void setCommandParts(java.lang.String[] commandParts)
void setBinds(java.util.List<com.github.dockerjava.api.model.Bind> binds)
@Deprecated void setLinkedContainers(java.util.Map<java.lang.String,LinkableContainer> linkedContainers)
void setWaitStrategy(WaitStrategy waitStrategy)