3
3
Docker image for building a [ json-server] ( https://github.com/typicode/json-server ) application.
4
4
5
5
This image will set up the application for you and allow you to simply mount in your "database
6
- source file". Currently it's required that file be ` index .js` . You can also optionally mount in a
6
+ source file", which by convention needs to be named ` db .js` . You can also optionally mount in a
7
7
` middleware.js ` and ` routes.json ` file.
8
8
9
9
Example ` docker-compose.yml ` file:
@@ -12,32 +12,103 @@ Example `docker-compose.yml` file:
12
12
version : ' 3'
13
13
14
14
services :
15
- docs :
16
- image : codfish/json-server:0.14.2
17
- command : npm run dev
15
+ json-server :
16
+ image : codfish/json-server:0.15.0
18
17
volumes :
19
- - ./index .js:/app/index .js:delegated
18
+ - ./db .js:/app/db .js:delegated
20
19
- ./routes.json:/app/routes.json:delegated
21
20
- ./middleware.js:/app/middleware.js:delegated
22
21
` ` `
23
22
24
23
Image versions are based off of the
25
24
[release versions for json-server](https://github.com/typicode/json-server/releases). However there
26
25
is not an image for every version. See the available versions
27
- here](https://hub.docker.com/r/codfish/json-server).
26
+ [here](https://hub.docker.com/r/codfish/json-server).
27
+
28
+ ## Usage
29
+
30
+ The container runs the ` json-server` cli 2 ways. By default it will run it directly but you can
31
+ easily override the command by running it through nodemon. The image is setup to do this already
32
+ through npm. Just change the command to `npm run dev`.
33
+
34
+ ` ` ` yml
35
+ version: '3'
36
+
37
+ services:
38
+ json-server:
39
+ image: codfish/json-server:0.15.0
40
+ command: npm run dev
41
+ ` ` `
42
+
43
+ To test this image directly you can run :
44
+
45
+ ` ` ` sh
46
+ git clone [email protected] :codfish/json-server-docker.git
47
+ docker-compose up -d
48
+ ` ` `
49
+
50
+ It's recommended to first install & start [`docker-proxy`](https://github.com/aj-may/docker-proxy).
51
+ The server will then be available at <http://json-server.docker>.
52
+
53
+ # ## Options
54
+
55
+ `json-server` cli options : <https://github.com/typicode/json-server#cli-usage>
28
56
29
- ## Helper Libraries
57
+ For the most part this image will rely on the default options that `json-server` has set. For
58
+ certain options like `--port` and `--host`, it overrides the defaults with it's own defaults that
59
+ work well with containers out of the box. We've also set a default routes & middleware filename so
60
+ all you need to do is mount over the files. No configuration is necessary for that.
61
+
62
+ However you still have the ability to override _almost_ every option yourself as well. Options
63
+ should be passed in as environment variables. The currently supported options available for :
64
+
65
+ | Option | Description | Default |
66
+ | ------------- | ---------------------------------------------------------------- | ----------------------------------------------------------- |
67
+ | `PORT` | Set port | `80` |
68
+ | `HOST` | Set host | `0.0.0.0` |
69
+ | `ROUTES` | Path to routes file | `routes.json` (Stored in image, optionally mount over it) |
70
+ | `MIDDLEWARES` | Path to middleware file | `middleware.js` (Stored in image, optionally mount over it) |
71
+ | `CONFIG` | Path to config file | Defers to `json-server` default |
72
+ | `ID` | Set database id property (e.g. `address`) | Defers to `json-server` default |
73
+ | `FKS` | Set foreign key suffix, (e.g. `Address` as in `contractAddress`) | Defers to `json-server` default |
74
+ | `DELAY` | Add delay to responses (ms) | - |
75
+ | `STATIC` | Set static files directory | Defers to `json-server` default |
76
+ | `QUIET` | Suppress log messages from output | Boolean flag only true if set |
77
+ | `NO_GZIP` | Disable GZIP Content-Encoding | Boolean flag only true if set |
78
+ | `NO_CORS` | Disable Cross-Origin Resource Sharing | Boolean flag only true if set |
79
+ | `READ_ONLY` | Allow only GET requests | Boolean flag only true if set |
80
+
81
+ For details on the options
82
+ [view `json-server`'s documentation](https://github.com/typicode/json-server#cli-usage).
83
+
84
+ # ## Full Example
85
+
86
+ Here's a recommended setup for local development with some optional overrides as an example.
87
+
88
+ ` ` ` yaml
89
+ services:
90
+ json-server:
91
+ image: codfish/json-server:0.15.0
92
+ command: npm run dev
93
+ volumes:
94
+ - ./db.js:/app/db.js:delegated
95
+ - ./routes.json:/app/routes.json:delegated
96
+ - ./middleware.js:/app/middleware.js:delegated
97
+ environment:
98
+ VIRTUAL_HOST: json-server.docker
99
+ FKS: Address
100
+ ID: address
101
+ NO_CORS: 'true'
102
+ NO_GZIP: 'true'
103
+ ` ` `
104
+
105
+ # # Database File
30
106
31
107
When building your mock api's you'll most like want to generate some fake data and return a number
32
108
of items for a specific collection. I've included [Lodash](https://lodash.com/) &
33
109
[faker.js](https://github.com/Marak/faker.js) in the image to help facilitate doing these sorts of
34
110
things inside your source file, or middleware for that matter. Here's an example of what I mean :
35
111
36
- #### index.js
37
-
38
- The source file you provide to json-server needs to be a json file, or js file that export's an
39
- object. You will have access to ` faker.js` and `Lodash` to help you out.
40
-
41
112
` ` ` js
42
113
const faker = require('faker');
43
114
const times = require('lodash/times');
@@ -55,5 +126,4 @@ module.exports = () => ({
55
126
56
127
# # Todo
57
128
58
- - Support multiple database file types (`db.json` vs `index.js` for instance)
59
129
- Add examples of using this with docker cli, without a compose file
0 commit comments