json-server
json-server copied to clipboard
Be clear at README.MD about Docker
I want to suggest an idea do let super clear in the README.MD by opening an section with title Docker
saying at least the need open the server with --host 0.0.0.0
. It's subtle problem if you forget.
And maybe another item to say is about the --watch with the volume.
Just to make clear for those having the same issue as me and @kafeltz - new data not showing up when saved to the db.json
file (--watch
not working basically) when running inside a container:
-
--host 0.0.0.0
must be used otherwise it'll not work from host machine; - Docker volume (not direct mounting of
db.json
file) must be used.
Here are Dockerfile and docker-compose.yaml
for someone running in the same problem:
Dockerfile
WORKDIR /data
RUN npm install -g json-server
CMD ["json-server", "--watch", "db.json", "--host", "0.0.0.0"]
docker-compose.yaml
version: '3'
services:
json-server:
container_name: json-server
build:
context: .
dockerfile: Dockerfile
volumes:
# - Assuming your db.json is located in '/Users/meh/code/data/db.json' (adjust path to the correct one):
- '/Users/meh/code/data:/data'
ports:
- '3000:3000'
File: Dockerfile
FROM node:18.16.0-alpine3.17
RUN mkdir -p /app
WORKDIR /app
RUN npm install -g json-server
RUN mkdir /app/public
EXPOSE 3000
VOLUME [ "/app/public", "/app/db.json" ]
CMD [ "npx", "json-server", "/app/db.json", "--host", "0.0.0.0", "-s", "/app/public"]
File: docker-compose.yml
version: '3.9'
services:
api:
build: .
volumes:
- ./db.json:/app/db.json:delegated
- ./public:/public:delegated
restart: unless-stopped
File:
Dockerfile
FROM node:18.16.0-alpine3.17 RUN mkdir -p /app WORKDIR /app RUN npm install -g json-server RUN mkdir /app/public EXPOSE 3000 VOLUME [ "/app/public", "/app/db.json" ] CMD [ "npx", "json-server", "/app/db.json", "--host", "0.0.0.0", "-s", "/app/public"]
File:
docker-compose.yml
version: '3.9' services: api: build: . volumes: - ./db.json:/app/db.json:delegated - ./public:/public:delegated restart: unless-stopped
you should commit a PR😌