Class MongoDbContainer

java.lang.Object
org.testcontainers.containers.FailureDetectingExternalResource
org.testcontainers.containers.GenericContainer<MongoDbContainer>
io.debezium.testing.testcontainers.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

public class MongoDbContainer extends org.testcontainers.containers.GenericContainer<MongoDbContainer>
A container for running a single MongoDB 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 Classes
    Modifier and Type
    Class
    Description
    static class 
     
    static final class 
     

    Nested classes/interfaces inherited from interface org.testcontainers.containers.Container

    org.testcontainers.containers.Container.ExecResult
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final org.testcontainers.utility.DockerImageName
     
    static final String
    Default should match version.mongo.server in parent pom.xml.
    private static final org.slf4j.Logger
     
    private final String
     
    private static final org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper
     
    private final int
     
    private final PortResolver
     
    private final String
     

    Fields inherited from class org.testcontainers.containers.GenericContainer

    CONTAINER_RUNNING_TIMEOUT_SEC, dependencies, dockerClient, INTERNAL_HOST_HOSTNAME, waitStrategy

    Fields inherited from interface org.testcontainers.containers.ContainerState

    STATE_HEALTHY
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
     
  • Method Summary

    Modifier and Type
    Method
    Description
    private void
    checkExitCode(org.testcontainers.containers.Container.ExecResult result)
     
    private void
     
    protected void
    containerIsStarted(com.github.dockerjava.api.command.InspectContainerResponse containerInfo)
     
    protected void
    containerIsStopped(com.github.dockerjava.api.command.InspectContainerResponse containerInfo)
     
    private void
    dockerCommand(Function<com.github.dockerjava.api.DockerClient,com.github.dockerjava.api.command.SyncDockerCmd<?>> action)
     
    org.testcontainers.shaded.com.fasterxml.jackson.databind.JsonNode
    eval(String command)
     
    org.testcontainers.containers.Container.ExecResult
     
    org.testcontainers.containers.Container.ExecResult
    execMongoScriptInContainer(org.testcontainers.utility.MountableFile file, String containerPath)
    Uploads given file to container and executes is as mongodb javascript
    Returns the public address that should be used by clients.
    Returns the named-address that is only guaranteed available within the network.
    private String
     
    void
    initReplicaSet(boolean configServer, MongoDbContainer.Address... Addresses)
    Invokes rs.initiate on the container.
    private static boolean
     
    void
     
     
    private static org.testcontainers.shaded.com.fasterxml.jackson.databind.JsonNode
     
    void
     
    void
    Invokes rs.stepDown on the container to instruct the primary of the replica set to become the primary.
    void
     

    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, withWorkingDirectory

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface org.testcontainers.containers.Container

    addFileSystemBind, followOutput, followOutput, self, withEnv, withFileSystemBind

    Methods inherited from interface org.testcontainers.containers.ContainerState

    copyFileFromContainer, copyFileToContainer, copyFileToContainer, execInContainer, execInContainer, getBoundPortNumbers, getContainerIpAddress, getCurrentContainerInfo, getFirstMappedPort, getHost, getLogs, getLogs, getMappedPort, isCreated, isHealthy, isRunning

    Methods inherited from interface org.testcontainers.lifecycle.Startable

    close
  • Field Details

    • LOGGER

      private static final org.slf4j.Logger LOGGER
    • IMAGE_VERSION

      public static final String IMAGE_VERSION
      Default should match version.mongo.server in parent pom.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

      private final String name
    • port

      private final int port
    • replicaSet

      private final String replicaSet
    • portResolver

      private final PortResolver portResolver
  • Constructor Details

  • Method Details

    • node

      public static MongoDbContainer.Builder node()
    • getClientAddress

      public MongoDbContainer.Address 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

      public MongoDbContainer.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

      public void initReplicaSet(boolean configServer, MongoDbContainer.Address... Addresses)
      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 uploaded
      containerPath - 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

      private String 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:
      IOException
      InterruptedException
    • eval

      public org.testcontainers.shaded.com.fasterxml.jackson.databind.JsonNode eval(String command)
    • 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:
      containerIsStarted in class org.testcontainers.containers.GenericContainer<MongoDbContainer>
    • containerIsStopped

      protected void containerIsStopped(com.github.dockerjava.api.command.InspectContainerResponse containerInfo)
      Overrides:
      containerIsStopped in class org.testcontainers.containers.GenericContainer<MongoDbContainer>
    • isLegacy

      private static boolean isLegacy()