Unable to reach angular running in Docker container [duplicate] - node.js

This question already has answers here:
ng serve not working in Docker container
(7 answers)
Closed 2 years ago.
I am brand new to Docker so pleas bear with me.
Dockerfile:
FROM node:alpine
WORKDIR '/app'
COPY ./package.json .
EXPOSE 4200
RUN npm i
COPY . .
CMD ["npm","start"]
Commands:
docker build -t angu .
docker run -p 4300:4200 angu
I am not sure if I need to include EXPOSE 4200 in Dockerfile. But it is not working either ways.

Your server inside the container is listening on localhost which is different from the localhost on your machine. The container has its own localhost.
To change the app to listen to outside traffic you need to add --host 0.0.0.0 to the ng serve command in the npm start script in package.json like so:
"start": "ng server --host 0.0.0.0"
You don't need to add EXPOSE in your Dockerfile since it doesn't do much practically, it's mostly for documentation purposes. You're already publishing the port with --p. You can read more about this in this article

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.

Unable to access Dockerized Angular application in container [duplicate]

This question already has answers here:
Cannot connect to exposed port of container started with docker-compose on Windows
(4 answers)
Closed 1 year ago.
Dockerfile:
FROM node:current-alpine3.10
RUN mkdir -p /dist/angular
WORKDIR /dist/angular
COPY package.json .
RUN npm install --legacy-peer-deps
COPY . .
EXPOSE 8500
CMD ["npm","run","start:stage-ena-sso"]
package.json:
...
"scripts": {
"start:stage-ena-sso": "ng serve -o -c=stage-ena-sso --port=8500 --baseHref=/"
}...
Folder structure:
Command used to build the Docker image:
docker build . -t ssoadminuiapp
Command used to run the Docker image:
docker run --rm -it -p 8500:8500/tcp ssoadminuiapp:latest
Check if container is running:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8959e5180eba ssoadminuiapp:latest "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 0.0.0.0:8500->8500/tcp recursing_fermat
But accessing localhost:8500 doesnt seem to work:
I'm really new to Docker, so any useful beginner-friendly tips/infos would be very appreciated.
Edit #1, this is the result after running docker run command:
This is the problem:
Angular Live Development Server is listening on localhost:8500
Because your application is bound to localhost inside the container,
the port isn't available for port forwarding. That's like trying to
run a service on your host bound to localhost -- you can reach it
locally, but nothing remote can connect to it (and from a network
perspective, you host is "remote" from the container).
You'll need to configure the Angular server to bind to all addresses
in the container (0.0.0.0). I'm not sure exactly how to do this in
your application; when running ng serve from the command line there
is a --host option to set the bind address.

frontend (React) call backend (express) fails to bind to HOST environment variable when trying to Dockerize

I am trying to dockerize both a frontend made with create-react-app and its express API backend.
The docker containers sit on ec2 instances. I have tried several tutorials and also threads here on stackoverflow, but I can't work out what is going wrong.
The error I get is:
Could not find an open port at ec2-x-xx-xx-xxx.eu-west-2.compute.amazonaws.com.
this refers to my .env file which contains only
HOST= ec2-x-xx-xx-xxx.eu-west-2.compute.amazonaws.com
my Dockerfile for the front end is as follows:
FROM node:12-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --silent
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
whereas the backend's:
FROM node:12-alpine as builder
WORKDIR /app
COPY package.json /app/package.json
RUN npm install
COPY . /app
EXPOSE 3001
CMD ["node", "app.js" ]
I need the backend to run on port 3001 and the frontend to run on 3000. The frontend is bound to the backend api via the proxy line in package.json, where I have placed:
"proxy":"http://ec2-x-xx-xx-xxx.eu-west-2.compute.amazonaws.com:3001",
Both containers build fine. However, running the backend with docker run -p 3001:3001 server and then docker run -p 3000:3000 client spits the error
Attempting to bind to HOST environment variable: ec2-x-xx-xx-xxx.eu-west-2.compute.amazonaws.com
If this was unintentional, check that you haven't mistakenly set it in your shell.
Learn more here: https://xxx.xx/xxx-advanced-config
Could not find an open port at ec2-x-xx-xx-xxx.eu-west-2.compute.amazonaws.com.
Network error message: listen EADDRNOTAVAIL: address not available 10.xx.0.xxx
I have tried running only the server side and then running npm start from my local machine, it works. The problem seems to have to do with docker networking between containers.
I also tried running the server side with the command
docker run -p 10.xx.0.xxx:3000:3000 client
to make sure the client pings the right ip address, however this didn't work.
Could anyone give me some direction please?
If you need more info on the source code, please just leave a comment, I didn't want to clutter the thread by making it longer than it already is..
Thank you

Run node Docker without port mapping

I am very to new Docker so please pardon me if this this is a very silly question. Googling hasn't really produced anything I am looking for. I have a very simple Dockerfile which looks like the following
FROM node:9.6.1
RUN mkdir /usr/src/app
WORKDIR /usr/src/app
ENV PATH /usr/src/app/node_modules/.bin:$PATH
# install and cache app dependencies
COPY package.json /usr/src/app/package.json
RUN npm install --silent
COPY . /usr/src/app
RUN npm start
EXPOSE 8000
In the container the app is running on port 8000. Is it possible to access port 8000 without the -p 8000:8000? I just want to be able to do
docker run imageName
and access the app on my browser on localhost:8000
By default, when you create a container, it does not publish any of its ports to the outside world. To make a port available to services outside of Docker, or to Docker containers which are not connected to the container’s network, use the ‍‍--publish or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host.
Read more: Container networking - Published ports
But you can use docker-compose to set config and run your docker images easily.
First installing the docker-compose. Install Docker Compose
Second create docker-compose.yml beside the Dockerfile and copy this code on them
version: '3'
services:
web:
build: .
ports:
- "8000:8000"
Now you can start your docker with this command
docker-compose up
If you want to run your services in the background, you can pass the ‍‍-d flag (for “detached” mode) to docker-compose up -d and use `docker-compose ps to see what is currently running.
Docker Compose Tutorial
Old question but someone might find it useful:
First get the IP of the docker container by running
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id
Then connect to it from the the browser or using curl using the IP and port exposed :
Note that you will not be able to access the container on 0.0.0.0 because port is not mapped

Docker Compose: Accessing my webapp from the browser

I'm finding the docs sorely lacking for this (or, I'm dumb), but here's my setup:
Webapp is running on Node and Express, in port 8080. It also connects with a MongoDB container (hence why I'm using docker-compose).
In my Dockerfile, I have:
FROM node:4.2.4-wheezy
# Set correct environment variables.
ENV HOME /root
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app/
RUN npm install;
# Bundle app source
COPY . /usr/src/app
CMD ["node", "app.js"]
EXPOSE 8080
I run:
docker-compose build web
docker-compose build db
docker-compose up -d db
docker-compose up -d web
When I run docker-machine ip default I get 192.168.99.100.
So when I go to 192.168.99.100:8080 in my browser, I would expect to see my app - but I get connection refused.
What silly thing have I done wrong here?
Fairly new here as well, but did you publish your ports on your docker-compose file?
Your Dockerfile will simply expose the ports but not open access to your host. Publishing (with the -p flag on a docker run command or ports on a docker-compose file. Will enable access from outside the container (see ports in the docs)
Something like this may help:
ports:
- "8080:8080"

Resources