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 ContainerThe typical sequence for running the MockServer docker image is as follows: customise how the container is run.
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:
Configure Port Mapping
This MockServer docker container exports the following ports:
- serverPort 1080
- proxyPort 1090
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 -p <proxyPort>:1090 jamesdbloom/mockserver
docker run -d -p 1080:1080 -p 1090:1090 jamesdbloom/mockserver
Only specify the required ports, for example, if you are not using the proxy there is no need to provide port mapping options for the proxy ports, as follows:
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 -logLevel INFO -serverPort 1080 -proxyPort 1090
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 -p 1090:1090 jamesdbloom/mockserver /opt/mockserver/run_mockserver.sh -logLevel INFO -serverPort 1080 -proxyPort 1090
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' or 'proxyPort' must be provided run_mockserver.sh [-logLevel <level>] \ [-serverPort <port>] \ [-proxyPort <port>] \ [-proxyRemotePort <port>] \ [-proxyRemoteHost <hostname>] valid options are: -logLevel <level> OFF, ERROR, WARN, INFO, DEBUG, TRACE or ALL, as follows: WARN - exceptions and errors INFO - all interactions -serverPort <port> Specifies the HTTP and HTTPS port for the MockServer. Port unification is used to support HTTP and HTTPS on the same port -proxyPort <port> Specifies the HTTP, HTTPS, SOCKS and HTTP CONNECT port for proxy. Port unification supports for all protocols on the same port -proxyRemotePort <port> Specifies the port to forward all proxy requests to (i.e. all requests received on portPort). This setting is used to enable the port forwarding mode therefore this option disables the HTTP, HTTPS, SOCKS and HTTP CONNECT support -proxyRemoteHost <hostname> Specified the host to forward all proxy requests to (i.e. all requests received on portPort). 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". -genericJVMOptions <system parameters> Specified generic JVM options or system properties. i.e. /opt/mockserver/run_mockserver.sh -logLevel INFO -serverPort 1080 -proxyPort 1090 -proxyRemotePort 80 -proxyRemoteHost www.mock-server.com -genericJVMOptions "-Dmockserver.enableCORSForAllResponses=true -Dmockserver.sslSubjectAlternativeNameDomains='org.mock-server.com,mock-server.com'"
Then the appropriate options can be specified, for example, to setup a direct proxy (from 0.0.0.0:1090 to www.mock-server.com:80) using the following command:
docker run -d -p 1090:1090 jamesdbloom/mockserver /opt/mockserver/run_mockserver.sh -proxyPort 1090 -proxyRemotePort 80 -proxyRemoteHost www.mock-server.com
It is possible to launch the container with an interactive bash shell as follows:
docker run -it -p 1080:1080 -p 1090:1090 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.