node App not reachable from outside docker - node.js

Im having troubles while trying to access my NodeJs app on a container.
Resume:
I customized my own image to run pm2 and loopback on my container.
I configured my app to listen to a mongodb on another container within a docker network. This works fine.
I created my docker-compose.yml file to looks like this:
version: '3.5'
services:
webapp:
expose:
- "3000"
ports:
- "3000:3000"
volumes:
- ${NODEJS_PATH}:${NODEJS_PATH_DESTIONATION}
image: ${CUSTOMIZED_IMAGE}
networks:
default:
ipv4_address: ${NODEJS_IP}
db:
image: mongo:4.2.6
container_name: mongodb
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE}
volumes:
- ${MONGO_SCRIPT_PATH}:${MONGO_SCRIPT_DESTINATION_PATH}
networks:
default:
ipv4_address: ${MONGO_INITDB_IP}
networks:
default:
external:
name: ${CUSTOMIZED_NET}
I ran my webapp with docker-compose run webapp for now for customize purposes.
Inside this container I execute the next cmd
pm2 start app.js --env environment_variables
It runs the NodeJS App on http://localhost:3000 inside the docker
I tried to check this from a browser http://{DOCKER_IP}:3000 but it's not working.
I would be eternally grateful with any answer to this trouble.

We used nginx to proxy_pass the service from the docker to the outside world.
We did it this way because we need other services on the docker_network to listen to this one.
Here is the config from the nginx
server {
listen 80;
server_name domain.local;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}

Related

Internal communication between two docker container (NGINX | Nodejs)

I am trying to communicate my frontend container with my nodejs backend container. In the docker cli when I ping to other container it shows no error but In the browser hitting localhost returns me a "504 Gateway Time-out"
docker-compose.yml
version: '3'
services:
nodejs-app:
build:
context: ./nodedocker_app
container_name: nodejsserver
hostname: nodejsserver
ports:
- "3000:3000"
networks:
- example-net
depends_on:
- mongo
mongo:
container_name: mongo
image: mongo
volumes:
- ./data:/data/db
ports:
- "27017:27017"
networks:
- example-net
nginx:
build:
context: ./nginx
container_name: nginx
hostname: nginx
ports:
- "80:80"
depends_on:
- nodejs-app
networks:
- example-net
networks:
example-net:
external: true
default.conf (nginx)
server {
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://nodejsserver:3000;
}
}

Nginx config for multiple react apps (docker-compose)

