I have a server-api app.
In my localhost, I just need 2 commands to launch the app:
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?
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.