Repository files navigation

json-server-docker

Dockerized json-server for building a full fake RESTful API.

json-server versionpullsMIT License

Created with <3 for front-end developers who need a quick back-end for prototyping and mocking.

Note: This version uses json-server v1 (beta), which is a ground-up rewrite. Many CLI options from v0 have been removed. See Options for the current set of supported configuration.

Features

  • 💨 Up and running quickly - Spin up a RESTful mock API in seconds.
  • ⚙️ Configurable - Supports json-server v1 configuration via environment variables.
  • TypeScriptTypescript support - Use TS for your db, middleware, or any file you mount into the container.
  • 💻 Mount in any supporting files you'd like! - For instance, want to use your custom data fixtures, utils, etc. in your db/middleware? Mount them in, import & prosper.
  • 📦 Useful dependencies are pre-installed in the image for your convenience. Use lodash-es & @faker-js/faker in any of the files powering your mock api.
  • 🧳 Install your own dependencies - Use the DEPENDENCIES envvar to pass a list of additional npm dependencies to use in your server files.
  • 🔂 Hot reloading the server on any changes.

Getting Started

Latest Version: codfish/json-server:1.0.0-beta.12

Note

You are reading the docs for the v1 beta of json-server-docker. If you'd prefer the stable v0 release, run docker run -p 3000:80 codfish/json-server:0.17.4 and view the v0 documentation.

By default, the image runs an instance of json-server with some dummy data for show. Spin up the example mock api in seconds.

# Visit <http://localhost:3000> to see it in action.
docker run -p 3000:3000 codfish/json-server

That's all good, but not very useful to you. You're meant to mount in your own db file(s) into the container. Read on for usage...

Usage

Warning

It's recommended to specify the tag of the image you want rather than using the latest image, which might break. Image tags are based off of the release versions for json-server. However there is not an image for every version. See the available versions on Docker Hub.

This project actually dogfoods itself. View the docker-compose.yml & the examples/ directory to see various usage examples. Also visit the json-server docs for more detailed examples on how to use the tool.

Examples

Docker Compose (Recommended)

services:
api:
image: codfish/json-server:1.0.0-beta.12ports:
- 3000:3000volumes:
- ./my-db.js:/app/db.js:delegated
- ./my-middleware.js:/app/middleware.js:delegated

Run docker compose up api. Visit http://localhost:3000/ to see your API.

Docker cli

Tip

The server listens on 0.0.0.0:3000 by default inside the container. Mapping a different host port (e.g. -p 3001:3000) will work fine, but your logs will still say localhost:3000. For the best DX, pass the PORT env var to sync them (e.g., docker run -e PORT=3001 -p 3001:3001 ...).

docker run -d -e PORT=3001 -p 3001:3001 \
-v ./my-db.js:/app/db.js \
-v ./my-middleware.js:/app/middleware.js \
codfish/json-server:1.0.0-beta.12

Advanced

Set configuration via environment variables.

services:
json-server:
image: codfish/json-server:1.0.0-beta.12volumes:
- ./db.ts:/app/db.ts:delegated
- ./middleware.ts:/app/middleware.ts:delegatedenvironment:
DEPENDENCIES: chance@1 node-emoji@1

See all the available options below.

Important Usage Notes

  • IDs must be strings. json-server v1 uses strict equality (===) to match URL params against record IDs. Since URL params are always strings, integer IDs will never match on individual resource lookups (e.g., GET /users/1). Use string IDs like "1" or UUIDs.
  • All mounted files should use ESM syntax (import/export default).
  • All files should be mounted into the /app directory in the container.
  • The following files are special and will "just work" when mounted over:
    • /app/db.{ts,js,json} - The database file. JS/TS files must export default a function that returns your data.
    • /app/middleware.{ts,js} - Custom middleware file. Must export default a (req, res, next) function.
    • /public - Static files directory.

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. Faker is included in the image to help facilitate doing these sorts of things inside your db or middleware files. For example:

// db.jsimport{faker}from'@faker-js/faker';exportdefault()=>({posts: faker.helpers.multiple(()=>({id: faker.string.uuid(),title: faker.lorem.words(3),body: faker.lorem.paragraphs(3),}),{count: 100},),});

Middleware

Mount a middleware.{js,ts} file to add custom middleware that runs before json-server's route handlers. This is useful for things like authentication checks, request mutation, custom headers, and logging.

// middleware.tsexportdefault(req,res,next)=>{// Add custom response headersres.set('X-Custom-Header','my-value');// Require authorization for non-browser requestsif(!req.accepts('html')&&!req.header('Authorization')){res.status(401).send();return;}next();};

Typescript Support

Important

  • Use export default ... for default exports from your ts files.
  • TS is configured to target ESM (es2022 / nodenext).
  • A path alias is configured for your convenience to map to the /app directory where all server files should be mounted.
  • When importing local files in TS, use the .js extension (e.g., import foo from './fixtures/bar.js').
// db.tsimport{faker}from'@faker-js/faker';interfaceDatabase{posts: Array<{id: string;title: string;body: string;}>;}exportdefault(): Database=>({posts: faker.helpers.multiple(()=>({id: faker.string.uuid(),title: faker.lorem.words(3),body: faker.lorem.paragraphs(3),}),{count: 10},),});
docker run -d -e PORT=3000 -p 3000:3000 -v ./db.ts:/app/db.ts codfish/json-server:1.0.0-beta.12

Options

json-server v1 has significantly fewer configuration options than v0. The following environment variables are supported:

OptionDescriptionDefault
DEPENDENCIESInstall extra npm dependencies in the container for you to use in your server files.
STATICServe an additional static files directory (./public is always served)
PORTSet the port the server listens on inside the container.3000

Caution

The DEPENDENCIES env var runs pnpm add with whatever packages you specify. A malicious package's install script will execute inside the container. Only use packages you trust.

Query Parameters

json-server v1 supports the following query parameters:

  • Pagination: _page and _per_page (e.g., ?_page=1&_per_page=10)
  • Sorting: _sort (e.g., ?_sort=id or ?_sort=-id for descending)
  • Filtering: Use field names directly (e.g., ?title=foo)
  • Embedding: _embed (e.g., ?_embed=comments)

Note: The _expand and q (full-text search) parameters from v0 have been removed in v1.

Maintaining/Contributing

This project dogfoods itself. To test it directly you can run:

git clone git@github.com:codfish/json-server-docker.git
cd json-server-docker
docker compose up -d

If you want to test it locally without docker, you can pnpm install and then pnpm start to run the server directly on your machine.

Examples

The docker-compose.yml defines several services that exercise different features. Each one maps to a directory in examples/.

ServicePortDescription
docker-compose up basic3000Default db.js with faker-generated data
docker-compose up typescript9998TypeScript db & middleware
docker-compose up json-db9997Plain JSON database file
docker-compose up middlewares9996Custom middleware that sets response headers
docker-compose up deps9995Extra dependencies installed via DEPENDENCIES envar
docker-compose up static9993Custom public directory with static HTML
docker-compose up dags9994Supporting files mounted alongside the db

Run all examples:

docker compose up --build

Or run a specific one:

docker compose up --build typescript