For development, I'm trying to run two react apps, locally, on the same port (so both can share localStorage), where app1 runs on localhost:8000 and app2 runs on localhost:8000/app2. However, with the setup below I have the issue that all requests to localhost:8000/app2/... are routed to app1.
Is there a way around this?
nginx.conf
Update: moved /app2 block in front of / (see comment).
server {
listen 8000;
server_name localhost;
location /app2 {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://app2:3001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://app1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
docker-compose.yml
version: "3"
services:
nginx:
image: nginx:latest
ports:
- "8000:8000"
volumes:
- ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf
app1:
container_name: "app1"
image: node:12
ports:
- "3000:3000"
build:
context: "./app1"
volumes:
- "./app1:/src/app"
app2:
container_name: "app2"
image: node:12
ports:
- "3001:3001"
build:
context: "./app2"
volumes:
- "./app2:/src/app"
Dockerfile app1
And the app2 Dockerfile has EXPOSE 3001 and npm start --port 3001
FROM node:12
WORKDIR /src/app
COPY package*.json ./
RUN npm install
EXPOSE 3000
CMD ["npm", "start", "--port", "3000"]
I'll update this old question with an answer: The problem was not related to nginx or docker-compose. Instead, I was running two create-react-app applications in development mode and I forgot to set { "homepage": "/app2" } in package.json or set PUBLIC_URL=/app2 as environment variable to serve the app from a subdirectory.

How can I use nginx for load balancer -simple node.js with docker compose?

I am googling and trying to use nginx for simple node.js application with docker compose. But when I look at localhost:8081 My request returned me as 502 bad gate way. How can I handle this error?
My file structure below:
Load-Balancer:
DockerFile:
FROM nginx:stable-alpine
LABEL xxx yyyyyy
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 8081
CMD ["nginx", "-g", "daemon off;"]
nginx.conf:
events { worker_connections 1024; }
http {
upstream localhost {
server backend1:3001;
server backend2:3001;
server backend3:3001;
}
server {
listen 8081;
server_name localhost;
location / {
proxy_pass http://localhost;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
}
}
}
docker-compose.yml
version: '3.2'
services:
backend1:
build: ./backend
tty: true
volumes:
- './backend'
backend2:
build: ./backend
tty: true
volumes:
- './backend'
backend3:
build: ./backend
tty: true
volumes:
- './backend'
loadbalancer:
build: ./load-balancer
tty: true
links:
- backend1
- backend2
- backend3
ports:
- '8081:8081'
volumes:
backend:
My repository: https://github.com/yusufkaratoprak/nginx_docker_loadbalancer
There is no command set for the backend container images to run.
The official nodejs images run node by default, which will start the cli when a tty exists. I assuming the tty was enabled in the compose definition to keep the containers from crashing.
A simple Dockerfile for an app would look like:
FROM node:boron
WORKDIR /app
COPY src/. /app/
RUN npm install
EXPOSE 3001
CMD [ "node", "/app/index.js" ]
A tty shouldn't be needed for most daemons, remove the tty settings from the docker-compose.yml. The links are also redundant in version 2+ compose files.
version: '3.2'
services:
backend1:
build: ./backend
volumes:
- './backend'
backend2:
build: ./backend
volumes:
- './backend'
backend3:
build: ./backend
volumes:
- './backend'
loadbalancer:
build: ./load-balancer
ports:
- '8081:8081'
volumes:
backend:

node express with docker-compose returns 502 bad gateway

My application is working. console.log is recorded on logs, and able to display process.env.URL on cmd. However, on the browser, it returns error 502, bad gateway.
This is my docker-compose
version: "2"
volumes:
mongostorage:
services:
app:
build: ./app
ports:
- "3000"
links:
- mongo
- redis
command: node ./bin/www
nginx:
build: ./nginx
ports:
- "80:80"
links:
- app:app
mongo:
image: mongo:latest
environment:
- MONGO_DATA_DIR=/data/db
volumes:
- mongostorage:/data/db
ports:
- "27017:27017"
redis:
image: redis
volumes:
- ./data/redis/db:/data/db
ports:
- "6379:6379"
nginx.conf
events {
worker_connections 1024;
}
http{
upstream app.dev{
least_conn;
server app:3000 weight=10 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
server_name app.dev;
location / {
proxy_pass http://app;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
}
This is app/Dockerfile
FROM node:6.3
WORKDIR /var/www/app
RUN mkdir -p /var/www/app
COPY package.json /var/www/app
RUN npm install
COPY . /var/www/app
This is nginx/Dockerfile
FROM nginx:latest
EXPOSE 80
COPY nginx.conf /etc/nginx/nginx.conf
In your nginx.conf file, In proxy_pass_http you should use the server name i.e
proxy_pass http://app.dev/ instead of proxy_pass http://app and it should work;

Nginx connection refused, when trying to connect to node app as reverse proxy

I trying to build a webapp with docker containers and I am getting connection refused when trying to run Nginx as a reverse proxy to my node app. I am not sure if it is a nginx server config issue or a docker-compose config issue.
[error] 5#5: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: foo.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:7770/", host: "foo.com"
I get this error when hitting foo.com, the weird thing is that my app works when the port number is referenced, so foo.com:7770 runs the app.
My nginx server config:
server {
listen 80;
server_name foo.com;
port_in_redirect off;
autoindex on;
location / {
proxy_pass http://127.0.0.1:7770;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
My Docker Compose File: (There might be some redundant things in here)
version: "2"
services:
nginx:
build: ./nginx
ports:
- "80:80"
depends_on:
- app
links:
- app
app:
build:
context: .
dockerfile: DockerFile
ports:
- "7770:7770"
links:
- mongo
depends_on:
- mongo
mongo:
image: mongo
ports:
- "27017:27017"
volumes_from:
- mongodata
depends_on:
- mongodata
mongodata:
image: tianon/true
volumes:
- /data/db
My Node Dockerfile:
FROM node:latest
ADD package.json /tmp/package.json
RUN cd /tmp && npm install
RUN mkdir -p /opt/app && cp -a /tmp/node_modules /opt/app/
WORKDIR /opt/app
ADD . /opt/app
EXPOSE 7770
CMD ["npm", "start"]
My ngnix Dockerfile
FROM nginx:1.10
COPY default.conf /etc/nginx/conf.d/default.conf
On npm start this will run:
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.listen(7770, function(err) {
if (err) {
console.log(err);
return;
}
console.log('Listening at http://localhost:7770');
});
This is my first run at docker, so I may have muddled up a few things. Also I am pointing foo.com to 127.0.0.1 in /private/etc/hosts.
c-holmes,
First of all, you need to remember each container has its own network stack, so you cannot use localhost inside the container to reach a service running in the docker host.
For this specific project, you will need to point the proxy_pass directive in your Nginx server config to a value what reach the app container. Something like:
proxy_pass http://app:7770;
You will need to do right that because in docker-compose context your container name will be mapped to an internal DNS entry. With that, you will not need to publish 7770 of the app container to the outside world and if your MongoBD will be accessed just by your app container, you will not need to publish the 27017 port either.
If you want to route traffic from nginx to app you have to use ip address or dns of the app container on proxy_pass. With Docker Compose services can discover each other with service names, so change in nginx conf
proxy_pass http://app:7770;
You don't need to publish port 7770 to outside world. Also for mongo you don't need to publish 27017 port.

Resources