typicode / json-server

Get a full fake REST API with zero coding in less than 30 seconds (seriously)
Other
73.02k stars 7.02k forks source link

Data isn't served from the docker container. #1283

Open RashiqAzhan opened 2 years ago

RashiqAzhan commented 2 years ago

I can get the json-server working on my local machine but not from docker. I don't know what gives.

docker-compose.yaml:

version: "3.9"
services:
  app:
    build: .
    ports:
      - "3200:8080"
      - "3201:3000"
    volumes:
      - .:/app
    environment:
      - CHOKIDAR_USEPOLLING=true

Dockerfile:

FROM node:lts-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
RUN npm install @vue/cli -g
COPY . .
EXPOSE 8080
EXPOSE 3000
CMD ["npm", "run", "dev"]

package.json:

{
...
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint",
    "server": "json-server --watch ./db.json --port 3204",
    "generate": "node ./src/database_faker/index.js > ./db.json",
    "dev": "npm run serve & npm run server"
  },
  "dependencies": {
    "autoprefixer": "^9",
    "axios": "^0.24.0",
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-service": "~4.5.0",
    "@vue/compiler-sfc": "^3.0.0",
    "json-server": "^0.17.0",
...
  },
...
}

Sample from db.json:

{
    "books": [
        {
          "id": 1,
          "title": "Human Data Agent",
          "author": "Paula Stamm"
        },
        {
          "id": 2,
          "title": "Forward Data Developer",
          "author": "Joanna Kshlerin"
        }
    ]
}

The data in the db.json appears on the browser when the server is running on the local machine but not when it is running on the container with an exposed port. The default 3000 port is mapped to 3201.

Any help is greatly appreciated.

sundarcodes commented 2 years ago

Please add the --host option to make it accessible from inside a docker container. I tried this in a docker-compose yaml and it worked. Got this from 1009

json-server --watch ./db.json --port 3204 --host 0.0.0.0