public class GenericContainer extends FailureDetectingExternalResource implements LinkableContainer
Modifier and Type | Field and Description |
---|---|
protected String |
containerId |
protected String |
containerName |
protected com.github.dockerjava.api.DockerClient |
dockerClient |
Constructor and Description |
---|
GenericContainer() |
GenericContainer(String dockerImageName) |
Modifier and Type | Method and Description |
---|---|
void |
addEnv(String key,
String value)
Add an environment variable to be passed to the container.
|
void |
addExposedPort(Integer port) |
void |
addExposedPorts(int... ports) |
void |
addFileSystemBind(String hostPath,
String containerPath,
BindMode mode) |
void |
addLink(LinkableContainer otherContainer,
String alias) |
protected void |
configure() |
protected void |
containerIsStarted(com.github.dockerjava.api.command.InspectContainerResponse containerInfo) |
protected void |
containerIsStarting(com.github.dockerjava.api.command.InspectContainerResponse containerInfo) |
protected Path |
createVolumeDirectory(boolean temporary)
Creates a directory on the local filesystem which will be mounted as a volume for the container.
|
protected void |
finished(org.junit.runner.Description description) |
String |
getHostIpAddress()
Get the IP address that containers (e.g.
|
String |
getIpAddress()
Get the IP address that this container may be reached on (may not be the local machine).
|
protected Integer |
getLivenessCheckPort() |
Integer |
getMappedPort(int originalPort)
Get the actual mapped port for a given port exposed by the container.
|
Boolean |
isRunning() |
protected org.slf4j.Logger |
logger()
Provide a logger that references the docker image name.
|
void |
setCommand(String... commandParts)
Set the command that should be run in the container
|
void |
setCommand(String command)
Set the command that should be run in the container
|
void |
setDockerImageName(String dockerImageName) |
void |
start()
Starts the container using docker, pulling an image if necessary.
|
protected void |
starting(org.junit.runner.Description description) |
void |
stop()
Stops the container.
|
protected void |
waitForListeningPort(String ipAddress,
Integer port)
Waits for a port to start listening for incoming connections.
|
protected void |
waitUntilContainerStarted()
Wait until the container has started.
|
GenericContainer |
withClasspathResourceMapping(String resourcePath,
String containerPath,
BindMode mode)
Map a resource (file or directory) on the classpath to a path inside the container
|
GenericContainer |
withCommand(String... commandParts)
Set the command that should be run in the container
|
GenericContainer |
withCommand(String cmd)
Set the command that should be run in the container
|
GenericContainer |
withEnv(String key,
String value)
Add an environment variable to be passed to the container.
|
GenericContainer |
withExposedPorts(Integer... ports)
Set the ports that this container listens on
|
apply, failed, succeeded
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getContainerName
protected com.github.dockerjava.api.DockerClient dockerClient
protected String containerId
protected String containerName
public GenericContainer()
public GenericContainer(@NonNull String dockerImageName)
public void start()
public void stop()
protected org.slf4j.Logger logger()
protected Path createVolumeDirectory(boolean temporary) throws IOException
temporary
- is the volume directory temporary? If true, the directory will be deleted on JVM shutdown.IOException
protected void configure()
protected void containerIsStarting(com.github.dockerjava.api.command.InspectContainerResponse containerInfo)
protected void containerIsStarted(com.github.dockerjava.api.command.InspectContainerResponse containerInfo)
protected Integer getLivenessCheckPort()
protected void waitUntilContainerStarted()
protected void waitForListeningPort(String ipAddress, Integer port)
ipAddress
- the IP address to attempt to connect toport
- the port which will start accepting connectionspublic void setCommand(@NonNull String command)
command
- a command in single string format (will automatically be split on spaces)public void setCommand(@NonNull String... commandParts)
commandParts
- a command as an array of string partspublic void addEnv(String key, String value)
key
- environment variable keyvalue
- environment variable valuepublic void addFileSystemBind(String hostPath, String containerPath, BindMode mode)
public void addLink(LinkableContainer otherContainer, String alias)
public void addExposedPort(Integer port)
public void addExposedPorts(int... ports)
protected void starting(org.junit.runner.Description description)
starting
in class FailureDetectingExternalResource
protected void finished(org.junit.runner.Description description)
finished
in class FailureDetectingExternalResource
public GenericContainer withExposedPorts(Integer... ports)
ports
- an array of TCP portspublic GenericContainer withEnv(String key, String value)
key
- environment variable keyvalue
- environment variable valuepublic GenericContainer withCommand(String cmd)
cmd
- a command in single string format (will automatically be split on spaces)public GenericContainer withCommand(String... commandParts)
commandParts
- a command as an array of string partspublic GenericContainer withClasspathResourceMapping(String resourcePath, 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 filepublic String getIpAddress()
public Boolean isRunning()
public Integer getMappedPort(int originalPort)
originalPort
- the original TCP port that is exposedpublic void setDockerImageName(@NonNull String dockerImageName)
public String getHostIpAddress()
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://" + getHostIpAddress() + ":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.
Copyright © 2016. All rights reserved.