1

I have a server-api app.

In my localhost, I just need 2 commands to launch the app:

  1. npm install
  2. npm start

No more, and it works perfect at the port 3000.

I'm trying to dockerize my server to launch it with docker-compose.

Ok, this is my dockerfile:

FROM node:14-alpine AS development

ENV NODE_ENV development

WORKDIR /app

COPY package.json .
COPY package-lock.json .

RUN npm install

COPY . .

EXPOSE 3000

CMD ["npm", "start"]

I launch the build command to build the image:

docker build --tag server-api . 

And when it finish, I run the container:

docker run -p 80:3000 server-api

The logs is perfect, it says that is running in the port 3000 and that I should connect to localhost:3000 to review my server-api app.

But, I undestand that is the port of the container, I had launch the container with the command -p 80:3000, so I check the port 80 of my computer... but nothing happens.

What should be my troubleshooting?

I already tried to get some info connecting with my container:

docker exec -it <container_id> sh 

But everythings looks perfect and is a simple app.

What Im doing wrong?

  • 1
    Did you check that your Node app listens to the special IP `0.0.0.0`, and **not** just localhost? – ErikMD Dec 12 '21 at 15:47
  • Does this answer your question? [How to convert a Spring-Boot web service into a Docker image?](https://stackoverflow.com/questions/54269973/how-to-convert-a-spring-boot-web-service-into-a-docker-image) – ErikMD Dec 12 '21 at 15:49
  • If my first comment was the crux of the issue, one may want to mark the question as a duplicate of [this answer](https://stackoverflow.com/a/54299199/9164010) (which recaps all requirements for your use case, so it's not specific to Java/Spring-Boot nor Node…) – ErikMD Dec 12 '21 at 15:51
  • Hi ! thanks for your comments but is not Spring Boot, is json-server: https://github.com/typicode/json-server I only have a json as database. This special IP is in all node apps? – Learning Javascript Dec 12 '21 at 16:07
  • Yes, but my remark was independent of the underlying language! whatever is your language (JavaScript/Node, Java/Spring, Python/…), you **have to** listen to the special IP `0.0.0.0`, otherwise the app can't be dockerized :) – ErikMD Dec 12 '21 at 16:08
  • See also [this section of the `json-server` doc°](https://github.com/typicode/json-server#cli-usage) (or the [docker setup outlined in the Node.js doc°](https://nodejs.org/en/docs/guides/nodejs-docker-webapp/)). – ErikMD Dec 12 '21 at 16:11
  • Thanks @ErikMD, it works perfect, I will add the answer here. Thank you ! – Learning Javascript Dec 12 '21 at 17:46
  • 1
    FYI you probably want `npm ci` instead of `npm install` here as install will *update* package-lock.json, not use it to install frozen versions. – jordanm Dec 12 '21 at 19:25

1 Answers1

1

As @ErikMD says, all backend apps need to listen the special IP 0.0.0.0 to be dockerized (this problem doesnt happen with React).

I nedeed to read this post: https://nodejs.org/en/docs/guides/nodejs-docker-webapp/

And to solve my problem I check how to change localhost to 0.0.0.0 as host.

By default my host was localhost.

So, I change this in my package.json:

"scripts": {
  "start": "json-server --watch db.json"
},

For this:

"scripts": {
   "start": "json-server --host 0.0.0.0 --port 8080 --watch db.json"
},

And now finally works perfect.

  • 2
    Great. BTW feel free to accept your own answer! (when you'll be able to do this, maybe you'll need to have more rep to do this) using the green checkmark on the left of your answer. – ErikMD Dec 12 '21 at 19:26