`NGINX` reverse proxy does not work in `DOKCER` (502 bad gateway) - docker

I have a VPS, my problem is that proxy_pass does not work, I get the 502 badgateway([error] 23#23: *1 connect() failed (111: Connection refused) while connecting to upstream,)
I saw similar questions but each case is different, please help me if you can.
The following is my configs:
NGINX (default.conf)
server {
listen 80 default_server;
server_name _;
location /account {
proxy_pass http://my_app_api:7000/account/;
}
location /info {
return 200 "MHF";
default_type text/plain;
}
}
docker-compose.yml
version: "2.4"
networks:
# internal:
web:
external:
name: my-app-network
services:
my_app_api:
build: .
container_name: my-app-container
env_file: .env
restart: always
stop_grace_period: 2m
networks:
- web
ports:
- 7000:7000
depends_on:
- db
environment:
DB_HOST_LOCAL: $DB_HOST_LOCAL
CHALLENGE_CODE_MIN: $CHALLENGE_CODE_MIN
CHALLENGE_CODE_MAX: $CHALLENGE_CODE_MAX
logging:
driver: json-file
options:
max-size: '12m'
max-file: '2'
db:
image: mongo:4.0
container_name: db_mongo
env_file: .env
restart: always
stop_grace_period: 2m
networks:
- web
environment:
MONGO_DB_URI_LOCAL: $MONGO_DB_URI_LOCAL
MONGO_DB_NAME: $MONGO_DB_NAME
ME_CONFIG_MONGO_DB_USERNAME: $ME_CONFIG_MONGO_DB_USERNAME
ME_CONFIG_MONGO_DB_PASSWORD: $ME_CONFIG_MONGO_DB_PASSWORD
volumes:
- ./my-app-db:/data/db
ports:
- 27017:27017
nginx:
image: nginx:latest
container_name: nginx-container
restart: always
networks:
- web
depends_on:
- my_app_api
ports:
- 80:80
volumes:
- type: bind
source: ./nginx/default.conf
target: /etc/nginx/conf.d/default.conf
read_only: true
logging:
driver: json-file
options:
max-size: '12m'
max-file: '2'
Dockerfile
FROM node:latest
# Create app directory & cd /app
WORKDIR /app
EXPOSE 7000
# Install app dependencies
COPY package.json yarn.lock ./
COPY .env .
# Install dependency
RUN yarn
# Bundle app source
COPY . .
CMD ["yarn", "start:debug"]

Related

Nginx get error when using in a docker-compose file

I use a docker-compose file with 3 service (node-red, mosquitto and mongo) and and I want use nginx as load balancer of node-red service:
version: '2.2'
networks:
Platform-Network:
name: IoT-Network
volumes:
Platform:
MQTT-broker:
DataBase:
Nginx:
services:
Platform:
image: custom-node-red:latest
networks:
- Platform-Network
restart: always
volumes:
- ./node-red:/data
# depends_on:
# - Nginx
MQTT-broker:
container_name: mosquitto
image: eclipse-mosquitto
ports:
- "192.168.100.101:1883:1883"
networks:
- Platform-Network
restart: always
depends_on:
- Platform
volumes:
- ./mosquitto/config:/mosquitto/config
- ./mosquitto/data:/mosquitto/data
- ./mosquitto/log:/mosquitto/log
DataBase:
container_name: mongodb
image: mongo
ports:
- "8083:27017"
networks:
- Platform-Network
restart: always
depends_on:
- Platform
volumes:
- ./mongodb/data:/data/db
- ./mongodb/backup:/data/backup
- ./mongodb/mongod.conf:/etc/mongod.conf
- ./mongodb/log:/var/log/mongodb/
Nginx:
container_name: nginx
image: nginx
ports:
- "4000:4000"
depends_on:
- Platform
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
restart: always
Nginx config file is:
user nginx;
events {
worker_connections 1000;
}
http {
upstream platform {
server platform_1:1880;
server platform_2:1880;
server platform_3:1880;
}
server {
listen [::]:4000;
listen 4000;
location / {
proxy_pass http://platform;
}
}
}
I run my service with docker-compose -f platform.yaml up -d --scale Platform=3 and containers up as show in
this
.But as showen in above image, nginx dosnt up.
Nginx container log get this error and i can't resolve it.
Your nginx service also need to join the same network as Platform:
Nginx:
container_name: nginx
image: nginx
ports:
- "4000:4000"
depends_on:
- Platform
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
restart: always
networks: # add this
- Platform-Network

Dockerize MERN stack using traefik

I did follow this tutorial : https://rafrasenberg.com/posts/docker-container-management-with-traefik-v2-and-portainer/ to build a treafik reverse proxy on my server
And I tested it with a very simple application that render a "hello world" in a index.html:
version: "3"
services:
app:
image: nginx
environment:
PORT: ${PORT}
volumes:
- .:/usr/share/nginx/html/
networks:
- proxy
- default
labels:
- "traefik.enable=true"
- "traefik.docker.network=proxy"
- "traefik.http.routers.app-secure.entrypoints=websecure"
- "traefik.http.routers.app-secure.rule=Host(`my-test.localhost`)"
networks:
proxy:
external: true
it works!
Now I want to go on the next step and use it to build a MERN stack project and I'm a bit lost.
usually I dockerize a mern stack by:
create a dockerfile in /server
create a dockerfile in /client
create a docker-compose on the root directory
version: "3.7"
services:
server:
build:
context: ./server
dockerfile: Dockerfile
image: myapp-server
container_name: myapp-node-server
command: /usr/src/app/node_modules/.bin/nodemon server.js
volumes:
- ./server/:/usr/src/app
- /usr/src/app/node_modules
ports:
- 5000
depends_on:
- mongo
env_file: ./server/.env
environment:
- NODE_ENV=development
networks:
- app-network
- proxy
mongo:
image: mongo
volumes:
- data-volume:/data/db
ports:
- 27017
networks:
- app-network
- proxy
client:
build:
context: ./client
dockerfile: Dockerfile
image: myapp-client
container_name: myapp-react-client
command: npm start
volumes:
- ./client/:/usr/app
- /usr/app/node_modules
depends_on:
- server
ports:
- 3001
networks:
- app-network
labels:
- "traefik.enable=true"
- "traefik.docker.network=proxy"
- "traefik.http.routers.app2-secure.entrypoints=websecure"
- "traefik.http.routers.app2-secure.rule=Host(`test.localhost`)"
networks:
app-network:
driver: bridge
proxy:
external: true
volumes:
data-volume:
node_modules:
web-root:
driver: local
But seems that my proxy not working because the console doesn't return any error and it works on localhost:3000 but on test.localhost I have a "Gateway Timeout" error

Why does the browser not display the site when going through the domain (it works if you go through example.com:8000 or 111.111.111.111)?

I am trying to set up nginx inside docker.
However, I ran into a problem, when going to example.com, the browser does not display the site. But if I go to the ip address (for example 111.111.111.111) or try to go to the site through the port: example.com:8000, then everything works. How to solve this problem?
docker-compose.yml
version: "3"
services:
postgresql:
build:
context: ./docker/postgres
dockerfile: Dockerfile
environment:
- POSTGRES_PASSWORD=password
volumes:
- ./docker/postgres/init.sql:/docker-entrypoint-initdb.d/init.sql
ports:
- "5432:5432"
restart: unless-stopped
django:
build:
context: ./
dockerfile: Dockerfile
env_file:
- ./.env
volumes:
- ./:/usr/src/app
ports:
- "8000:8000"
depends_on:
- postgresql
restart: unless-stopped
nginx:
build:
context: ./docker/nginx
dockerfile: Dockerfile
ports:
- "80:80"
depends_on:
- postgresql
- django
restart: unless-stopped
docker/nginx/nginx.conf
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://django:8000;
}
}
docker/nginx/Dockerfile
FROM nginx:latest
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d

