Docker
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: 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 mockserver/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 --rm -P mockserver/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 --rm -p <serverPort>:1080 mockserver/mockserver
For example:
docker run -d --rm -p 1080:1080 mockserver/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 alternative arguments to the entrypoint for the container, by appending arguments to the end of the run command, as follows:
docker run -d --rm -p 1080:1080 mockserver/mockserver -serverPort 1080 -logLevel INFO
For following command can be used to view the available command line switches:
docker run mockserver/mockserver ""
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 using SLF4J levels:
TRACE, DEBUG, INFO, WARN, ERROR, OFF or Java
Logger levels: FINEST, FINE, INFO, WARNING,
SEVERE 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 --rm -p 1080:1080 mockserver/mockserver -serverPort 1080 -proxyRemotePort 80 -proxyRemoteHost www.mock-server.com
Interactive Shell
It is possible to launch the container with an interactive bash shell by modifying the entrypoint as follows:
docker run -it -p 1080:1080 --entrypoint "/bin/bash" mockserver/mockserver
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.
Docker Compose
MockServer can be run using docker compose by adding the container as a service.
The MockServer container uses an entrypoint, so it is possible to configure the MockServer by specifying the command line flags using by specifying the command, as follows:
version: "2.4"
services:
mockServer:
image: mockserver/mockserver:mockserver-5.9.0
command: -logLevel DEBUG -serverPort 1090 -proxyRemotePort 80 -proxyRemoteHost www.mock-server.com -jvmOptions "-Dmockserver.sslSubjectAlternativeNameDomains='org.mock-server.com,mock-server.com'"
ports:
- 1080:1090
It is also possible to configure the MockServer by setting environment variables, as follows:
version: "2.4"
services:
mockServer:
image: mockserver/mockserver:mockserver-5.9.0
ports:
- 1080:1090
environment:
LOG_LEVEL: "DEBUG"
SERVER_PORT: 1090
PROXY_REMOTE_PORT: 80
PROXY_REMOTE_HOST: www.mock-server.com
JVM_OPTIONS: -Dmockserver.sslSubjectAlternativeNameDomains='org.mock-server.com,mock-server.com'
It is also possible to configure the MockServer by mounting a volume containing a properties file or JSON expectation initializer, as follows:
version: "2.4"
services:
mockServer:
image: mockserver/mockserver:mockserver-5.9.0
ports:
- 1080:1080
environment:
MOCKSERVER_PROPERTY_FILE: /config/mockserver.properties
MOCKSERVER_INITIALIZATION_JSON_PATH: /config/initializerJson.json
volumes:
- type: bind
source: .
target: /config