Internal communication between two docker container (NGINX | Nodejs) - node.js

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;
}
}

Related

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.

node App not reachable from outside docker

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;
}
}

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:

How to config nginx to request multiples microservices on node

I'm trying to configure nginx to manage all request and redirect to proper microservice on node/express. All microservices works fine, connecting to Redis and Postgres.
I would like to manage without use ports on URL something like...
http://localhost/auth (dockerized microservice 1)
http://localhost/banks (dockerized microservice 2)
http://localhost/users (dockerized microservice 3)
I'm currently using Docker with multiples images, here's most relevant files..
Any idea? Thanks!
docker-compose.yml
version: '3'
services:
nginx:
build: ./nginx
links:
- auth:auth
- banks:banks
- users:users
ports:
- "80:80"
amplify:
build: ./docker-nginx-amplify
environment:
- API_KEY=
- AMPLIFY_IMAGENAME=development
auth:
build: ./api-auth
environment:
- PG_SERVER=172.19.0.3
links:
- redis
- postgres
- banks
ports:
- "8888"
banks:
build: ./api-banks
environment:
- PG_SERVER=172.19.0.3
links:
- redis
- postgres
ports:
- "8888"
users:
build: ./api-users
environment:
- PG_SERVER=172.19.0.3
links:
- redis
- postgres
ports:
- "8888"
redis:
image: redis
ports:
- "6379"
postgres:
image: postgres
hostname: postgres
ports:
- "5432:5432"
environment:
POSTGRES_USER: postgresuser
POSTGRES_PASSWORD: postgresuser
POSTGRES_DB: privatedatabase
restart: unless-stopped
nginx.conf
worker_processes 4;
events { worker_connections 1024; }
http {
upstream node-app {
least_conn;
server auth:8888 weight=10 max_fails=3 fail_timeout=30s;
server banks:8888 weight=10 max_fails=3 fail_timeout=30s;
server users:8888 weight=10 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
location / {
proxy_pass http://node-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;
}
}
}

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;

Resources