Docker bridge network curl request

I have two docker-compose file.
Firstly upped backend container and inspect the container and copy ip address for extra_hosts for frontend docker-compose file and than upped frontend. But I cant curl port of backend container from frontend container. I get connection time out error. How can I solve the problem?
version: '3'
services:
frontend:
build:
context: .
dockerfile: Dockerfile.dev
image: test/frontend:dev
container_name: test_frontend_dev_1
ports:
- "80:8080"
extra_hosts:
- "api-test.local:192.168.144.2"
# all below is not necessary in final state
networks:
- test_network
volumes:
- .:/var/www/html
- .docker/nginx/nginx.conf:/etc/nginx/nginx.conf
- .docker/config/php.ini:/usr/local/etc/php/conf.d/custom.ini
# user: root
#command: sh
networks:
test_network:
driver: bridge
version: '3'
services:
backend:
build:
context: .
dockerfile: Dockerfile.dev
image: test/backend:dev
container_name: test_backend_dev_1
ports:
- "8888:8080"
expose:
- 8080
# all below is not necessary in final state
volumes:
- .:/var/www/html
# user: root
#command: sh
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'test'
# So you don't have to use root, but you can if you like
MYSQL_USER: 'user'
# You can use whatever password you like
MYSQL_PASSWORD: 'root'
# Password for root access
MYSQL_ROOT_PASSWORD: 'root'
ports:
# <Port exposed> : < MySQL Port running inside container>
- '3306:3306'
expose:
# Opens port 3306 on the container
- '3306'
# Where our data will be persisted
volumes:
- my-db:/var/lib/mysql
networks:
- test_network
networks:
test_network:
driver: bridge
## Names our volume
volumes:
my-db:

