MockServer is available as a docker container that allows you to easily run MockServer as a separate container on any environment without having to install Java or any other libraries. The docker container fully encapsulates all requirements required to run MockServer (such as Java) and separates the running MockServer instance from all other parts of the system.

MockServer docker container can be found at MockServer Docker

Running MockServer Docker Container

The typical sequence for running the MockServer docker image is as follows:
  1. Install Docker
  2. Pull (or Update) Image
  3. Run Container
In addition it is possible to customise how the container is run.  

Install Docker

To install Docker see the installation instructions.

 

Pull MockServer Image

To pull the MockServer Docker image use the pull command, as follows:

docker pull jamesdbloom/mockserver

This is not strictly necessary as the image will be automatically pulled if it does not exist when the run command is used. However, using the pull command will ensure the latest version of the image is downloaded.

 

Run MockServer Container

Then to run MockServer as a Docker container run the following command:

docker run -d -P jamesdbloom/mockserver

The -P switch in this command tells Docker to map all ports exported by the MockServer container to dynamically allocated ports on the host machine.

To view information about the MockServer container, including which dynamic ports have been used run the following command:

docker ps
 

Configure Port Mapping

This MockServer docker container exports the following port:

  • serverPort 1080

To specify which ports (on the host machine) should be mapped to the MockServer docker container use the -p <host port>:<container port> option, as follows:

docker run -d -p <serverPort>:1080 jamesdbloom/mockserver

For example:

docker run -d -p 1080:1080 jamesdbloom/mockserver

Modifying Default Command

By default when the MockServer container runs it executes a bash script passing three command line options, as follows

/opt/mockserver/run_mockserver.sh -serverPort 1080 -logLevel INFO

It is possible to pass an alternative command line to the container, by pre-pending the command to the end of the run command, as follows:

docker run -d -p 1080:1080 jamesdbloom/mockserver /opt/mockserver/run_mockserver.sh -serverPort 1080 -logLevel INFO

For following command can be used to view the available command line switches:

docker run jamesdbloom/mockserver /opt/mockserver/run_mockserver.sh

   Error: At least 'serverPort' must be provided

   run_mockserver.sh -serverPort <port> [-proxyRemotePort <port>] [-proxyRemoteHost <hostname>] [-logLevel <level>] [-jvmOptions <system parameters>]

     valid options are:

        -serverPort <port>                      The HTTP, HTTPS, SOCKS and HTTP CONNECT
                                                port(s) for both mocking and proxying
                                                requests.  Port unification is used to
                                                support all protocols for proxying and
                                                mocking on the same port(s). Supports
                                                comma separated list for binding to
                                                multiple ports.

        -proxyRemotePort <port>                 Optionally enables port forwarding mode.
                                                When specified all requests received will
                                                be forwarded to the specified port, unless
                                                they match an expectation.

        -proxyRemoteHost <hostname>             Specified the host to forward all proxy
                                                requests to when port forwarding mode has
                                                been enabled using the proxyRemotePort
                                                option.  This setting is ignored unless
                                                proxyRemotePort has been specified. If no
                                                value is provided for proxyRemoteHost when
                                                proxyRemotePort has been specified,
                                                proxyRemoteHost will default to \"localhost\".

        -logLevel <level>                       Optionally specify log level as TRACE, DEBUG,
                                                INFO, WARN, ERROR or OFF. If not specified
                                                default is INFO

        -jvmOptions <system parameters>  Specified generic JVM options or system properties.

   i.e. /opt/mockserver/run_mockserver.sh -serverPort 1080,1081 -proxyRemotePort 80 -proxyRemoteHost www.mock-server.com -jvmOptions -logLevel INFO "-Dmockserver.enableCORSForAllResponses=true -Dmockserver.sslSubjectAlternativeNameDomains='org.mock-server.com,mock-server.com'"

Then the appropriate options can be specified, for example, to setup a port forwarding proxy (from 0.0.0.0:1080 to www.mock-server.com:80) using the following command:

docker run -d -p 1080:1080 jamesdbloom/mockserver /opt/mockserver/run_mockserver.sh -serverPort 1080 -proxyRemotePort 80 -proxyRemoteHost www.mock-server.com

Interactive Shell

It is possible to launch the container with an interactive bash shell as follows:

docker run -it -p 1080:1080 jamesdbloom/mockserver /bin/bash

Note: in this example above the -d flag (for daemon) has been replaced with -i (to stdin open) and -t (for pseudo-tty) to ensure docker creates the container in the foreground with an attached stdin, see the docker documentation for more details.