public class GenericContainer<SELF extends GenericContainer<SELF>> extends FailureDetectingExternalResource implements Container<SELF>, java.lang.AutoCloseable, WaitStrategyTarget, Startable
Modifier and Type | Class and Description |
---|---|
static class |
GenericContainer.AbstractWaitStrategy
Deprecated.
|
Container.ExecResult
Modifier and Type | Field and Description |
---|---|
static int |
CONTAINER_RUNNING_TIMEOUT_SEC |
protected java.lang.String |
containerId |
protected java.lang.String |
containerName |
protected com.github.dockerjava.api.DockerClient |
dockerClient |
protected com.github.dockerjava.api.model.Info |
dockerDaemonInfo |
static java.lang.String |
INTERNAL_HOST_HOSTNAME |
protected WaitStrategy |
waitStrategy
The approach to determine if the container is ready.
|
STATE_HEALTHY
Constructor and Description |
---|
GenericContainer() |
GenericContainer(java.util.concurrent.Future<java.lang.String> image) |
GenericContainer(java.lang.String dockerImageName) |
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.
|
void |
addFileSystemBind(java.lang.String hostPath,
java.lang.String containerPath,
BindMode mode,
SelinuxContext selinuxContext)
Adds a file system binding.
|
protected void |
addFixedExposedPort(int hostPort,
int containerPort)
Add a TCP container port that should be bound to a fixed port on the docker host.
|
protected void |
addFixedExposedPort(int hostPort,
int containerPort,
InternetProtocol protocol)
Add a container port that should be bound to a fixed port on the docker host.
|
void |
addLink(LinkableContainer otherContainer,
java.lang.String alias)
|
org.junit.runners.model.Statement |
apply(org.junit.runners.model.Statement base,
org.junit.runner.Description description)
Deprecated.
|
protected void |
configure() |
protected void |
containerIsCreated(java.lang.String containerId) |
protected void |
containerIsStarted(com.github.dockerjava.api.command.InspectContainerResponse containerInfo) |
protected void |
containerIsStarting(com.github.dockerjava.api.command.InspectContainerResponse containerInfo) |
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> consumer)
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.
|
protected java.nio.file.Path |
createVolumeDirectory(boolean temporary)
Creates a directory on the local filesystem which will be mounted as a volume for the container.
|
protected void |
doStart() |
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.
|
protected void |
failed(java.lang.Throwable e,
org.junit.runner.Description description)
Deprecated.
|
com.github.dockerjava.api.model.Info |
fetchDockerDaemonInfo() |
protected void |
finished(org.junit.runner.Description description)
Deprecated.
|
java.lang.String |
getDockerImageName()
Get image name.
|
java.util.List<java.lang.String> |
getEnv() |
java.util.Map<java.lang.String,java.lang.String> |
getEnvMap() |
java.lang.String |
getIpAddress()
Deprecated.
please use getContainerIpAddress() instead
|
protected java.lang.Integer |
getLivenessCheckPort()
Deprecated.
see
getLivenessCheckPorts() for replacement |
java.util.Set<java.lang.Integer> |
getLivenessCheckPortNumbers() |
protected @NotNull java.util.Set<java.lang.Integer> |
getLivenessCheckPorts()
Deprecated.
use
getLivenessCheckPortNumbers() instead |
java.lang.String |
getTestHostIpAddress()
Get the IP address that containers (e.g.
|
protected WaitStrategy |
getWaitStrategy()
The
WaitStrategy to use to determine if the container is ready. |
protected org.slf4j.Logger |
logger()
Provide a logger that references the docker image name.
|
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 |
setDockerImageName(java.lang.String dockerImageName)
Resolve Docker image and set it.
|
void |
setEnv(java.util.List<java.lang.String> env) |
void |
setWaitStrategy(WaitStrategy waitStrategy) |
void |
start()
Starts the container using docker, pulling an image if necessary.
|
protected void |
starting(org.junit.runner.Description description)
Deprecated.
|
void |
stop()
Stops the container.
|
protected void |
succeeded(org.junit.runner.Description description)
Deprecated.
|
SELF |
waitingFor(WaitStrategy waitStrategy)
Specify the
WaitStrategy to use to determine if the container is ready. |
protected void |
waitUntilContainerStarted()
Wait until the container has started.
|
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 |
withCreateContainerCmdModifier(java.util.function.Consumer<com.github.dockerjava.api.command.CreateContainerCmd> modifier)
Allow low level modifications of
CreateContainerCmd after it was pre-configured in tryStart(Profiler) . |
SELF |
withEnv(java.util.Map<java.lang.String,java.lang.String> env)
Add environment variables 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
|
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 |
withStartupAttempts(int attempts)
Allow container startup to be attempted more than once if an error occurs.
|
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.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
addFileSystemBind, followOutput, followOutput, getBinds, getCommandParts, getDockerClient, getDockerDaemonInfo, getExtraHosts, getImage, getLinkedContainers, getPortBindings, self, setBinds, setCommandParts, setExposedPorts, setExtraHosts, setImage, setLinkedContainers, setPortBindings, withEnv, withFileSystemBind
getContainerName
getBoundPortNumbers, getContainerId, getContainerInfo, getContainerIpAddress, getCurrentContainerInfo, getExposedPorts, getFirstMappedPort, getMappedPort, isCreated, isHealthy, isRunning
public static final int CONTAINER_RUNNING_TIMEOUT_SEC
public static final java.lang.String INTERNAL_HOST_HOSTNAME
protected com.github.dockerjava.api.DockerClient dockerClient
protected com.github.dockerjava.api.model.Info dockerDaemonInfo
protected java.lang.String containerId
protected java.lang.String containerName
@NonNull protected WaitStrategy waitStrategy
public GenericContainer()
public GenericContainer(@NonNull java.lang.String dockerImageName)
public GenericContainer(@NonNull java.util.concurrent.Future<java.lang.String> image)
public void start()
protected void doStart()
protected org.slf4j.Logger logger()
protected java.nio.file.Path createVolumeDirectory(boolean temporary)
temporary
- is the volume directory temporary? If true, the directory will be deleted on JVM shutdown.protected void configure()
protected void containerIsCreated(java.lang.String containerId)
protected void containerIsStarting(com.github.dockerjava.api.command.InspectContainerResponse containerInfo)
protected void containerIsStarted(com.github.dockerjava.api.command.InspectContainerResponse containerInfo)
@Deprecated protected java.lang.Integer getLivenessCheckPort()
getLivenessCheckPorts()
for replacement@NotNull @NonNull @Deprecated protected @NotNull java.util.Set<java.lang.Integer> getLivenessCheckPorts()
getLivenessCheckPortNumbers()
insteadpublic java.util.Set<java.lang.Integer> getLivenessCheckPortNumbers()
getLivenessCheckPortNumbers
in interface WaitStrategyTarget
public SELF waitingFor(@NonNull WaitStrategy waitStrategy)
WaitStrategy
to use to determine if the container is ready.waitingFor
in interface Container<SELF extends GenericContainer<SELF>>
waitStrategy
- the WaitStrategy to useWait.defaultWaitStrategy()
protected WaitStrategy getWaitStrategy()
WaitStrategy
to use to determine if the container is ready.
Defaults to Wait.defaultWaitStrategy()
.WaitStrategy
to usepublic void setWaitStrategy(WaitStrategy waitStrategy)
setWaitStrategy
in interface Container<SELF extends GenericContainer<SELF>>
protected void waitUntilContainerStarted()
WaitStrategy
public void setCommand(@NonNull java.lang.String command)
Container.withCommand(String)
for building a container in a fluent style.setCommand
in interface Container<SELF extends GenericContainer<SELF>>
command
- a command in single string format (will automatically be split on spaces)public void setCommand(@NonNull java.lang.String... commandParts)
Container.withCommand(String...)
for building a container in a fluent style.setCommand
in interface Container<SELF extends GenericContainer<SELF>>
commandParts
- a command as an array of string partspublic java.util.Map<java.lang.String,java.lang.String> getEnvMap()
getEnvMap
in interface Container<SELF extends GenericContainer<SELF>>
public java.util.List<java.lang.String> getEnv()
getEnv
in interface Container<SELF extends GenericContainer<SELF>>
public void setEnv(java.util.List<java.lang.String> env)
setEnv
in interface Container<SELF extends GenericContainer<SELF>>
public void addEnv(java.lang.String key, java.lang.String value)
Container.withEnv(String, String)
for building a container in a fluent style.addEnv
in interface Container<SELF extends GenericContainer<SELF>>
key
- environment variable keyvalue
- environment variable valuepublic void addFileSystemBind(java.lang.String hostPath, java.lang.String containerPath, BindMode mode, SelinuxContext selinuxContext)
Container.withFileSystemBind(String, String, BindMode)
for building a container in a fluent style.addFileSystemBind
in interface Container<SELF extends GenericContainer<SELF>>
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 filepublic SELF withFileSystemBind(java.lang.String hostPath, java.lang.String containerPath, BindMode mode)
withFileSystemBind
in interface Container<SELF extends GenericContainer<SELF>>
hostPath
- the file system path on the hostcontainerPath
- the file system path inside the containermode
- the bind modepublic SELF withVolumesFrom(Container container, BindMode mode)
withVolumesFrom
in interface Container<SELF extends GenericContainer<SELF>>
container
- the container to add volumes frommode
- the bind mode@Deprecated public void addLink(LinkableContainer otherContainer, java.lang.String alias)
Container
addLink
in interface Container<SELF extends GenericContainer<SELF>>
otherContainer
- the other container object to link toalias
- the alias (for the other container) that this container should be able to usepublic void addExposedPort(java.lang.Integer port)
Container
Container.withExposedPorts(Integer...)
for building a container in a fluent style.addExposedPort
in interface Container<SELF extends GenericContainer<SELF>>
port
- a TCP portpublic void addExposedPorts(int... ports)
Container
Container.withExposedPorts(Integer...)
for building a container in a fluent style.addExposedPorts
in interface Container<SELF extends GenericContainer<SELF>>
ports
- an array of TCP ports@Deprecated public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runner.Description description)
apply
in interface org.junit.rules.TestRule
apply
in class FailureDetectingExternalResource
@Deprecated protected void starting(org.junit.runner.Description description)
starting
in class FailureDetectingExternalResource
@Deprecated protected void succeeded(org.junit.runner.Description description)
succeeded
in class FailureDetectingExternalResource
@Deprecated protected void failed(java.lang.Throwable e, org.junit.runner.Description description)
failed
in class FailureDetectingExternalResource
@Deprecated protected void finished(org.junit.runner.Description description)
finished
in class FailureDetectingExternalResource
public SELF withExposedPorts(java.lang.Integer... ports)
withExposedPorts
in interface Container<SELF extends GenericContainer<SELF>>
ports
- an array of TCP portsprotected void addFixedExposedPort(int hostPort, int containerPort)
Note that this method is protected scope to discourage use, as clashes or instability are more likely when
using fixed port mappings. If you need to use this method from a test, please use FixedHostPortGenericContainer
instead of GenericContainer.
hostPort
- containerPort
- protected void addFixedExposedPort(int hostPort, int containerPort, InternetProtocol protocol)
Note that this method is protected scope to discourage use, as clashes or instability are more likely when
using fixed port mappings. If you need to use this method from a test, please use FixedHostPortGenericContainer
instead of GenericContainer.
hostPort
- containerPort
- protocol
- public SELF withEnv(java.lang.String key, java.lang.String value)
withEnv
in interface Container<SELF extends GenericContainer<SELF>>
key
- environment variable keyvalue
- environment variable valuepublic SELF withEnv(java.util.Map<java.lang.String,java.lang.String> env)
withEnv
in interface Container<SELF extends GenericContainer<SELF>>
env
- map of environment variablespublic SELF withLabel(java.lang.String key, java.lang.String value)
withLabel
in interface Container<SELF extends GenericContainer<SELF>>
key
- label keyvalue
- label valuepublic SELF withLabels(java.util.Map<java.lang.String,java.lang.String> labels)
withLabels
in interface Container<SELF extends GenericContainer<SELF>>
labels
- map of labelspublic SELF withCommand(java.lang.String cmd)
withCommand
in interface Container<SELF extends GenericContainer<SELF>>
cmd
- a command in single string format (will automatically be split on spaces)public SELF withCommand(java.lang.String... commandParts)
withCommand
in interface Container<SELF extends GenericContainer<SELF>>
commandParts
- a command as an array of string partspublic SELF withExtraHost(java.lang.String hostname, java.lang.String ipAddress)
withExtraHost
in interface Container<SELF extends GenericContainer<SELF>>
hostname
- hostname to use for this hosts file entryipAddress
- IP address to use for this hosts file entrypublic SELF withNetworkMode(java.lang.String networkMode)
Container
--net <name>
option on the docker CLI.withNetworkMode
in interface Container<SELF extends GenericContainer<SELF>>
networkMode
- network mode, e.g. including 'host', 'bridge', 'none' or the name of an existing named network.public SELF withNetwork(Network network)
Container
--network <name>
option on the docker CLI.withNetwork
in interface Container<SELF extends GenericContainer<SELF>>
network
- the instance of Network
public SELF withNetworkAliases(java.lang.String... aliases)
Container
--network-alias <my-service>
option on the docker CLI.withNetworkAliases
in interface Container<SELF extends GenericContainer<SELF>>
aliases
- the list of aliasespublic SELF withClasspathResourceMapping(java.lang.String resourcePath, java.lang.String containerPath, BindMode mode)
withClasspathResourceMapping
in interface Container<SELF extends GenericContainer<SELF>>
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 filepublic SELF withClasspathResourceMapping(java.lang.String resourcePath, java.lang.String containerPath, BindMode mode, SelinuxContext selinuxContext)
withClasspathResourceMapping
in interface Container<SELF extends GenericContainer<SELF>>
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 filepublic SELF withStartupTimeout(java.time.Duration startupTimeout)
withStartupTimeout
in interface Container<SELF extends GenericContainer<SELF>>
startupTimeout
- timeoutWaitStrategy.waitUntilReady(org.testcontainers.containers.wait.strategy.WaitStrategyTarget)
public SELF withPrivilegedMode(boolean mode)
Container
withPrivilegedMode
in interface Container<SELF extends GenericContainer<SELF>>
mode
- booleanpublic SELF withMinimumRunningDuration(java.time.Duration minimumRunningDuration)
withMinimumRunningDuration
in interface Container<SELF extends GenericContainer<SELF>>
minimumRunningDuration
- duration this container should run for if started successfullypublic SELF withStartupCheckStrategy(StartupCheckStrategy strategy)
withStartupCheckStrategy
in interface Container<SELF extends GenericContainer<SELF>>
strategy
- startup check strategypublic SELF withWorkingDirectory(java.lang.String workDir)
withWorkingDirectory
in interface Container<SELF extends GenericContainer<SELF>>
workDir
- path to the working directory inside the containerpublic SELF withCopyFileToContainer(MountableFile mountableFile, java.lang.String containerPath)
withCopyFileToContainer
in interface Container<SELF extends GenericContainer<SELF>>
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 copied@Deprecated public java.lang.String getIpAddress()
public void setDockerImageName(@NonNull java.lang.String dockerImageName)
setDockerImageName
in interface Container<SELF extends GenericContainer<SELF>>
dockerImageName
- image name@NonNull public java.lang.String getDockerImageName()
getDockerImageName
in interface Container<SELF extends GenericContainer<SELF>>
public 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.
getTestHostIpAddress
in interface Container<SELF extends GenericContainer<SELF>>
public SELF withLogConsumer(java.util.function.Consumer<OutputFrame> consumer)
More than one consumer may be registered.
withLogConsumer
in interface Container<SELF extends GenericContainer<SELF>>
consumer
- consumer that output frames should be sent topublic com.github.dockerjava.api.model.Info fetchDockerDaemonInfo() throws java.io.IOException
fetchDockerDaemonInfo
in interface Container<SELF extends GenericContainer<SELF>>
java.io.IOException
public Container.ExecResult execInContainer(java.lang.String... command) throws java.lang.UnsupportedOperationException, java.io.IOException, java.lang.InterruptedException
execInContainer
in interface Container<SELF extends GenericContainer<SELF>>
java.lang.UnsupportedOperationException
java.io.IOException
java.lang.InterruptedException
ExecInContainerPattern.execInContainer(com.github.dockerjava.api.command.InspectContainerResponse, String...)
public void copyFileToContainer(MountableFile mountableFile, java.lang.String containerPath)
copyFileToContainer
in interface Container<SELF extends GenericContainer<SELF>>
mountableFile
- file or directory which is copied into the containercontainerPath
- destination path inside the containerpublic void copyFileToContainer(Transferable transferable, java.lang.String containerPath)
Container
copyFileToContainer
in interface Container<SELF extends GenericContainer<SELF>>
transferable
- file which is copied into the containercontainerPath
- destination path inside the containerpublic void copyFileFromContainer(java.lang.String containerPath, java.lang.String destinationPath)
copyFileFromContainer
in interface Container<SELF extends GenericContainer<SELF>>
containerPath
- path to file which is copied from containerdestinationPath
- destination path to which file is copied with file namepublic <T> T copyFileFromContainer(java.lang.String containerPath, ThrowingFunction<java.io.InputStream,T> consumer)
Container
copyFileFromContainer
in interface Container<SELF extends GenericContainer<SELF>>
containerPath
- path to file which is copied from containerconsumer
- function that takes InputStream of the copied filepublic Container.ExecResult execInContainer(java.nio.charset.Charset outputCharset, java.lang.String... command) throws java.lang.UnsupportedOperationException, java.io.IOException, java.lang.InterruptedException
execInContainer
in interface Container<SELF extends GenericContainer<SELF>>
java.lang.UnsupportedOperationException
java.io.IOException
java.lang.InterruptedException
ExecInContainerPattern.execInContainer(com.github.dockerjava.api.command.InspectContainerResponse, Charset, String...)
public SELF withStartupAttempts(int attempts)
attempts
- number of attemptspublic SELF withCreateContainerCmdModifier(java.util.function.Consumer<com.github.dockerjava.api.command.CreateContainerCmd> modifier)
CreateContainerCmd
after it was pre-configured in tryStart(Profiler)
.
Invocation happens eagerly on a moment when container is created.
Warning: this does expose the underlying docker-java API so might change outside of our control.modifier
- Consumer
of CreateContainerCmd
.