Sign inSign up

codfish/json-server

By codfish

Updated 1 day ago

Docker image for simple json-server apps. Simply mount in your database, routes & middleware files.

Image
0

100K+

codfish/json-server repository overview

json-server-docker

Docker image for building a json-server application.

This image will set up the application for you and allow you to simply mount in your "database source file", which by convention needs to be named db.js. You can also optionally mount in a middleware.js and routes.json file.

Image versions are based off of the release versions for json-server. However there is not an image for every version. See the available versions here.

Usage

The container runs the json-server cli 2 ways. By default it will run it directly but you can easily override the command by running it through nodemon. The image is setup to do this already through npm. Just change the command to npm run dev.

version: '3'

services:
  json-server:
    image: codfish/json-server
    command: npm run dev
    volumes:
      - ./db.js:/app/db.js
      - ./routes.json:/app/routes.json:delegated
      - ./middleware.js:/app/middleware.js:delegated

To test this image directly you can run:

git clone [email protected]:codfish/json-server-docker.git
cd json-server-docker
docker-compose up -d

I highly recommend installing dotdocker first. The container will then be accessible at http://json-server.docker.

Options

json-server cli options: https://github.com/typicode/json-server#cli-usage

For the most part this image will rely on the default options that json-server has set. For certain options like --port and --host, it overrides the defaults with it's own defaults that work well with containers out of the box. We've also set a default routes & middleware filename so all you need to do is mount over the files. No configuration is necessary for that.

However you still have the ability to override almost every option yourself as well. Options should be passed in as environment variables. The currently supported options available for:

OptionDescriptionDefault
PORTSet port80
HOSTSet host0.0.0.0
ROUTESPath to routes fileroutes.json (Stored in image, optionally mount over it)
MIDDLEWARESPath to middleware filemiddleware.js (Stored in image, optionally mount over it)
CONFIGPath to config fileDefers to json-server default
IDSet database id property (e.g. address)Defers to json-server default
FKSSet foreign key suffix, (e.g. Address as in contractAddress)Defers to json-server default
DELAYAdd delay to responses (ms)-
STATICSet static files directoryDefers to json-server default
QUIETSuppress log messages from outputBoolean flag only true if set
NO_GZIPDisable GZIP Content-EncodingBoolean flag only true if set
NO_CORSDisable Cross-Origin Resource SharingBoolean flag only true if set
READ_ONLYAllow only GET requestsBoolean flag only true if set

For details on the options view json-server's documentation.

Full Example

Here's a recommended setup for local development with some optional overrides as an example.

services:
  json-server:
    image: codfish/json-server
    command: npm run dev
    volumes:
      - ./db.js:/app/db.js:delegated
      - ./routes.json:/app/routes.json:delegated
      - ./middleware.js:/app/middleware.js:delegated
    environment:
      VIRTUAL_HOST: json-server.docker
      FKS: Address
      ID: address
      NO_CORS: 'true'
      NO_GZIP: 'true'

Database File

When building your mock api's you'll most like want to generate some fake data and return a number of items for a specific collection. I've included Lodash & faker.js in the image to help facilitate doing these sorts of things inside your source file, or middleware for that matter. Here's an example of what I mean:

const faker = require('faker');
const times = require('lodash/times');
const startCase = require('lodash/startCase');

module.exports = () => ({
  posts: times(100, index => ({
    id: index,
    title: startCase(faker.lorem.words(3)),
    body: faker.lorem.paragraphs(3),
    // and so on...
  })),
});

Maintaining/Contributing

  • Bump version of json-server in Dockerfile
  • Bump node dependencies
  • Test it out
docker-compose up -d --build

Check out http://json-server.docker. Update db.js, routes.json, or middleware.js to test out functionality. Changes should propagate automatically, just refresh the page.

Releasing

New version:

git tag -f -m 'v0.17.0' v0.17.0
git push origin v0.17.0

Updating old version

We keep our versions in sync with json-server. This scenario would happen if there's a bug fix or feature change with our implementation but the json-server version doesn't change.

git tag -fa v0.16.1 -m "Update v0.16.1 tag" && git push origin v0.16.1 --force

Docker Hub is configured to automatically build on new tags pushed to GitHub.

Todo

  • Add examples of using this with docker cli, without a compose file

Tag summary

Content type

Image

Digest

sha256:2a79c9253

Size

108.6 MB

Last updated

7 months ago

docker pull codfish/json-server