Github Repository: https://github.com/xshyamx/docker-json-server/
The recommended way to run this container looks like this:
docker run -d \
-p 3000:3000 \
-v /home/user/articles.json:/data/db.json \
json-server
You can supply any number of JSON Server arguments that will be passed through unmodified.
$ docker run -it --rm json-server --help
The following environment variables can be overriden to achieve different behaviour
| Name | Description | Default Value |
|---|---|---|
SERVER_JS | Custom server using json-server as a module | /app/server.js |
DB_JSON | API database json | /data/db.json |
ROUTES_JSON | Custom routes | /data/routes.json |
FILES_JS | Load custome data programmatically | /data/file.js |
HOST | Expose server on host | 0.0.0.0 |
PORT | Expose server on port | 3000 |
The presence of SERVER_JS will override DB_JSON, ROUTES_JSON &
FILES_JS and gets priority over the other environment variables.
Sample files for each of the these files are available in the
/sample folder in the container
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, "title": "json-server", "author": "typicode" },
{ "id": 2, "title": "Run json-server in Docker", "author": "xshyamx" }
],
"comments": [
{ "id": 1, "body": "some comment", "postId": 1 },
{ "id": 2, "body": "This is awesome", "postId": 2 },
{ "id": 3, "body": "Meh!", "postId": 2 }
],
"profile": { "name": "typicode" }
}
Which can be mounted to the default path for DB_JSON
docker run -d \
-p 3000:3000 \
-v /home/user/data.json:/data/db.json \
json-server
or the DB_JSON environment variable can be overriden
docker run -d \
-p 3000:3000 \
-e DB_JSON=/sample/db.json
json-server
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;
}
Which can be mounted as
docker run -d \
-p 3000:3000 \
-v /home/user/index.js:/data/file.js
json-server
or alternatively override the FILES_JS environment variable
docker run -d \
-p 3000:3000 \
-e FILES_JS=/sample/file.js
json-server
If you mount a file to /data/routes.json, it will automatically be
used as the custom routes configuration.
A sample file could look like this:
{
"/posts/:id/comments": "/comments?postId=:id"
}
Which can be mounted to the default path for ROUTES_JSON
docker run -d \
-p 3000:3000 \
-v /home/user/data.json:/data/db.json \
-v /home/user/routes.json:/data/routes.json \
json-server
or the ROUTES_JSON environment variable can be overriden
docker run -d \
-p 3000:3000 \
-e DB_JSON=/sample/db.json
-e ROUTES_JSON=/sample/routes.json
json-server
If you mount a file to /app/server.js, it will be used as a custom
server configuration where json-server is used as a module for a
custom server.
A sample file looks like
// server.js
const jsonServer = require('json-server')
const server = jsonServer.create()
const router = jsonServer.router('db.json')
const middlewares = jsonServer.defaults()
server.use(middlewares)
server.use(router)
const port = process.env.PORT || 3000
server.listen(port, () => {
console.log('JSON Server is running on port', port)
})
Which can be mounted to the default path for SERVER_JS
docker run -d \
-p 3000:3000 \
-v /home/user/server.js:/app/server.js \
json-server
or the SERVER_JS environment variable can be overriden
docker run -d \
-p 3000:3000 \
-e SERVER_JS=/sample/server.js
json-server
Content type
Image
Digest
Size
45.8 MB
Last updated
almost 5 years ago
docker pull xshyamx/json-server