Visit the corresponding port (e.g., http://localhost:9998) to verify.

To update:

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

Visit http://localhost:3000. Update db.js or middleware.js to test out functionality. Changes should propagate automatically, just refresh the page.

Releasing

New version:

git tag -m '1.0.0-beta.12' 1.0.0-beta.12
git push origin 1.0.0-beta.12

Pushing a tag triggers the release workflow, which builds and pushes the Docker image tagged with the version.

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 1.0.0-beta.12 -m "Update 1.0.0-beta.12 tag"&& git push origin 1.0.0-beta.12 --force

Force-pushing the tag re-triggers the release workflow to rebuild the image.

About

Docker image to easily integrate json-server mock api's into your app.

Resources

Stars

19 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Repository files navigation

json-server-docker

Dockerized json-server for building a full fake RESTful API.

json-server versionpullsMIT License

Created with <3 for front-end developers who need a quick back-end for prototyping and mocking.

Note: This version uses json-server v1 (beta), which is a ground-up rewrite. Many CLI options from v0 have been removed. See Options for the current set of supported configuration.

Features

  • 💨 Up and running quickly - Spin up a RESTful mock API in seconds.
  • ⚙️ Configurable - Supports json-server v1 configuration via environment variables.
  • TypeScriptTypescript support - Use TS for your db, middleware, or any file you mount into the container.
  • 💻 Mount in any supporting files you'd like! - For instance, want to use your custom data fixtures, utils, etc. in your db/middleware? Mount them in, import & prosper.
  • 📦 Useful dependencies are pre-installed in the image for your convenience. Use lodash-es & @faker-js/faker in any of the files powering your mock api.
  • 🧳 Install your own dependencies - Use the DEPENDENCIES envvar to pass a list of additional npm dependencies to use in your server files.
  • 🔂 Hot reloading the server on any changes.

Getting Started

Latest Version: codfish/json-server:1.0.0-beta.12

Note

You are reading the docs for the v1 beta of json-server-docker. If you'd prefer the stable v0 release, run docker run -p 3000:80 codfish/json-server:0.17.4 and view the v0 documentation.

By default, the image runs an instance of json-server with some dummy data for show. Spin up the example mock api in seconds.

# Visit <http://localhost:3000> to see it in action.
docker run -p 3000:3000 codfish/json-server

That's all good, but not very useful to you. You're meant to mount in your own db file(s) into the container. Read on for usage...

Usage

Warning

It's recommended to specify the tag of the image you want rather than using the latest image, which might break. Image tags are based off of the release versions for json-server. However there is not an image for every version. See the available versions on Docker Hub.

This project actually dogfoods itself. View the docker-compose.yml & the examples/ directory to see various usage examples. Also visit the json-server docs for more detailed examples on how to use the tool.

Examples

Docker Compose (Recommended)

services:
api:
image: codfish/json-server:1.0.0-beta.12ports:
- 3000:3000volumes:
- ./my-db.js:/app/db.js:delegated
- ./my-middleware.js:/app/middleware.js:delegated

Run docker compose up api. Visit http://localhost:3000/ to see your API.

Docker cli

Tip

The server listens on 0.0.0.0:3000 by default inside the container. Mapping a different host port (e.g. -p 3001:3000) will work fine, but your logs will still say localhost:3000. For the best DX, pass the PORT env var to sync them (e.g., docker run -e PORT=3001 -p 3001:3001 ...).

docker run -d -e PORT=3001 -p 3001:3001 \
-v ./my-db.js:/app/db.js \
-v ./my-middleware.js:/app/middleware.js \
codfish/json-server:1.0.0-beta.12

Advanced

Set configuration via environment variables.

services:
json-server:
image: codfish/json-server:1.0.0-beta.12volumes:
- ./db.ts:/app/db.ts:delegated
- ./middleware.ts:/app/middleware.ts:delegatedenvironment:
DEPENDENCIES: chance@1 node-emoji@1

See all the available options below.

Important Usage Notes

  • IDs must be strings. json-server v1 uses strict equality (===) to match URL params against record IDs. Since URL params are always strings, integer IDs will never match on individual resource lookups (e.g., GET /users/1). Use string IDs like "1" or UUIDs.
  • All mounted files should use ESM syntax (import/export default).
  • All files should be mounted into the /app directory in the container.
  • The following files are special and will "just work" when mounted over:
    • /app/db.{ts,js,json} - The database file. JS/TS files must export default a function that returns your data.
    • /app/middleware.{ts,js} - Custom middleware file. Must export default a (req, res, next) function.
    • /public - Static files directory.

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. Faker is included in the image to help facilitate doing these sorts of things inside your db or middleware files. For example:

// db.jsimport{faker}from'@faker-js/faker';exportdefault()=>({posts: faker.helpers.multiple(()=>({id: faker.string.uuid(),title: faker.lorem.words(3),body: faker.lorem.paragraphs(3),}),{count: 100},),});

Middleware

Mount a middleware.{js,ts} file to add custom middleware that runs before json-server's route handlers. This is useful for things like authentication checks, request mutation, custom headers, and logging.

// middleware.tsexportdefault(req,res,next)=>{// Add custom response headersres.set('X-Custom-Header','my-value');// Require authorization for non-browser requestsif(!req.accepts('html')&&!req.header('Authorization')){res.status(401).send();return;}next();};

Typescript Support

Important

  • Use export default ... for default exports from your ts files.
  • TS is configured to target ESM (es2022 / nodenext).
  • A path alias is configured for your convenience to map to the /app directory where all server files should be mounted.
  • When importing local files in TS, use the .js extension (e.g., import foo from './fixtures/bar.js').
// db.tsimport{faker}from'@faker-js/faker';interfaceDatabase{posts: Array<{id: string;title: string;body: string;}>;}exportdefault(): Database=>({posts: faker.helpers.multiple(()=>({id: faker.string.uuid(),title: faker.lorem.words(3),body: faker.lorem.paragraphs(3),}),{count: 10},),});
docker run -d -e PORT=3000 -p 3000:3000 -v ./db.ts:/app/db.ts codfish/json-server:1.0.0-beta.12

Options

json-server v1 has significantly fewer configuration options than v0. The following environment variables are supported:

OptionDescriptionDefault
DEPENDENCIESInstall extra npm dependencies in the container for you to use in your server files.
STATICServe an additional static files directory (./public is always served)
PORTSet the port the server listens on inside the container.3000

Caution

The DEPENDENCIES env var runs pnpm add with whatever packages you specify. A malicious package's install script will execute inside the container. Only use packages you trust.

Query Parameters

json-server v1 supports the following query parameters:

  • Pagination: _page and _per_page (e.g., ?_page=1&_per_page=10)
  • Sorting: _sort (e.g., ?_sort=id or ?_sort=-id for descending)
  • Filtering: Use field names directly (e.g., ?title=foo)
  • Embedding: _embed (e.g., ?_embed=comments)

Note: The _expand and q (full-text search) parameters from v0 have been removed in v1.

Maintaining/Contributing

This project dogfoods itself. To test it directly you can run:

git clone git@github.com:codfish/json-server-docker.git
cd json-server-docker
docker compose up -d

If you want to test it locally without docker, you can pnpm install and then pnpm start to run the server directly on your machine.

Examples

The docker-compose.yml defines several services that exercise different features. Each one maps to a directory in examples/.

ServicePortDescription
docker-compose up basic3000Default db.js with faker-generated data
docker-compose up typescript9998TypeScript db & middleware
docker-compose up json-db9997Plain JSON database file
docker-compose up middlewares9996Custom middleware that sets response headers
docker-compose up deps9995Extra dependencies installed via DEPENDENCIES envar
docker-compose up static9993Custom public directory with static HTML
docker-compose up dags9994Supporting files mounted alongside the db

Run all examples:

docker compose up --build

Or run a specific one:

docker compose up --build typescript

Visit the corresponding port (e.g., http://localhost:9998) to verify.

To update:

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

Visit http://localhost:3000. Update db.js or middleware.js to test out functionality. Changes should propagate automatically, just refresh the page.

Releasing

New version:

git tag -m '1.0.0-beta.12' 1.0.0-beta.12
git push origin 1.0.0-beta.12

Pushing a tag triggers the release workflow, which builds and pushes the Docker image tagged with the version.

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 1.0.0-beta.12 -m "Update 1.0.0-beta.12 tag"&& git push origin 1.0.0-beta.12 --force

Force-pushing the tag re-triggers the release workflow to rebuild the image.

About

Docker image to easily integrate json-server mock api's into your app.

Resources

Stars

19 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

json-server-docker

Dockerized json-server for building a full fake RESTful API.

json-server versionpullsMIT License

Created with <3 for front-end developers who need a quick back-end for prototyping and mocking.

Note: This version uses json-server v1 (beta), which is a ground-up rewrite. Many CLI options from v0 have been removed. See Options for the current set of supported configuration.

Features

  • 💨 Up and running quickly - Spin up a RESTful mock API in seconds.
  • ⚙️ Configurable - Supports json-server v1 configuration via environment variables.
  • TypeScriptTypescript support - Use TS for your db, middleware, or any file you mount into the container.
  • 💻 Mount in any supporting files you'd like! - For instance, want to use your custom data fixtures, utils, etc. in your db/middleware? Mount them in, import & prosper.
  • 📦 Useful dependencies are pre-installed in the image for your convenience. Use lodash-es & @faker-js/faker in any of the files powering your mock api.
  • 🧳 Install your own dependencies - Use the DEPENDENCIES envvar to pass a list of additional npm dependencies to use in your server files.
  • 🔂 Hot reloading the server on any changes.

Getting Started

Latest Version: codfish/json-server:1.0.0-beta.12

Note

You are reading the docs for the v1 beta of json-server-docker. If you'd prefer the stable v0 release, run docker run -p 3000:80 codfish/json-server:0.17.4 and view the v0 documentation.

By default, the image runs an instance of json-server with some dummy data for show. Spin up the example mock api in seconds.

# Visit <http://localhost:3000> to see it in action.
docker run -p 3000:3000 codfish/json-server

That's all good, but not very useful to you. You're meant to mount in your own db file(s) into the container. Read on for usage...

Usage

Warning

It's recommended to specify the tag of the image you want rather than using the latest image, which might break. Image tags are based off of the release versions for json-server. However there is not an image for every version. See the available versions on Docker Hub.

This project actually dogfoods itself. View the docker-compose.yml & the examples/ directory to see various usage examples. Also visit the json-server docs for more detailed examples on how to use the tool.

Examples

Docker Compose (Recommended)

services:
api:
image: codfish/json-server:1.0.0-beta.12ports:
- 3000:3000volumes:
- ./my-db.js:/app/db.js:delegated
- ./my-middleware.js:/app/middleware.js:delegated

Run docker compose up api. Visit http://localhost:3000/ to see your API.

Docker cli

Tip

The server listens on 0.0.0.0:3000 by default inside the container. Mapping a different host port (e.g. -p 3001:3000) will work fine, but your logs will still say localhost:3000. For the best DX, pass the PORT env var to sync them (e.g., docker run -e PORT=3001 -p 3001:3001 ...).

docker run -d -e PORT=3001 -p 3001:3001 \
-v ./my-db.js:/app/db.js \
-v ./my-middleware.js:/app/middleware.js \
codfish/json-server:1.0.0-beta.12

Advanced

Set configuration via environment variables.

services:
json-server:
image: codfish/json-server:1.0.0-beta.12volumes:
- ./db.ts:/app/db.ts:delegated
- ./middleware.ts:/app/middleware.ts:delegatedenvironment:
DEPENDENCIES: chance@1 node-emoji@1

See all the available options below.

Important Usage Notes

  • IDs must be strings. json-server v1 uses strict equality (===) to match URL params against record IDs. Since URL params are always strings, integer IDs will never match on individual resource lookups (e.g., GET /users/1). Use string IDs like "1" or UUIDs.
  • All mounted files should use ESM syntax (import/export default).
  • All files should be mounted into the /app directory in the container.
  • The following files are special and will "just work" when mounted over:
    • /app/db.{ts,js,json} - The database file. JS/TS files must export default a function that returns your data.
    • /app/middleware.{ts,js} - Custom middleware file. Must export default a (req, res, next) function.
    • /public - Static files directory.

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. Faker is included in the image to help facilitate doing these sorts of things inside your db or middleware files. For example:

// db.jsimport{faker}from'@faker-js/faker';exportdefault()=>({posts: faker.helpers.multiple(()=>({id: faker.string.uuid(),title: faker.lorem.words(3),body: faker.lorem.paragraphs(3),}),{count: 100},),});

Middleware

Mount a middleware.{js,ts} file to add custom middleware that runs before json-server's route handlers. This is useful for things like authentication checks, request mutation, custom headers, and logging.

// middleware.tsexportdefault(req,res,next)=>{// Add custom response headersres.set('X-Custom-Header','my-value');// Require authorization for non-browser requestsif(!req.accepts('html')&&!req.header('Authorization')){res.status(401).send();return;}next();};

Typescript Support

Important

  • Use export default ... for default exports from your ts files.
  • TS is configured to target ESM (es2022 / nodenext).
  • A path alias is configured for your convenience to map to the /app directory where all server files should be mounted.
  • When importing local files in TS, use the .js extension (e.g., import foo from './fixtures/bar.js').
// db.tsimport{faker}from'@faker-js/faker';interfaceDatabase{posts: Array<{id: string;title: string;body: string;}>;}exportdefault(): Database=>({posts: faker.helpers.multiple(()=>({id: faker.string.uuid(),title: faker.lorem.words(3),body: faker.lorem.paragraphs(3),}),{count: 10},),});
docker run -d -e PORT=3000 -p 3000:3000 -v ./db.ts:/app/db.ts codfish/json-server:1.0.0-beta.12

Options

json-server v1 has significantly fewer configuration options than v0. The following environment variables are supported:

OptionDescriptionDefault
DEPENDENCIESInstall extra npm dependencies in the container for you to use in your server files.
STATICServe an additional static files directory (./public is always served)
PORTSet the port the server listens on inside the container.3000

Caution

The DEPENDENCIES env var runs pnpm add with whatever packages you specify. A malicious package's install script will execute inside the container. Only use packages you trust.

Query Parameters

json-server v1 supports the following query parameters:

  • Pagination: _page and _per_page (e.g., ?_page=1&_per_page=10)
  • Sorting: _sort (e.g., ?_sort=id or ?_sort=-id for descending)
  • Filtering: Use field names directly (e.g., ?title=foo)
  • Embedding: _embed (e.g., ?_embed=comments)

Note: The _expand and q (full-text search) parameters from v0 have been removed in v1.

Maintaining/Contributing

This project dogfoods itself. To test it directly you can run:

git clone git@github.com:codfish/json-server-docker.git
cd json-server-docker
docker compose up -d

If you want to test it locally without docker, you can pnpm install and then pnpm start to run the server directly on your machine.

Examples

The docker-compose.yml defines several services that exercise different features. Each one maps to a directory in examples/.

ServicePortDescription
docker-compose up basic3000Default db.js with faker-generated data
docker-compose up typescript9998TypeScript db & middleware
docker-compose up json-db9997Plain JSON database file
docker-compose up middlewares9996Custom middleware that sets response headers
docker-compose up deps9995Extra dependencies installed via DEPENDENCIES envar
docker-compose up static9993Custom public directory with static HTML
docker-compose up dags9994Supporting files mounted alongside the db

Run all examples:

docker compose up --build

Or run a specific one:

docker compose up --build typescript

Visit the corresponding port (e.g., http://localhost:9998) to verify.

To update:

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

Visit http://localhost:3000. Update db.js or middleware.js to test out functionality. Changes should propagate automatically, just refresh the page.

Releasing

New version:

git tag -m '1.0.0-beta.12' 1.0.0-beta.12
git push origin 1.0.0-beta.12

Pushing a tag triggers the release workflow, which builds and pushes the Docker image tagged with the version.

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 1.0.0-beta.12 -m "Update 1.0.0-beta.12 tag"&& git push origin 1.0.0-beta.12 --force

Force-pushing the tag re-triggers the release workflow to rebuild the image.

About

Docker image to easily integrate json-server mock api's into your app.

Resources

Stars

19 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

json-server-docker

Dockerized json-server for building a full fake RESTful API.

json-server versionpullsMIT License

Created with <3 for front-end developers who need a quick back-end for prototyping and mocking.

Note: This version uses json-server v1 (beta), which is a ground-up rewrite. Many CLI options from v0 have been removed. See Options for the current set of supported configuration.

Features

  • 💨 Up and running quickly - Spin up a RESTful mock API in seconds.
  • ⚙️ Configurable - Supports json-server v1 configuration via environment variables.
  • TypeScriptTypescript support - Use TS for your db, middleware, or any file you mount into the container.
  • 💻 Mount in any supporting files you'd like! - For instance, want to use your custom data fixtures, utils, etc. in your db/middleware? Mount them in, import & prosper.
  • 📦 Useful dependencies are pre-installed in the image for your convenience. Use lodash-es & @faker-js/faker in any of the files powering your mock api.
  • 🧳 Install your own dependencies - Use the DEPENDENCIES envvar to pass a list of additional npm dependencies to use in your server files.
  • 🔂 Hot reloading the server on any changes.

Getting Started

Latest Version: codfish/json-server:1.0.0-beta.12

Note

You are reading the docs for the v1 beta of json-server-docker. If you'd prefer the stable v0 release, run docker run -p 3000:80 codfish/json-server:0.17.4 and view the v0 documentation.

By default, the image runs an instance of json-server with some dummy data for show. Spin up the example mock api in seconds.

# Visit <http://localhost:3000> to see it in action.
docker run -p 3000:3000 codfish/json-server

That's all good, but not very useful to you. You're meant to mount in your own db file(s) into the container. Read on for usage...

Usage

Warning

It's recommended to specify the tag of the image you want rather than using the latest image, which might break. Image tags are based off of the release versions for json-server. However there is not an image for every version. See the available versions on Docker Hub.

This project actually dogfoods itself. View the docker-compose.yml & the examples/ directory to see various usage examples. Also visit the json-server docs for more detailed examples on how to use the tool.

Examples

Docker Compose (Recommended)

services:
api:
image: codfish/json-server:1.0.0-beta.12ports:
- 3000:3000volumes:
- ./my-db.js:/app/db.js:delegated
- ./my-middleware.js:/app/middleware.js:delegated

Run docker compose up api. Visit http://localhost:3000/ to see your API.

Docker cli

Tip

The server listens on 0.0.0.0:3000 by default inside the container. Mapping a different host port (e.g. -p 3001:3000) will work fine, but your logs will still say localhost:3000. For the best DX, pass the PORT env var to sync them (e.g., docker run -e PORT=3001 -p 3001:3001 ...).

docker run -d -e PORT=3001 -p 3001:3001 \
-v ./my-db.js:/app/db.js \
-v ./my-middleware.js:/app/middleware.js \
codfish/json-server:1.0.0-beta.12

Advanced

Set configuration via environment variables.

services:
json-server:
image: codfish/json-server:1.0.0-beta.12volumes:
- ./db.ts:/app/db.ts:delegated
- ./middleware.ts:/app/middleware.ts:delegatedenvironment:
DEPENDENCIES: chance@1 node-emoji@1

See all the available options below.

Important Usage Notes

  • IDs must be strings. json-server v1 uses strict equality (===) to match URL params against record IDs. Since URL params are always strings, integer IDs will never match on individual resource lookups (e.g., GET /users/1). Use string IDs like "1" or UUIDs.
  • All mounted files should use ESM syntax (import/export default).
  • All files should be mounted into the /app directory in the container.
  • The following files are special and will "just work" when mounted over:
    • /app/db.{ts,js,json} - The database file. JS/TS files must export default a function that returns your data.
    • /app/middleware.{ts,js} - Custom middleware file. Must export default a (req, res, next) function.
    • /public - Static files directory.

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. Faker is included in the image to help facilitate doing these sorts of things inside your db or middleware files. For example:

// db.jsimport{faker}from'@faker-js/faker';exportdefault()=>({posts: faker.helpers.multiple(()=>({id: faker.string.uuid(),title: faker.lorem.words(3),body: faker.lorem.paragraphs(3),}),{count: 100},),});

Middleware

Mount a middleware.{js,ts} file to add custom middleware that runs before json-server's route handlers. This is useful for things like authentication checks, request mutation, custom headers, and logging.

// middleware.tsexportdefault(req,res,next)=>{// Add custom response headersres.set('X-Custom-Header','my-value');// Require authorization for non-browser requestsif(!req.accepts('html')&&!req.header('Authorization')){res.status(401).send();return;}next();};

Typescript Support

Important

  • Use export default ... for default exports from your ts files.
  • TS is configured to target ESM (es2022 / nodenext).
  • A path alias is configured for your convenience to map to the /app directory where all server files should be mounted.
  • When importing local files in TS, use the .js extension (e.g., import foo from './fixtures/bar.js').
// db.tsimport{faker}from'@faker-js/faker';interfaceDatabase{posts: Array<{id: string;title: string;body: string;}>;}exportdefault(): Database=>({posts: faker.helpers.multiple(()=>({id: faker.string.uuid(),title: faker.lorem.words(3),body: faker.lorem.paragraphs(3),}),{count: 10},),});
docker run -d -e PORT=3000 -p 3000:3000 -v ./db.ts:/app/db.ts codfish/json-server:1.0.0-beta.12

Options

json-server v1 has significantly fewer configuration options than v0. The following environment variables are supported:

OptionDescriptionDefault
DEPENDENCIESInstall extra npm dependencies in the container for you to use in your server files.
STATICServe an additional static files directory (./public is always served)
PORTSet the port the server listens on inside the container.3000

Caution

The DEPENDENCIES env var runs pnpm add with whatever packages you specify. A malicious package's install script will execute inside the container. Only use packages you trust.

Query Parameters

json-server v1 supports the following query parameters:

  • Pagination: _page and _per_page (e.g., ?_page=1&_per_page=10)
  • Sorting: _sort (e.g., ?_sort=id or ?_sort=-id for descending)
  • Filtering: Use field names directly (e.g., ?title=foo)
  • Embedding: _embed (e.g., ?_embed=comments)

Note: The _expand and q (full-text search) parameters from v0 have been removed in v1.

Maintaining/Contributing

This project dogfoods itself. To test it directly you can run:

git clone git@github.com:codfish/json-server-docker.git
cd json-server-docker
docker compose up -d

If you want to test it locally without docker, you can pnpm install and then pnpm start to run the server directly on your machine.

Examples

The docker-compose.yml defines several services that exercise different features. Each one maps to a directory in examples/.

ServicePortDescription
docker-compose up basic3000Default db.js with faker-generated data
docker-compose up typescript9998TypeScript db & middleware
docker-compose up json-db9997Plain JSON database file
docker-compose up middlewares9996Custom middleware that sets response headers
docker-compose up deps9995Extra dependencies installed via DEPENDENCIES envar
docker-compose up static9993Custom public directory with static HTML
docker-compose up dags9994Supporting files mounted alongside the db

Run all examples:

docker compose up --build

Or run a specific one:

docker compose up --build typescript

Visit the corresponding port (e.g., http://localhost:9998) to verify.

To update:

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

Visit http://localhost:3000. Update db.js or middleware.js to test out functionality. Changes should propagate automatically, just refresh the page.

Releasing

New version:

git tag -m '1.0.0-beta.12' 1.0.0-beta.12
git push origin 1.0.0-beta.12

Pushing a tag triggers the release workflow, which builds and pushes the Docker image tagged with the version.

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 1.0.0-beta.12 -m "Update 1.0.0-beta.12 tag"&& git push origin 1.0.0-beta.12 --force

Force-pushing the tag re-triggers the release workflow to rebuild the image.

About

Docker image to easily integrate json-server mock api's into your app.

Resources

Stars

19 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Repository files navigation

json-server-docker

Dockerized json-server for building a full fake RESTful API.

json-server versionpullsMIT License

Created with <3 for front-end developers who need a quick back-end for prototyping and mocking.

Note: This version uses json-server v1 (beta), which is a ground-up rewrite. Many CLI options from v0 have been removed. See Options for the current set of supported configuration.

Features

  • 💨 Up and running quickly - Spin up a RESTful mock API in seconds.
  • ⚙️ Configurable - Supports json-server v1 configuration via environment variables.
  • TypeScriptTypescript support - Use TS for your db, middleware, or any file you mount into the container.
  • 💻 Mount in any supporting files you'd like! - For instance, want to use your custom data fixtures, utils, etc. in your db/middleware? Mount them in, import & prosper.
  • 📦 Useful dependencies are pre-installed in the image for your convenience. Use lodash-es & @faker-js/faker in any of the files powering your mock api.
  • 🧳 Install your own dependencies - Use the DEPENDENCIES envvar to pass a list of additional npm dependencies to use in your server files.
  • 🔂 Hot reloading the server on any changes.

Getting Started

Latest Version: codfish/json-server:1.0.0-beta.12

Note

You are reading the docs for the v1 beta of json-server-docker. If you'd prefer the stable v0 release, run docker run -p 3000:80 codfish/json-server:0.17.4 and view the v0 documentation.

By default, the image runs an instance of json-server with some dummy data for show. Spin up the example mock api in seconds.

# Visit <http://localhost:3000> to see it in action.
docker run -p 3000:3000 codfish/json-server

That's all good, but not very useful to you. You're meant to mount in your own db file(s) into the container. Read on for usage...

Usage

Warning

It's recommended to specify the tag of the image you want rather than using the latest image, which might break. Image tags are based off of the release versions for json-server. However there is not an image for every version. See the available versions on Docker Hub.

This project actually dogfoods itself. View the docker-compose.yml & the examples/ directory to see various usage examples. Also visit the json-server docs for more detailed examples on how to use the tool.

Examples

Docker Compose (Recommended)

services:
api:
image: codfish/json-server:1.0.0-beta.12ports:
- 3000:3000volumes:
- ./my-db.js:/app/db.js:delegated
- ./my-middleware.js:/app/middleware.js:delegated

Run docker compose up api. Visit http://localhost:3000/ to see your API.

Docker cli

Tip

The server listens on 0.0.0.0:3000 by default inside the container. Mapping a different host port (e.g. -p 3001:3000) will work fine, but your logs will still say localhost:3000. For the best DX, pass the PORT env var to sync them (e.g., docker run -e PORT=3001 -p 3001:3001 ...).

docker run -d -e PORT=3001 -p 3001:3001 \
-v ./my-db.js:/app/db.js \
-v ./my-middleware.js:/app/middleware.js \
codfish/json-server:1.0.0-beta.12

Advanced

Set configuration via environment variables.

services:
json-server:
image: codfish/json-server:1.0.0-beta.12volumes:
- ./db.ts:/app/db.ts:delegated
- ./middleware.ts:/app/middleware.ts:delegatedenvironment:
DEPENDENCIES: chance@1 node-emoji@1

See all the available options below.

Important Usage Notes

  • IDs must be strings. json-server v1 uses strict equality (===) to match URL params against record IDs. Since URL params are always strings, integer IDs will never match on individual resource lookups (e.g., GET /users/1). Use string IDs like "1" or UUIDs.
  • All mounted files should use ESM syntax (import/export default).
  • All files should be mounted into the /app directory in the container.
  • The following files are special and will "just work" when mounted over:
    • /app/db.{ts,js,json} - The database file. JS/TS files must export default a function that returns your data.
    • /app/middleware.{ts,js} - Custom middleware file. Must export default a (req, res, next) function.
    • /public - Static files directory.

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. Faker is included in the image to help facilitate doing these sorts of things inside your db or middleware files. For example:

// db.jsimport{faker}from'@faker-js/faker';exportdefault()=>({posts: faker.helpers.multiple(()=>({id: faker.string.uuid(),title: faker.lorem.words(3),body: faker.lorem.paragraphs(3),}),{count: 100},),});

Middleware

Mount a middleware.{js,ts} file to add custom middleware that runs before json-server's route handlers. This is useful for things like authentication checks, request mutation, custom headers, and logging.

// middleware.tsexportdefault(req,res,next)=>{// Add custom response headersres.set('X-Custom-Header','my-value');// Require authorization for non-browser requestsif(!req.accepts('html')&&!req.header('Authorization')){res.status(401).send();return;}next();};

Typescript Support

Important

  • Use export default ... for default exports from your ts files.
  • TS is configured to target ESM (es2022 / nodenext).
  • A path alias is configured for your convenience to map to the /app directory where all server files should be mounted.
  • When importing local files in TS, use the .js extension (e.g., import foo from './fixtures/bar.js').
// db.tsimport{faker}from'@faker-js/faker';interfaceDatabase{posts: Array<{id: string;title: string;body: string;}>;}exportdefault(): Database=>({posts: faker.helpers.multiple(()=>({id: faker.string.uuid(),title: faker.lorem.words(3),body: faker.lorem.paragraphs(3),}),{count: 10},),});
docker run -d -e PORT=3000 -p 3000:3000 -v ./db.ts:/app/db.ts codfish/json-server:1.0.0-beta.12

Options

json-server v1 has significantly fewer configuration options than v0. The following environment variables are supported:

OptionDescriptionDefault
DEPENDENCIESInstall extra npm dependencies in the container for you to use in your server files.
STATICServe an additional static files directory (./public is always served)
PORTSet the port the server listens on inside the container.3000

Caution

The DEPENDENCIES env var runs pnpm add with whatever packages you specify. A malicious package's install script will execute inside the container. Only use packages you trust.

Query Parameters

json-server v1 supports the following query parameters:

  • Pagination: _page and _per_page (e.g., ?_page=1&_per_page=10)
  • Sorting: _sort (e.g., ?_sort=id or ?_sort=-id for descending)
  • Filtering: Use field names directly (e.g., ?title=foo)
  • Embedding: _embed (e.g., ?_embed=comments)

Note: The _expand and q (full-text search) parameters from v0 have been removed in v1.

Maintaining/Contributing

This project dogfoods itself. To test it directly you can run:

git clone git@github.com:codfish/json-server-docker.git
cd json-server-docker
docker compose up -d

If you want to test it locally without docker, you can pnpm install and then pnpm start to run the server directly on your machine.

Examples

The docker-compose.yml defines several services that exercise different features. Each one maps to a directory in examples/.

ServicePortDescription
docker-compose up basic3000Default db.js with faker-generated data
docker-compose up typescript9998TypeScript db & middleware
docker-compose up json-db9997Plain JSON database file
docker-compose up middlewares9996Custom middleware that sets response headers
docker-compose up deps9995Extra dependencies installed via DEPENDENCIES envar
docker-compose up static9993Custom public directory with static HTML
docker-compose up dags9994Supporting files mounted alongside the db

Run all examples:

docker compose up --build

Or run a specific one:

docker compose up --build typescript

Visit the corresponding port (e.g., http://localhost:9998) to verify.

To update:

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

Visit http://localhost:3000. Update db.js or middleware.js to test out functionality. Changes should propagate automatically, just refresh the page.

Releasing

New version:

git tag -m '1.0.0-beta.12' 1.0.0-beta.12
git push origin 1.0.0-beta.12

Pushing a tag triggers the release workflow, which builds and pushes the Docker image tagged with the version.

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 1.0.0-beta.12 -m "Update 1.0.0-beta.12 tag"&& git push origin 1.0.0-beta.12 --force

Force-pushing the tag re-triggers the release workflow to rebuild the image.

About

Docker image to easily integrate json-server mock api's into your app.

Resources

Stars

19 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

json-server-docker

Dockerized json-server for building a full fake RESTful API.

json-server versionpullsMIT License

Created with <3 for front-end developers who need a quick back-end for prototyping and mocking.

Note: This version uses json-server v1 (beta), which is a ground-up rewrite. Many CLI options from v0 have been removed. See Options for the current set of supported configuration.

Features

  • 💨 Up and running quickly - Spin up a RESTful mock API in seconds.
  • ⚙️ Configurable - Supports json-server v1 configuration via environment variables.
  • TypeScriptTypescript support - Use TS for your db, middleware, or any file you mount into the container.
  • 💻 Mount in any supporting files you'd like! - For instance, want to use your custom data fixtures, utils, etc. in your db/middleware? Mount them in, import & prosper.
  • 📦 Useful dependencies are pre-installed in the image for your convenience. Use lodash-es & @faker-js/faker in any of the files powering your mock api.
  • 🧳 Install your own dependencies - Use the DEPENDENCIES envvar to pass a list of additional npm dependencies to use in your server files.
  • 🔂 Hot reloading the server on any changes.

Getting Started

Latest Version: codfish/json-server:1.0.0-beta.12

Note

You are reading the docs for the v1 beta of json-server-docker. If you'd prefer the stable v0 release, run docker run -p 3000:80 codfish/json-server:0.17.4 and view the v0 documentation.

By default, the image runs an instance of json-server with some dummy data for show. Spin up the example mock api in seconds.

# Visit <http://localhost:3000> to see it in action.
docker run -p 3000:3000 codfish/json-server

That's all good, but not very useful to you. You're meant to mount in your own db file(s) into the container. Read on for usage...

Usage

Warning

It's recommended to specify the tag of the image you want rather than using the latest image, which might break. Image tags are based off of the release versions for json-server. However there is not an image for every version. See the available versions on Docker Hub.

This project actually dogfoods itself. View the docker-compose.yml & the examples/ directory to see various usage examples. Also visit the json-server docs for more detailed examples on how to use the tool.

Examples

Docker Compose (Recommended)

services:
api:
image: codfish/json-server:1.0.0-beta.12ports:
- 3000:3000volumes:
- ./my-db.js:/app/db.js:delegated
- ./my-middleware.js:/app/middleware.js:delegated

Run docker compose up api. Visit http://localhost:3000/ to see your API.

Docker cli

Tip

The server listens on 0.0.0.0:3000 by default inside the container. Mapping a different host port (e.g. -p 3001:3000) will work fine, but your logs will still say localhost:3000. For the best DX, pass the PORT env var to sync them (e.g., docker run -e PORT=3001 -p 3001:3001 ...).

docker run -d -e PORT=3001 -p 3001:3001 \
-v ./my-db.js:/app/db.js \
-v ./my-middleware.js:/app/middleware.js \
codfish/json-server:1.0.0-beta.12

Advanced

Set configuration via environment variables.

services:
json-server:
image: codfish/json-server:1.0.0-beta.12volumes:
- ./db.ts:/app/db.ts:delegated
- ./middleware.ts:/app/middleware.ts:delegatedenvironment:
DEPENDENCIES: chance@1 node-emoji@1

See all the available options below.

Important Usage Notes

  • IDs must be strings. json-server v1 uses strict equality (===) to match URL params against record IDs. Since URL params are always strings, integer IDs will never match on individual resource lookups (e.g., GET /users/1). Use string IDs like "1" or UUIDs.
  • All mounted files should use ESM syntax (import/export default).
  • All files should be mounted into the /app directory in the container.
  • The following files are special and will "just work" when mounted over:
    • /app/db.{ts,js,json} - The database file. JS/TS files must export default a function that returns your data.
    • /app/middleware.{ts,js} - Custom middleware file. Must export default a (req, res, next) function.
    • /public - Static files directory.

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. Faker is included in the image to help facilitate doing these sorts of things inside your db or middleware files. For example:

// db.jsimport{faker}from'@faker-js/faker';exportdefault()=>({posts: faker.helpers.multiple(()=>({id: faker.string.uuid(),title: faker.lorem.words(3),body: faker.lorem.paragraphs(3),}),{count: 100},),});

Middleware

Mount a middleware.{js,ts} file to add custom middleware that runs before json-server's route handlers. This is useful for things like authentication checks, request mutation, custom headers, and logging.

// middleware.tsexportdefault(req,res,next)=>{// Add custom response headersres.set('X-Custom-Header','my-value');// Require authorization for non-browser requestsif(!req.accepts('html')&&!req.header('Authorization')){res.status(401).send();return;}next();};

Typescript Support

Important

  • Use export default ... for default exports from your ts files.
  • TS is configured to target ESM (es2022 / nodenext).
  • A path alias is configured for your convenience to map to the /app directory where all server files should be mounted.
  • When importing local files in TS, use the .js extension (e.g., import foo from './fixtures/bar.js').
// db.tsimport{faker}from'@faker-js/faker';interfaceDatabase{posts: Array<{id: string;title: string;body: string;}>;}exportdefault(): Database=>({posts: faker.helpers.multiple(()=>({id: faker.string.uuid(),title: faker.lorem.words(3),body: faker.lorem.paragraphs(3),}),{count: 10},),});
docker run -d -e PORT=3000 -p 3000:3000 -v ./db.ts:/app/db.ts codfish/json-server:1.0.0-beta.12

Options

json-server v1 has significantly fewer configuration options than v0. The following environment variables are supported:

OptionDescriptionDefault
DEPENDENCIESInstall extra npm dependencies in the container for you to use in your server files.
STATICServe an additional static files directory (./public is always served)
PORTSet the port the server listens on inside the container.3000

Caution

The DEPENDENCIES env var runs pnpm add with whatever packages you specify. A malicious package's install script will execute inside the container. Only use packages you trust.

Query Parameters

json-server v1 supports the following query parameters:

  • Pagination: _page and _per_page (e.g., ?_page=1&_per_page=10)
  • Sorting: _sort (e.g., ?_sort=id or ?_sort=-id for descending)
  • Filtering: Use field names directly (e.g., ?title=foo)
  • Embedding: _embed (e.g., ?_embed=comments)

Note: The _expand and q (full-text search) parameters from v0 have been removed in v1.

Maintaining/Contributing

This project dogfoods itself. To test it directly you can run:

git clone git@github.com:codfish/json-server-docker.git
cd json-server-docker
docker compose up -d

If you want to test it locally without docker, you can pnpm install and then pnpm start to run the server directly on your machine.

Examples

The docker-compose.yml defines several services that exercise different features. Each one maps to a directory in examples/.

ServicePortDescription
docker-compose up basic3000Default db.js with faker-generated data
docker-compose up typescript9998TypeScript db & middleware
docker-compose up json-db9997Plain JSON database file
docker-compose up middlewares9996Custom middleware that sets response headers
docker-compose up deps9995Extra dependencies installed via DEPENDENCIES envar
docker-compose up static9993Custom public directory with static HTML
docker-compose up dags9994Supporting files mounted alongside the db

Run all examples:

docker compose up --build

Or run a specific one:

docker compose up --build typescript

Visit the corresponding port (e.g., http://localhost:9998) to verify.

To update:

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

Visit http://localhost:3000. Update db.js or middleware.js to test out functionality. Changes should propagate automatically, just refresh the page.

Releasing

New version:

git tag -m '1.0.0-beta.12' 1.0.0-beta.12
git push origin 1.0.0-beta.12

Pushing a tag triggers the release workflow, which builds and pushes the Docker image tagged with the version.

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 1.0.0-beta.12 -m "Update 1.0.0-beta.12 tag"&& git push origin 1.0.0-beta.12 --force

Force-pushing the tag re-triggers the release workflow to rebuild the image.

About

Docker image to easily integrate json-server mock api's into your app.

Resources

Stars

19 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

json-server-docker

Dockerized json-server for building a full fake RESTful API.

json-server versionpullsMIT License

Created with <3 for front-end developers who need a quick back-end for prototyping and mocking.

Note: This version uses json-server v1 (beta), which is a ground-up rewrite. Many CLI options from v0 have been removed. See Options for the current set of supported configuration.

Features

  • 💨 Up and running quickly - Spin up a RESTful mock API in seconds.
  • ⚙️ Configurable - Supports json-server v1 configuration via environment variables.
  • TypeScriptTypescript support - Use TS for your db, middleware, or any file you mount into the container.
  • 💻 Mount in any supporting files you'd like! - For instance, want to use your custom data fixtures, utils, etc. in your db/middleware? Mount them in, import & prosper.
  • 📦 Useful dependencies are pre-installed in the image for your convenience. Use lodash-es & @faker-js/faker in any of the files powering your mock api.
  • 🧳 Install your own dependencies - Use the DEPENDENCIES envvar to pass a list of additional npm dependencies to use in your server files.
  • 🔂 Hot reloading the server on any changes.

Getting Started

Latest Version: codfish/json-server:1.0.0-beta.12

Note

You are reading the docs for the v1 beta of json-server-docker. If you'd prefer the stable v0 release, run docker run -p 3000:80 codfish/json-server:0.17.4 and view the v0 documentation.

By default, the image runs an instance of json-server with some dummy data for show. Spin up the example mock api in seconds.

# Visit <http://localhost:3000> to see it in action.
docker run -p 3000:3000 codfish/json-server

That's all good, but not very useful to you. You're meant to mount in your own db file(s) into the container. Read on for usage...

Usage

Warning

It's recommended to specify the tag of the image you want rather than using the latest image, which might break. Image tags are based off of the release versions for json-server. However there is not an image for every version. See the available versions on Docker Hub.

This project actually dogfoods itself. View the docker-compose.yml & the examples/ directory to see various usage examples. Also visit the json-server docs for more detailed examples on how to use the tool.

Examples

Docker Compose (Recommended)

services:
api:
image: codfish/json-server:1.0.0-beta.12ports:
- 3000:3000volumes:
- ./my-db.js:/app/db.js:delegated
- ./my-middleware.js:/app/middleware.js:delegated

Run docker compose up api. Visit http://localhost:3000/ to see your API.

Docker cli

Tip

The server listens on 0.0.0.0:3000 by default inside the container. Mapping a different host port (e.g. -p 3001:3000) will work fine, but your logs will still say localhost:3000. For the best DX, pass the PORT env var to sync them (e.g., docker run -e PORT=3001 -p 3001:3001 ...).

docker run -d -e PORT=3001 -p 3001:3001 \
-v ./my-db.js:/app/db.js \
-v ./my-middleware.js:/app/middleware.js \
codfish/json-server:1.0.0-beta.12

Advanced

Set configuration via environment variables.

services:
json-server:
image: codfish/json-server:1.0.0-beta.12volumes:
- ./db.ts:/app/db.ts:delegated
- ./middleware.ts:/app/middleware.ts:delegatedenvironment:
DEPENDENCIES: chance@1 node-emoji@1

See all the available options below.

Important Usage Notes

  • IDs must be strings. json-server v1 uses strict equality (===) to match URL params against record IDs. Since URL params are always strings, integer IDs will never match on individual resource lookups (e.g., GET /users/1). Use string IDs like "1" or UUIDs.
  • All mounted files should use ESM syntax (import/export default).
  • All files should be mounted into the /app directory in the container.
  • The following files are special and will "just work" when mounted over:
    • /app/db.{ts,js,json} - The database file. JS/TS files must export default a function that returns your data.
    • /app/middleware.{ts,js} - Custom middleware file. Must export default a (req, res, next) function.
    • /public - Static files directory.

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. Faker is included in the image to help facilitate doing these sorts of things inside your db or middleware files. For example:

// db.jsimport{faker}from'@faker-js/faker';exportdefault()=>({posts: faker.helpers.multiple(()=>({id: faker.string.uuid(),title: faker.lorem.words(3),body: faker.lorem.paragraphs(3),}),{count: 100},),});

Middleware

Mount a middleware.{js,ts} file to add custom middleware that runs before json-server's route handlers. This is useful for things like authentication checks, request mutation, custom headers, and logging.

// middleware.tsexportdefault(req,res,next)=>{// Add custom response headersres.set('X-Custom-Header','my-value');// Require authorization for non-browser requestsif(!req.accepts('html')&&!req.header('Authorization')){res.status(401).send();return;}next();};

Typescript Support

Important

  • Use export default ... for default exports from your ts files.
  • TS is configured to target ESM (es2022 / nodenext).
  • A path alias is configured for your convenience to map to the /app directory where all server files should be mounted.
  • When importing local files in TS, use the .js extension (e.g., import foo from './fixtures/bar.js').
// db.tsimport{faker}from'@faker-js/faker';interfaceDatabase{posts: Array<{id: string;title: string;body: string;}>;}exportdefault(): Database=>({posts: faker.helpers.multiple(()=>({id: faker.string.uuid(),title: faker.lorem.words(3),body: faker.lorem.paragraphs(3),}),{count: 10},),});
docker run -d -e PORT=3000 -p 3000:3000 -v ./db.ts:/app/db.ts codfish/json-server:1.0.0-beta.12

Options

json-server v1 has significantly fewer configuration options than v0. The following environment variables are supported:

OptionDescriptionDefault
DEPENDENCIESInstall extra npm dependencies in the container for you to use in your server files.
STATICServe an additional static files directory (./public is always served)
PORTSet the port the server listens on inside the container.3000

Caution

The DEPENDENCIES env var runs pnpm add with whatever packages you specify. A malicious package's install script will execute inside the container. Only use packages you trust.

Query Parameters

json-server v1 supports the following query parameters:

  • Pagination: _page and _per_page (e.g., ?_page=1&_per_page=10)
  • Sorting: _sort (e.g., ?_sort=id or ?_sort=-id for descending)
  • Filtering: Use field names directly (e.g., ?title=foo)
  • Embedding: _embed (e.g., ?_embed=comments)

Note: The _expand and q (full-text search) parameters from v0 have been removed in v1.

Maintaining/Contributing

This project dogfoods itself. To test it directly you can run:

git clone git@github.com:codfish/json-server-docker.git
cd json-server-docker
docker compose up -d

If you want to test it locally without docker, you can pnpm install and then pnpm start to run the server directly on your machine.

Examples

The docker-compose.yml defines several services that exercise different features. Each one maps to a directory in examples/.

ServicePortDescription
docker-compose up basic3000Default db.js with faker-generated data
docker-compose up typescript9998TypeScript db & middleware
docker-compose up json-db9997Plain JSON database file
docker-compose up middlewares9996Custom middleware that sets response headers
docker-compose up deps9995Extra dependencies installed via DEPENDENCIES envar
docker-compose up static9993Custom public directory with static HTML
docker-compose up dags9994Supporting files mounted alongside the db

Run all examples:

docker compose up --build

Or run a specific one:

docker compose up --build typescript

Visit the corresponding port (e.g., http://localhost:9998) to verify.

To update:

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

Visit http://localhost:3000. Update db.js or middleware.js to test out functionality. Changes should propagate automatically, just refresh the page.

Releasing

New version:

git tag -m '1.0.0-beta.12' 1.0.0-beta.12
git push origin 1.0.0-beta.12

Pushing a tag triggers the release workflow, which builds and pushes the Docker image tagged with the version.

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 1.0.0-beta.12 -m "Update 1.0.0-beta.12 tag"&& git push origin 1.0.0-beta.12 --force

Force-pushing the tag re-triggers the release workflow to rebuild the image.

About

Docker image to easily integrate json-server mock api's into your app.

Resources

Stars

19 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Repository files navigation

json-server-docker

Dockerized json-server for building a full fake RESTful API.

json-server versionpullsMIT License

Created with <3 for front-end developers who need a quick back-end for prototyping and mocking.

Note: This version uses json-server v1 (beta), which is a ground-up rewrite. Many CLI options from v0 have been removed. See Options for the current set of supported configuration.

Features

  • 💨 Up and running quickly - Spin up a RESTful mock API in seconds.
  • ⚙️ Configurable - Supports json-server v1 configuration via environment variables.
  • TypeScriptTypescript support - Use TS for your db, middleware, or any file you mount into the container.
  • 💻 Mount in any supporting files you'd like! - For instance, want to use your custom data fixtures, utils, etc. in your db/middleware? Mount them in, import & prosper.
  • 📦 Useful dependencies are pre-installed in the image for your convenience. Use lodash-es & @faker-js/faker in any of the files powering your mock api.
  • 🧳 Install your own dependencies - Use the DEPENDENCIES envvar to pass a list of additional npm dependencies to use in your server files.
  • 🔂 Hot reloading the server on any changes.

Getting Started

Latest Version: codfish/json-server:1.0.0-beta.12

Note

You are reading the docs for the v1 beta of json-server-docker. If you'd prefer the stable v0 release, run docker run -p 3000:80 codfish/json-server:0.17.4 and view the v0 documentation.

By default, the image runs an instance of json-server with some dummy data for show. Spin up the example mock api in seconds.

# Visit <http://localhost:3000> to see it in action.
docker run -p 3000:3000 codfish/json-server

That's all good, but not very useful to you. You're meant to mount in your own db file(s) into the container. Read on for usage...

Usage

Warning

It's recommended to specify the tag of the image you want rather than using the latest image, which might break. Image tags are based off of the release versions for json-server. However there is not an image for every version. See the available versions on Docker Hub.

This project actually dogfoods itself. View the docker-compose.yml & the examples/ directory to see various usage examples. Also visit the json-server docs for more detailed examples on how to use the tool.

Examples

Docker Compose (Recommended)

services:
api:
image: codfish/json-server:1.0.0-beta.12ports:
- 3000:3000volumes:
- ./my-db.js:/app/db.js:delegated
- ./my-middleware.js:/app/middleware.js:delegated

Run docker compose up api. Visit http://localhost:3000/ to see your API.

Docker cli

Tip

The server listens on 0.0.0.0:3000 by default inside the container. Mapping a different host port (e.g. -p 3001:3000) will work fine, but your logs will still say localhost:3000. For the best DX, pass the PORT env var to sync them (e.g., docker run -e PORT=3001 -p 3001:3001 ...).

docker run -d -e PORT=3001 -p 3001:3001 \
-v ./my-db.js:/app/db.js \
-v ./my-middleware.js:/app/middleware.js \
codfish/json-server:1.0.0-beta.12

Advanced

Set configuration via environment variables.

services:
json-server:
image: codfish/json-server:1.0.0-beta.12volumes:
- ./db.ts:/app/db.ts:delegated
- ./middleware.ts:/app/middleware.ts:delegatedenvironment:
DEPENDENCIES: chance@1 node-emoji@1

See all the available options below.

Important Usage Notes

  • IDs must be strings. json-server v1 uses strict equality (===) to match URL params against record IDs. Since URL params are always strings, integer IDs will never match on individual resource lookups (e.g., GET /users/1). Use string IDs like "1" or UUIDs.
  • All mounted files should use ESM syntax (import/export default).
  • All files should be mounted into the /app directory in the container.
  • The following files are special and will "just work" when mounted over:
    • /app/db.{ts,js,json} - The database file. JS/TS files must export default a function that returns your data.
    • /app/middleware.{ts,js} - Custom middleware file. Must export default a (req, res, next) function.
    • /public - Static files directory.

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. Faker is included in the image to help facilitate doing these sorts of things inside your db or middleware files. For example:

// db.jsimport{faker}from'@faker-js/faker';exportdefault()=>({posts: faker.helpers.multiple(()=>({id: faker.string.uuid(),title: faker.lorem.words(3),body: faker.lorem.paragraphs(3),}),{count: 100},),});

Middleware

Mount a middleware.{js,ts} file to add custom middleware that runs before json-server's route handlers. This is useful for things like authentication checks, request mutation, custom headers, and logging.

// middleware.tsexportdefault(req,res,next)=>{// Add custom response headersres.set('X-Custom-Header','my-value');// Require authorization for non-browser requestsif(!req.accepts('html')&&!req.header('Authorization')){res.status(401).send();return;}next();};

Typescript Support

Important

  • Use export default ... for default exports from your ts files.
  • TS is configured to target ESM (es2022 / nodenext).
  • A path alias is configured for your convenience to map to the /app directory where all server files should be mounted.
  • When importing local files in TS, use the .js extension (e.g., import foo from './fixtures/bar.js').
// db.tsimport{faker}from'@faker-js/faker';interfaceDatabase{posts: Array<{id: string;title: string;body: string;}>;}exportdefault(): Database=>({posts: faker.helpers.multiple(()=>({id: faker.string.uuid(),title: faker.lorem.words(3),body: faker.lorem.paragraphs(3),}),{count: 10},),});
docker run -d -e PORT=3000 -p 3000:3000 -v ./db.ts:/app/db.ts codfish/json-server:1.0.0-beta.12

Options

json-server v1 has significantly fewer configuration options than v0. The following environment variables are supported:

OptionDescriptionDefault
DEPENDENCIESInstall extra npm dependencies in the container for you to use in your server files.
STATICServe an additional static files directory (./public is always served)
PORTSet the port the server listens on inside the container.3000

Caution

The DEPENDENCIES env var runs pnpm add with whatever packages you specify. A malicious package's install script will execute inside the container. Only use packages you trust.

Query Parameters

json-server v1 supports the following query parameters:

  • Pagination: _page and _per_page (e.g., ?_page=1&_per_page=10)
  • Sorting: _sort (e.g., ?_sort=id or ?_sort=-id for descending)
  • Filtering: Use field names directly (e.g., ?title=foo)
  • Embedding: _embed (e.g., ?_embed=comments)

Note: The _expand and q (full-text search) parameters from v0 have been removed in v1.

Maintaining/Contributing

This project dogfoods itself. To test it directly you can run:

git clone git@github.com:codfish/json-server-docker.git
cd json-server-docker
docker compose up -d

If you want to test it locally without docker, you can pnpm install and then pnpm start to run the server directly on your machine.

Examples

The docker-compose.yml defines several services that exercise different features. Each one maps to a directory in examples/.

ServicePortDescription
docker-compose up basic3000Default db.js with faker-generated data
docker-compose up typescript9998TypeScript db & middleware
docker-compose up json-db9997Plain JSON database file
docker-compose up middlewares9996Custom middleware that sets response headers
docker-compose up deps9995Extra dependencies installed via DEPENDENCIES envar
docker-compose up static9993Custom public directory with static HTML
docker-compose up dags9994Supporting files mounted alongside the db

Run all examples:

docker compose up --build

Or run a specific one:

docker compose up --build typescript

Visit the corresponding port (e.g., http://localhost:9998) to verify.

To update:

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

Visit http://localhost:3000. Update db.js or middleware.js to test out functionality. Changes should propagate automatically, just refresh the page.

Releasing

New version:

git tag -m '1.0.0-beta.12' 1.0.0-beta.12
git push origin 1.0.0-beta.12

Pushing a tag triggers the release workflow, which builds and pushes the Docker image tagged with the version.

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 1.0.0-beta.12 -m "Update 1.0.0-beta.12 tag"&& git push origin 1.0.0-beta.12 --force

Force-pushing the tag re-triggers the release workflow to rebuild the image.

About

Docker image to easily integrate json-server mock api's into your app.

Resources

Stars

19 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages