Unable to access docker container from the port mapped by docker - node.js

I have created a docker container but unable to run it on the port mapped by the docker (http://localhost:3000). Below are the details of docker configurations that I am using in my app.
Docker version : 17.05.0-ce
Os : ubuntu 16.04
My Dockerfile:
FROM node:boron
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app
RUN npm install -g bower
RUN npm install -g grunt-cli
RUN npm install
RUN bower install --allow-root
#RUN grunt --force
EXPOSE 3000
CMD ["grunt", "serve"]
Creating docker container:
docker build -t viki76/ng-app .
Running Container:
docker run -p 3000:3000 -d viki76/ng-app
docker ps:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
21541171d884 viki/ng-app "grunt serve" 10 min ago Up 0.0.0.0:3000->3000/tcp
EDIT:
Updated Dockerfile configuration
EXPOSE 9000
$ docker run -p 9000:9000 viki76/ng-app
Running "serve" task
Running "clean:server" (clean) task
>> 1 path cleaned.
Running "wiredep:app" (wiredep) task
Running "wiredep:test" (wiredep) task
Running "concurrent:server" (concurrent) task
Running "copy:styles" (copy) task
Copied 2 files
Done, without errors.
Execution Time (2017-05-17 13:00:13 UTC-0)
loading tasks 189ms ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 88%
loading grunt-contrib-copy 11ms ▇▇ 5%
copy:styles 16ms ▇▇▇ 7%
Total 216ms
Running "postcss:server" (postcss) task
>> 2 processed stylesheets created.
Running "connect:livereload" (connect) task
Started connect web server on http://localhost:9000
Running "watch" task
Waiting...
From Gruntfile.js
connect: {
options: {
port: 9000,
// Change this to '0.0.0.0' to access the server from outside.
hostname: '0.0.0.0',
livereload: 35729
},
Please help me to fix it.
Thanks

I think your problem is that grunt is binding to localhost:9000 - which is internal to the container so the port you're publishing won't have any effect.
It needs to be listening on 0.0.0.0:9000 - I couldn't tell you off hand what your Gruntfile.js should say for that to happen, but off-hand it looks like, out of the box, grunt serve will only serve from localhost.

You're doing everyhthing right, though I'm assuming that your problem is simply configuration.
By default, grunt-serve serves on port 9000, not 3000. Do you want to try and EXPOSE and publish that port?

Try running the container in Host mode:
--net="host"
Pass the above when you run the container.

Related

Docker and Node .mjs files

I have an express application with all the JS files using the *.mjs extension.
So, to start the server I do node index.mjs and it works as expected.
Now I'm trying to containerize the app.
I have this basic Dockerfile
FROM mhart/alpine-node:14
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD node index.mjs
EXPOSE 80
After building (with no errors) and tagging I try to run my application (docker run my-app:latest) it breaks the line in the console but I don't see the console logs of my server.
If I try to hit localhost at port 80, it doesn't work.
I check the containers with docker container ls and I see the container
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ce7ca2a0db96 my-app:latest "/bin/sh -c 'node in…" 6 minutes ago Up 6 minutes 80/tcp clever_bhabha
If I look for logs, nothing.
Does anyone have this issue? Could it be related to .mjs files? If so, is there a way to use them in Docker?
Thanks
I think you need to expose a port different to 80 locally. You should try
docker run -p 8080:80 my-app
Then in localhost:8080 you should reach your app.

Problems to access a recent a vue app deployed in docker container

This is my second VUE web application deploy which I can't access. Inside Docker host Linux running curl localhost:8081, curl: (56) Recv failure: Connection reset by peer
Dockerfile:
FROM node:12
WORKDIR /app_teste
COPY package*.json ./
RUN npm --version
RUN npm install
COPY . .
EXPOSE 8081
CMD ["npm","run","serve"]
dockercompose.yml:
version: '3'
services:
website:
build: .
ports:
- "8081:8081"
container_name: dexter_g_website
I was reading here some topics similars which says about binding to 0.0.0.0 would work, however it never happens. Anyone can help/suggest anything?
docker port dexter_g_webiste
8081/tcp -> 0.0.0.0:8081
Based on Vue CLI documentation, the default port for serve command is 8080.
If you want to serve it to port 8081, try
CMD npm run serve --port 8081
Some more debugging tips:
You can run curl command inside the docker container to debug your app.
SSH into the running container
docker-compose exec -it <container_id> sh
Install CURL
curl the URL and see which is working
curl http://localhost:8080
curl http://localhost:8081

Containerized NodeJS application on podman not reachable

I'm running a nodejs app in a pod (together with a mongo container)
Nodejs app listens on port 3000, which I expose from the container.
I have published port 3000 on the pod.
The container starts successfully (logs verified), but I can't reach my application on the host. When I curl to my app from within the pod it works.
Containers run rootfull, OS: CentOS Linux release 8.0.1905 (Core).
What am I missing?
curl http://localhost:3000
curl: (7) Failed to connect to localhost port 3000: No route to host
podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
30da37306acf registry.gitlab.com/xxx/switchboard:master node main.js 34 minutes ago Up 34 minutes ago 0.0.0.0:3000->3000/tcp switchboard-app
acc08c71147b docker.io/library/mongo:latest mongod 35 minutes ago Up 35 minutes ago 0.0.0.0:3000->3000/tcp switchboard-mongo
podman port switchboard-app
3000/tcp -> 0.0.0.0:3000
app.listen(3000, "0.0.0.0",function () {
console.log('App is listening on port 3000!');
});
FROM node:13
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY /dist/apps/switchboard .
EXPOSE 3000
CMD [ "node", "main.js" ]
If you want to create production docker build, you can go another way. It's not a good idea to do npm install inside container, because it will be too large. Run npm run build first, and then copy builded statics into docker container, created with nginx image.
So your Dockerfile shoud look like:
FROM nginx:1.13.0-alpine
COPY build/* /usr/share/nginx/html/
also specify exposed ports correctly with docker run. So, if you want expose 3000, your steps is:
cd /project/dir
docker build -t switchboard-app .
docker run -d -p 3000:80 --name switchboard-app switchboard-app

error Command failed with signal "SIGINT". when try to terminal the docker container

I have a simple node.js web app.
☁ node-app [master] yarn start
yarn run v1.17.3
$ node ./dist/main.js
Http server is listening on http://localhost:3000
^C
Start the web application in the localhost and use control-c terminal it. It works fine.
Now, I build a docker image for this node web application. Run the docker container and terminal it uses the same way control-c.
☁ node-app [master] docker run -it -p 3000:3000 node-app:1.0
yarn run v1.16.0
$ node ./dist/main.js
Http server is listening on http://localhost:3000
error Command failed with signal "SIGINT".
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
The docker container is terminated as expected. But it seems yarn CLI throw an error for this.
Dockerfile:
# install dependencies ...
# build ...
CMD yarn start
How can I solve this?

Docker container with Angular2 app and NodeJs does not respond

I created new Angular2 app by angular-cli and run it in Docker. But I cannot connect it from localhost.
At first I init app on my local machine:
ng new project && cd project && "put my Dockerfile there" && docker build -t my-ui .
I start it by command:
docker run -p 4200:4200 my-ui
Then try on my localhost:
curl localhost:4200
and receive
curl: (56) Recv failure: Connection reset by peer
Then, I tried switch into running container (docker exec -ti container-id bash) and run curl localhost:4200 and it works.
I also tried to run container with --net= host param:
docker run --net=host -p 4200:4200 my-ui
And it works. What is the problem? I also tried to run container in daemon mode and it did not helped. Thanks.
My Dockerfile
FROM node
RUN npm install -g angular-cli#v1.0.0-beta.24 && npm cache clean && rm -rf ~/.npm
RUN mkdir -p /opt/client-ui/src
WORKDIR /opt/client-ui
COPY package.json /opt/client-ui/
COPY angular-cli.json /opt/client-ui/
COPY tslint.json /opt/client-ui/
ADD src/ /opt/client-ui/src
RUN npm install
RUN ng build --prod --aot
EXPOSE 4200
ENV PATH="$PATH:/usr/local/bin/"
CMD ["npm", "start"]
It seems that you use ng serve to run development server and it by default starts on loop interface (available only on localhost). You should provide specific parameter:
ng serve --host 0.0.0.0
to run it on all interfaces.
You need to change angular-cli to serve the app externally i.e update your npm script to ng serve --host 0.0.0.0

Resources