Search code examples
dockerjson-server

"json-server" command not found in Docker container even though it is installed


I have a little application that I want to deploy in a docker container and in this container should also run a json-server on a different port to mock an API. During creation of the container the json-server is installed but when I try to run the server using CMD in the Dockerfile I only get the response that the command json-server is not found.

My Dockerfile looks like this:

FROM node:10-alpine as build-step    
RUN mkdir -p /app    
WORKDIR /app    
COPY package.json /app   
RUN npm install -g json-server 
RUN npm install    
COPY . /app    
RUN npm run build --prod 
 
# Stage 2
FROM nginx:1.17.1-alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY --from=build-step /app/dist/my-app /usr/share/nginx/html

CMD json-server --watch services/mock-api/db.json --routes services/mock-api/routes.json --port 3000 --host 0.0.0.0

My .dockerignore file should not be relevant but here it is:

.git
.firebase
.editorconfig
/node_modules
/e2e
/docs
.gitignore
*.zip
*.md

Any idea on what I am doing wrong?


Solution

  • Unfortunately ale917k's answer did not work so I had to alter the Dockerfile a bit after more research:

    # Stage 1
    FROM node:10-alpine as build-step
    RUN mkdir -p /app
    WORKDIR /app
    COPY package.json /app
    RUN npm install
    COPY . /app
    RUN npm run build --prod
    
    # Stage 2
    FROM ubuntu:16.04
    USER root
    WORKDIR /home/app
    COPY package.json /home/app/package.json
    RUN apt-get update
    RUN apt-get -y install curl gnupg
    RUN curl -sL https://deb.nodesource.com/setup_11.x  | bash -
    RUN apt-get -y install nodejs
    RUN apt update
    RUN apt-get -y install nginx
    RUN npm i -g json-server
    
    COPY nginx.conf /etc/nginx/nginx.conf
    COPY --from=build-step /app/dist/my-app/usr/share/nginx/html
    CMD json-server --watch /usr/share/nginx/html/assets/mock-api/db.json --routes /usr/share/nginx/html/assets/mock-api/routes.json --no-cors --host 0.0.0.0
    

    Very important to add: -host 0.0.0.0.