Class MongoDbContainer
- All Implemented Interfaces:
AutoCloseable,org.junit.rules.TestRule,org.testcontainers.containers.Container<MongoDbContainer>,org.testcontainers.containers.ContainerState,org.testcontainers.containers.traits.LinkableContainer,org.testcontainers.containers.wait.strategy.WaitStrategyTarget,org.testcontainers.lifecycle.Startable
mongod or mongos process.
In order to interact with a running container from the host running Docker Desktop using a client driver, the
container's network alias (name) must be resolvable from the host. On most systems this will require
configuring /etc/hosts to have an entry that maps name to 127.0.0.1. Fixed ports are used on
the host and are mapped 1:1 exactly with the container. Random free ports are assigned to minimize the chance of
conflicts.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic final classNested classes/interfaces inherited from interface org.testcontainers.containers.Container
org.testcontainers.containers.Container.ExecResult -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final org.testcontainers.utility.DockerImageNamestatic final StringDefault should matchversion.mongo.serverin parentpom.xml.private static final org.slf4j.Loggerprivate final Stringprivate static final org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapperprivate final intprivate final PortResolverprivate final StringFields inherited from class org.testcontainers.containers.GenericContainer
CONTAINER_RUNNING_TIMEOUT_SEC, dependencies, dockerClient, INTERNAL_HOST_HOSTNAME, waitStrategyFields inherited from interface org.testcontainers.containers.ContainerState
STATE_HEALTHY -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidcheckExitCode(org.testcontainers.containers.Container.ExecResult result) private voidprotected voidcontainerIsStarted(com.github.dockerjava.api.command.InspectContainerResponse containerInfo) protected voidcontainerIsStopped(com.github.dockerjava.api.command.InspectContainerResponse containerInfo) private voiddockerCommand(Function<com.github.dockerjava.api.DockerClient, com.github.dockerjava.api.command.SyncDockerCmd<?>> action) org.testcontainers.shaded.com.fasterxml.jackson.databind.JsonNodeorg.testcontainers.containers.Container.ExecResultexecMongoInContainer(String... command) org.testcontainers.containers.Container.ExecResultexecMongoScriptInContainer(org.testcontainers.utility.MountableFile file, String containerPath) Uploads given file to container and executes is as mongodb javascriptReturns the public address that should be used by clients.Returns the named-address that is only guaranteed available within the network.private StringvoidinitReplicaSet(boolean configServer, MongoDbContainer.Address... Addresses) Invokes rs.initiate on the container.private static booleanisLegacy()voidkill()static MongoDbContainer.Buildernode()private static org.testcontainers.shaded.com.fasterxml.jackson.databind.JsonNodeparseResponse(String stdout) voidpause()voidstepDown()Invokes rs.stepDown on the container to instruct the primary of the replica set to become the primary.voidunpause()Methods inherited from class org.testcontainers.containers.GenericContainer
addEnv, addExposedPort, addExposedPorts, addFileSystemBind, addFixedExposedPort, addFixedExposedPort, addLink, apply, canBeReused, configure, containerIsCreated, containerIsStarted, containerIsStarting, containerIsStarting, containerIsStopping, copyFileFromContainer, createVolumeDirectory, dependsOn, dependsOn, dependsOn, doStart, equals, failed, finished, getBinds, getCommandParts, getContainerId, getContainerInfo, getContainerName, getCopyToFileContainerPathMap, getCreateContainerCmdModifiers, getDependencies, getDockerClient, getDockerImageName, getEnv, getEnvMap, getExposedPorts, getExtraHosts, getImage, getIpAddress, getLabels, getLinkedContainers, getLivenessCheckPort, getLivenessCheckPortNumbers, getLivenessCheckPorts, getLogConsumers, getNetwork, getNetworkAliases, getNetworkMode, getPortBindings, getShmSize, getStartupAttempts, getStartupCheckStrategy, getTestHostIpAddress, getTmpFsMapping, getVolumesFroms, getWaitStrategy, getWorkingDirectory, hashCode, isHostAccessible, isPrivilegedMode, isShouldBeReused, logger, setBinds, setCommand, setCommand, setCommandParts, setCopyToFileContainerPathMap, setDockerImageName, setEnv, setExposedPorts, setExtraHosts, setHostAccessible, setImage, setLabels, setLinkedContainers, setLogConsumers, setNetwork, setNetworkAliases, setNetworkMode, setPortBindings, setPrivilegedMode, setShmSize, setStartupAttempts, setStartupCheckStrategy, setTmpFsMapping, setVolumesFroms, setWaitStrategy, setWorkingDirectory, start, starting, stop, succeeded, toString, waitingFor, waitUntilContainerStarted, withAccessToHost, withClasspathResourceMapping, withClasspathResourceMapping, withCommand, withCommand, withCopyFileToContainer, withCopyToContainer, withCreateContainerCmdModifier, withEnv, withEnv, withExposedPorts, withExtraHost, withFileSystemBind, withImagePullPolicy, withLabel, withLabels, withLogConsumer, withMinimumRunningDuration, withNetwork, withNetworkAliases, withNetworkMode, withPrivilegedMode, withReuse, withSharedMemorySize, withStartupAttempts, withStartupCheckStrategy, withStartupTimeout, withTmpFs, withVolumesFrom, withWorkingDirectoryMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.testcontainers.containers.Container
addFileSystemBind, followOutput, followOutput, self, withEnv, withFileSystemBindMethods inherited from interface org.testcontainers.containers.ContainerState
copyFileFromContainer, copyFileToContainer, copyFileToContainer, execInContainer, execInContainer, getBoundPortNumbers, getContainerIpAddress, getCurrentContainerInfo, getFirstMappedPort, getHost, getLogs, getLogs, getMappedPort, isCreated, isHealthy, isRunningMethods inherited from interface org.testcontainers.lifecycle.Startable
close
-
Field Details
-
LOGGER
private static final org.slf4j.Logger LOGGER -
IMAGE_VERSION
Default should matchversion.mongo.serverin parentpom.xml. -
IMAGE_NAME
private static final org.testcontainers.utility.DockerImageName IMAGE_NAME -
OBJECT_MAPPER
private static final org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper OBJECT_MAPPER -
name
-
port
private final int port -
replicaSet
-
portResolver
-
-
Constructor Details
-
MongoDbContainer
-
-
Method Details
-
node
-
getClientAddress
Returns the public address that should be used by clients.Must be called after
GenericContainer.start()since the public address may not be available on all platforms before then.- Returns:
- the host-addressable address
-
getNamedAddress
Returns the named-address that is only guaranteed available within the network.Can always be called before
GenericContainer.start()safely. Useful for intra-cluster addressing that must be configured before containers are running and the addresses are not available (e.g. in the constructor of a replica set or sharded cluster).- Returns:
- the name-addressable network address
-
initReplicaSet
Invokes rs.initiate on the container.- Parameters:
configServer- whether this replica set is a used for a sharded cluster's config server.Addresses- the list of hostname / port numbers of the set members
-
execMongoScriptInContainer
public org.testcontainers.containers.Container.ExecResult execMongoScriptInContainer(org.testcontainers.utility.MountableFile file, String containerPath) Uploads given file to container and executes is as mongodb javascript- Parameters:
file- file to be uploadedcontainerPath- path in the container- Returns:
- execution result
-
stepDown
public void stepDown()Invokes rs.stepDown on the container to instruct the primary of the replica set to become the primary. -
kill
public void kill() -
pause
public void pause() -
unpause
public void unpause() -
getNetworkIp
-
dockerCommand
private void dockerCommand(Function<com.github.dockerjava.api.DockerClient, com.github.dockerjava.api.command.SyncDockerCmd<?>> action) -
execMongoInContainer
public org.testcontainers.containers.Container.ExecResult execMongoInContainer(String... command) throws IOException, InterruptedException - Throws:
IOExceptionInterruptedException
-
eval
-
parseResponse
private static org.testcontainers.shaded.com.fasterxml.jackson.databind.JsonNode parseResponse(String stdout) -
checkExitCode
private void checkExitCode(org.testcontainers.containers.Container.ExecResult result) -
checkStarted
private void checkStarted() -
containerIsStarted
protected void containerIsStarted(com.github.dockerjava.api.command.InspectContainerResponse containerInfo) - Overrides:
containerIsStartedin classorg.testcontainers.containers.GenericContainer<MongoDbContainer>
-
containerIsStopped
protected void containerIsStopped(com.github.dockerjava.api.command.InspectContainerResponse containerInfo) - Overrides:
containerIsStoppedin classorg.testcontainers.containers.GenericContainer<MongoDbContainer>
-
isLegacy
private static boolean isLegacy()
-