0

I have a React app that is connected to a json server which I am trying to run in a docker container. The Dockerfile is as follows:

# base image
FROM node:alpine

# set working directory
WORKDIR '/app'

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install and cache app dependencies
COPY package.json /app/package.json
RUN npm install --silent
RUN npm install react-scripts@3.0.1 -g --silent
RUN npm i serve
COPY . . 

# start app
CMD ["npm", "start"]
CMD ["node", "server.js"]
CMD ["serve", "-p", "3000", "-s", "."]

The json server is as follows:

var jsonServer  = require('json-server')
var server      = jsonServer.create()
var router      = jsonServer.router(require('./db.js')())
var middlewares = jsonServer.defaults()

server.use(middlewares)
server.use(router)
server.listen(8080, function () {
console.log('JSON Server is running')
})

The problem is when I run docker run -it -p 3000:3000 personal-react-website it says my app is running on http://localhost:3000 but when I go there, it is not showing me anything. I was wondering if anyone could help me figure out why this is not the case?

In my test environment, my React app is on port 3000 and the json server is on 8080 and I just type

npm start
node server.js

any everything works fine.

praventz
  • 51
  • 10

1 Answers1

1

You app server and npm start never executed Because of CMD behaviour.

There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect.

So you need entrypoint to handle this using some entrypoint or you can use some node process manager to start all application by the process manager.

Or if you want to go for a simple solution then you can try

How can I run multiple npm scripts in parallel?

Adiii
  • 54,482
  • 7
  • 145
  • 148
  • thanks, I ended up running 2 containers with a docker-compose file. Now it runs on 192.168.99.100:3000, 1 more question if you don't mind: I'm trying to connect to my json server which runs on 192.168.99.100:8080, but is there a generic way to connect to it? I read that you can refer to the container name specific i.e. json_server:8080 or something of the sort – praventz Sep 19 '19 at 15:10
  • Yes you referred with container name and that is most generic way, if the is linking or in same docker compose file – Adiii Sep 19 '19 at 15:18