Public | Automated Build

Last pushed: a year ago
Short Description
Creates a mock API date from a JSON file.
Full Description

docker-mock-json-server

JSON Server provides REST API mocking based on plain JSON.
This is a docker image that eases setup.

Usage

Create a "db.json" file on your desktop (which is your mock data).
Make sure docker is running on your machine and run the following set of commands:

  1. Pulls the latest docker image:

    $ docker pull suhailtaj/mock-api
    
  2. Run the docker image from your desktop (db.json file)

    $ docker run -d -p 4000:9090 -v ~/Desktop/db.json:/data/db.json  suhailtaj/mock-api
    

Now that your your REST API server should be started on port 4000.

To view the served API, open the following URL in your browser:
http://localhost:4000

This is a rather common setup following docker's conventions:

  • -d will run a detached instance in the background
  • -p {OutsidePort}:9090 will bind the webserver to the given outside port
  • -v {AbsolutePathToJsonFile}:/data/db.json should be passed to mount the given JSON file into the container
  • suhailtaj/mock-api the name of this docker image

Help

You can supply any number of JSON Server arguments that will be passed through unmodified.

$ docker run -it --rm suhailtaj/mock-api --help

JSON source

If you mount a file to /data/db.json (as in the above example),
it will automatically be used as the plain JSON data source file.

A sample file could look like this:

{
  "posts": [
    { "id": 1, "body": "foo" },
    { "id": 2, "body": "bar" }
  ],
  "comments": [
    { "id": 1, "body": "baz", "postId": 1 },
    { "id": 2, "body": "qux", "postId": 2 }
  ]
}

JS seed file

If you mount a file to /data/file.js,
it will automatically be used as a JS seed file.

JSON Server expects JS files to export a function that returns an object.
Seed files are useful if you need to programmaticaly create a lot of data.

A sample file could look like this:

module.exports = function() {
  var data = {};

  data.posts = [];
  data.posts.push({ id: 1, body: 'foo' });
  //...

  return data;
}
Docker Pull Command
Owner
suhailtaj
Source Repository

Comments (0)