json-server icon indicating copy to clipboard operation
json-server copied to clipboard

Be clear at README.MD about Docker

Open kafeltz opened this issue 5 years ago • 3 comments

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.

kafeltz avatar Aug 14 '19 13:08 kafeltz

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:

  1. --host 0.0.0.0 must be used otherwise it'll not work from host machine;
  2. 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'

ernescz avatar Sep 23 '21 09:09 ernescz

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

tobiashochguertel avatar Jan 25 '24 12:01 tobiashochguertel

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😌

AceyKubbo avatar Jan 31 '24 06:01 AceyKubbo