Skip to main content
Version: 4.0.0

Docker image

Preface

Mocks Server can be also executed as a Docker container easily using the available public images. Docker images contain all of the needed dependencies and automatically start Mocks Server when executed.

Prerequisites

  • You must have Docker installed

Docker images

ImageDocker HubNotes
mainmocksserver/mainIncludes the @mocks-server/main distribution, with all official plugins installed
info

For information about how images are versioned, you can read the Releases docs

Quick start

You can start a mocksserver/main Docker container by running:

docker run -ti -p 3100:3100 -p 3110:3110 mocksserver/main

This will start Mocks Server, and, as no /mocks folder nor the configuration file were provided, it will create a scaffold. If you hit the next URLs you'll see:

Providing routes, collections and config file

The Mocks Server application in the Docker container is pre-configured to search for the mocks folder (which contains routes and collections) and the configuration file in the /input folder. So, you can follow all of the guidelines about organizing files described in these docs, and simply mount the same structure in the /input folder of the Docker container.

Let's assume that you have the next file tree:

project/
├── mocks/
│ ├── routes/ <- DEFINE YOUR ROUTES HERE
│ │ ├── common.js
│ │ └── users.js
│ └── collections.json <- DEFINE YOUR COLLECTIONS HERE
└── mocks.config.js <- DEFINE YOUR CONFIGURATION HERE

Then, you can mount your /project folder as /input folder in the container:

docker run -ti -p 3100:3100 -p 3110:3110 \
-v /Users/foo/project:/input \
mocksserver/main

Now, Mocks Server will find your routes, collections and configuration file, and it will start your mock server!

Configuration

As described in the How to change settings docs page, environment variables can be used to change the Mocks Server configuration. So, simply pass the corresponding environment variables to the container:

docker run -ti -p 3100:3100 -p 3110:3110 \
-v /Users/foo/project:/input \
-e MOCKS_LOG=debug
mocksserver/main

The Docker image includes some pre-configuration to make easier to use the app through Docker, but you can change these options using environment variables as well in case you want to customize your container:

  • MOCKS_PLUGINS_INQUIRER_CLI_ENABLED=false. The interactive CLI is disabled by default
  • MOCKS_FILES_PATH=/input/mocks. The files path is set as /input/mocks by default.
  • MOCKS_CONFIG_FILE_SEARCH_FROM=/input. The configuration file is expected to be in the /input folder.
caution

When setting paths in your configuration, take into account that the application in the container is running in the /usr/app folder, so, that is the process.cwd used for resolving relative paths. So, using absolute paths when defining custom configuration is recommended.

Building a self-contained image

You can also build a self-contained image, containing both the Mocks Server app and your routes, collections and configuration files. This makes your mock portable to wherever Docker runs.

Let's assume the following file structure:

project/
├── mocks/
│ ├── routes/ <- DEFINE YOUR ROUTES HERE
│ │ ├── common.js
│ │ └── users.js
│ └── collections.json <- DEFINE YOUR COLLECTIONS HERE
├── Dockerfile
└── mocks.config.js <- DEFINE YOUR CONFIGURATION HERE

The Dockerfile should contain:

FROM mocksserver/main
COPY ./mocks /input/mocks/
COPY ./mocks.config.js /input/mocks.config.js

Now, you could build your container image as follows:

docker build -t mock:test .

The created container image contains both the Mocks Server app, your mocks folder, and your mocks.config.js file.

Run the container:

docker run -ti -p 3100:3100 -p 3110:3110 mock:test

Now your API mock is available at http://localhost:3100, and the administration REST API is running at http://localhost:3110. 🎉