Skip to content

Commit 5d20acb

Browse files
committed
feat: total refactor, allow overriding options, bump deps, bump node
1 parent a4c74ca commit 5d20acb

10 files changed

+1075
-1787
lines changed

.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.lintstagedrc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2-
'*.{json,md}': ['prettier --write', 'git add'],
2+
'*.{json}': ['prettier --write', 'git add'],
3+
'*.md': ['prettier --write', 'markdownlint', 'git add'],
34
'*.js': ['eslint --fix', 'git add'],
45
};

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ sudo: false
22

33
language: node_js
44

5-
node_js: '10'
5+
node_js: '12'
66

77
cache:
88
directories:

Dockerfile

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
FROM node:11.12
1+
FROM node:12.4
22

33
RUN mkdir /app
44
WORKDIR /app
55

6+
RUN npm install -g json-server
67
COPY package.json package-lock.json ./
78
RUN npm ci --only="prod"
89

9-
COPY index.js middleware.js routes.json ./
10+
COPY ./db.js ./middleware.js ./routes.json ./server.sh ./
1011

1112
EXPOSE 80
12-
CMD npm run start
13+
CMD [ "./server.sh" ]

README.md

+83-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Docker image for building a [json-server](https://github.com/typicode/json-server) application.
44

55
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
77
`middleware.js` and `routes.json` file.
88

99
Example `docker-compose.yml` file:
@@ -12,32 +12,103 @@ Example `docker-compose.yml` file:
1212
version: '3'
1313

1414
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
1817
volumes:
19-
- ./index.js:/app/index.js:delegated
18+
- ./db.js:/app/db.js:delegated
2019
- ./routes.json:/app/routes.json:delegated
2120
- ./middleware.js:/app/middleware.js:delegated
2221
```
2322
2423
Image versions are based off of the
2524
[release versions for json-server](https://github.com/typicode/json-server/releases). However there
2625
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>
2856

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
30106

31107
When building your mock api's you'll most like want to generate some fake data and return a number
32108
of items for a specific collection. I've included [Lodash](https://lodash.com/) &
33109
[faker.js](https://github.com/Marak/faker.js) in the image to help facilitate doing these sorts of
34110
things inside your source file, or middleware for that matter. Here's an example of what I mean:
35111

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-
41112
```js
42113
const faker = require('faker');
43114
const times = require('lodash/times');
@@ -55,5 +126,4 @@ module.exports = () => ({
55126

56127
## Todo
57128

58-
- Support multiple database file types (`db.json` vs `index.js` for instance)
59129
- Add examples of using this with docker cli, without a compose file

index.js db.js

File renamed without changes.

docker-compose.yml

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
version: '3'
22

33
services:
4-
api:
4+
json-server:
55
build: .
66
command: npm run dev
77
volumes:
8-
- ./index.js:/app/index.js
9-
- ./package.json:/app/package.json:delegated
8+
- ./db.js:/app/db.js
109
- ./routes.json:/app/routes.json:delegated
1110
- ./middleware.js:/app/middleware.js:delegated
1211
environment:
13-
VIRTUAL_HOST: json-server-docker.docker
12+
VIRTUAL_HOST: json-server.docker

0 commit comments

Comments
 (0)