Docker-Nginx unexpected redirects (304)

I’m having trouble with docker-compose and nginx. First, I have this docker-compose.yml:
services:
nginx:
build: ./nginx
ports:
- '8080:80'
depends_on:
- web
- api
web:
build: ./web
depends_on:
- api
api:
build: ./api
Both web (port 3000) and api (port 8000) are express servers that return WEB and API respectively. Now, inside ./nginx:
# Dockefile
FROM nginx:alpine
COPY ["default.conf", "/etc/nginx/conf.d/"]
EXPOSE 80
# default.conf
server {
location / {
proxy_pass http://web:3000;
}
location /api {
proxy_pass http://api:8000;
}
}
Now, when I go to http://localhost:8080, I get WEB, but when I go to http://localhost:8080/api, it redirects to http://localhost:1337/api/ and I get nothing (by the way, it throws a 304 error)
However, when I write this default.conf (put api in /)
# default.conf
server {
location / {
proxy_pass http://api:8000;
}
location /api {
proxy_pass http://web:3000;
}
}
I get the same result but in / I get API, so both servers work.
I don't know whether it helps you or not. I am using nginx docker image directly in my docker-compose.
for example.
docker-compose.yml
version: '3'
services:
jobsaf-server:
build:
context: .
dockerfile: Dockerfile.production
container_name: jobsaf-server
ports:
- "3000:3000"
- "5858:5858"
- "35729:35729"
- "6379:6379"
environment:
- NODE_ENV=production
networks:
- front-tier
- back-tier
depends_on:
- "redis"
- "mongo"
links:
- mongo
- redis
volumes:
- ./server:/var/www/app/jobsaf-website/server
nginx:
image: nginx:stable
depends_on:
- jobsaf-server
links:
- jobsaf-server
volumes:
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
ports:
- "0.0.0.0:80:80"
mongo:
image: mongo:latest
container_name: mongo
volumes:
- "db-data:/data/db"
environment:
- MONGO_INITDB_ROOT_USERNAME=${DB_USER}
- MONGO_INITDB_ROOT_PASSWORD=${DB_PASS}
- MONGO_INITDB_DATABASE=admin
ports:
- "0.0.0.0:27017:27017"
networks:
- back-tier
redis:
image: redis
container_name: redis
networks:
- back-tier
volumes:
db-data:
# - /data/db
networks:
front-tier:
back-tier:

Resources