I am dockerizing a Django application, using also Nginx as proxy.
My problems is that the DB is running (non dockerized) on the same machine of the where the dockers are hosted.
Therefore, i have to use network_mode: "host" for the django app in order to connect to the db using localhost.
On the other side, if I do so, the nginx image is not able to reach the Django app anymore and gives
nginx: [emerg] host not found in upstream
My docker compose file is the following:
version: '3.4'
services:
web:
build: .
command: sh /start.sh
networks:
- db_network
- web_network
volumes:
- static_volume:/allarmi/src/AllarmiWs/static
expose:
- 8001
environment:
- DEBUG=0
- DJANGO_SETTINGS_MODULE=AllarmiWs.settings.deploy
nginx:
build: ./nginx
ports:
- 8000:80
depends_on:
- web
volumes:
- static_volume:/allarmi/src/AllarmiWs/static
networks:
- web_network
volumes:
static_volume:
networks:
web_network:
driver: bridge
db_network:
driver: host
This is my nginx.conf file
upstream allarmi {
server web:8001;
}
server {
listen 80;
location / {
proxy_pass http://allarmi;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /static/ {
alias /allarmi/src/AllarmiWs/static/;
}
}
How can I do it
Related
My project has two virtual environments, "main" and "test". I want to unite them on one server. I've been advised to use nginx proxy to do this, but I'm not sure how, especially since each environment already has its own network:
.yml backend of one project (infra/main folder) (the backend of the "test" project is similar):
version: "3.8"
services:
postgres:
image: postgres:13.3
container_name: postgres_main
restart: always
volumes:
- postgres_data_main:/var/lib/postgresql/data
ports:
- 5432:5432
env_file:
- .env-main
networks:
- main_db_network
backend:
<...>
depends_on:
- postgres
env_file:
- .env-main
networks:
- main_db_network
- main_swag_network
migrations:
<...>
networks:
main_db_network:
name: main_db_network
external: true
main_swag_network:
name: main_swag_network
external: true
volumes:
postgres_data_main:
name: postgres_data_main
static_value_main:
name: static_value_main
How do I set up a nginx_proxy to unite the two on one server?
You need to add a new service nginx - probably in a separate docker-compose file
nginx.conf will look like:
upstream main {
server backend:8000; # name of the service in compose file and opened port
}
upstream test {
server test-backend:8000;
}
location /main {
proxy_pass http://main;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /test{
proxy_pass http://test;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
Or instead of changing service names - you may differentiate ports. E.g. main to have mapping 8000:8000 and test e.g. 8001:8000
Dockerfile for nginx:
FROM nginx:1.19.0-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d
docker-compose.yml for serving Nginx
version: "3.8"
services:
nginx:
build: ./nginx
ports:
- "80:80"
networks:
- main_swag_network
- test_swag_network
networks:
main_swag_network:
external: true
test_swag_network:
external: true
It just need to serve nginx and have connections to both networks defined in test and main configs
In my project, I am using Django and nginx, but I want to manage my cloud databases through phpmyadmin.
Django is working fine but I can't do the same with phpmyadmin because it is running in apache at localhost:8080, when I want it to run in nginx at localhost/phpmyadmin.
here is the docker-compose.yml
version: "3.9"
services:
web:
restart: always
build:
context: .
env_file:
- .env
volumes:
- ./project:/project
expose:
- 8000
nginx:
restart: always
build: ./nginx
volumes:
- ./static:/static
ports:
- 80:80
depends_on:
- web
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
restart: always
environment:
PMA_HOST: <host_address>
PMA_USER: <user>
PMA_PASSWORD: <password>
PMA_PORT: 3306
UPLOAD_LIMIT: 300M
ports:
- 8080:80
and nginx default.conf
upstream django{
server web:8000;
}
server{
listen 80;
location / {
proxy_pass http://django;
}
location /pma/ {
proxy_pass http://localhost:8080/;
proxy_buffering off;
}
location /static/ {
alias /static/;
}
}
I hope somebody will be able to tell me how to make nginx work as a reverse proxy for the phpMyAdmin docker container.
If some important information is missing please let me know.
You can access another docker container with its hostname and the internal port (not the exposed one).
Also a rewrite of the url is necessary.
location ~ \/pma {
rewrite ^/pma(/.*)$ $1 break;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://phpmyadmin;
}
I tested with this docker-compose.yml:
version: "3.9"
services:
nginx:
image: nginx:latest
volumes:
- ./templates:/etc/nginx/templates
ports:
- 80:80
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
I am relatively new to Docker and have run into a problem that I can't seem to find a solution for. I have two Docker stacks that are running Nginx and Django however, one of the stacks is reachable via web browser, and the other times out with a bad gateway error. Both stacks are running on the same server but on different ports, the stack that runs on port 8000 is reachable the stack that runs on 8010 throws the 502 error. I have verified that the ports are open on the host and that I can ping from the Nginx container to the Django container. I have also spun up each stack separately which ended with the same results. I am running out of ideas so, any insight would be appreciated.
Server: VM - Red Hat Enterprise Linux Server release 7.9 (Maipo)
Docker version 20.10.3, build 48d30b5
Working Docker-compose file
version: '3.3'
services:
web:
build:
context: ./
dockerfile: Dockerfile
command: sh -c "python manage.py makemigrations &&
python manage.py migrate &&
gunicorn laitteiden_saatavuus.wsgi:application -w 3 --bind 0.0.0.0:8000"
volumes:
- static_volume:/home/app/web/static
expose:
- 8000
- 1433
env_file:
- ./laitteiden_saatavuus/.env
networks:
- database
- nginx_network
depends_on:
- db
db:
image: postgres:latest
volumes:
- postgres_data:/var/lib/postgresql/data/
- ./laitteiden_db.sql:/docker-entrypoint-initdb.d/laitteiden_db.sql
networks:
- database
nginx:
build: ./nginx
volumes:
- static_volume:/home/app/web/static
ports:
- 8000:80
networks:
- nginx_network
depends_on:
- web
networks:
database:
driver: bridge
nginx_network:
driver: bridge
volumes:
postgres_data:
static_volume:
Working Nginx conf
upstream laitteiden_saatavuus {
server web:8000;
}
server {
listen 80;
location / {
proxy_pass http://laitteiden_saatavuus;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /static/ {
alias /home/app/web/static/;
}
}
Non working docker-compose file
version: '3.3'
services:
web:
build:
context: ./
dockerfile: Dockerfile
command: sh -c "gunicorn eq_allocation.wsgi:application -w 3 --bind 0.0.0.0:8000"
volumes:
- static_volume:/home/equipment_drill/web/static
expose:
- 8000
- 1433
env_file:
- ./eq_allocation/.env
networks:
- eq_network
nginx:
build: ./nginx
volumes:
- static_volume:/home/equipment_drill/web/static
ports:
- 8010:80
networks:
- eq_network
depends_on:
- web
networks:
eq_network:
driver: bridge
volumes:
static_volume:
Non working Nginx conf
upstream eq_allocation {
server web:8000;
}
server {
listen 80;
location / {
proxy_pass http://eq_allocation;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /static/ {
alias /home/equipment_drill/web/static/;
}
}
I have been following this tutorial here Dockerize Django to dockerize my django application with Nginx as production web server. However inside my django code get_current_site(request).domain returns localhost as a result all generated url endpoints have the domain localhost instead of the production server domain name.
my nginx.conf: here
upstream fullstack_application {
server web:8000;
}
server {
listen 80;
location / {
proxy_pass http://fullstack_application;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
location /static/ {
alias /home/app/web/static/;
}
location /media/ {
alias /home/app/web/media/;
}
}
my production docker-compose.yml: here
version: "3.7"
services:
web:
build:
context: ./app
dockerfile: Dockerfile.prod
command: gunicorn fullstack_application.wsgi:application --bind 0.0.0.0:8000
volumes:
- static_volume:/home/app/web/static
- media_volume:/home/app/web/media
expose:
- 8000
env_file:
- ./.env.prod
depends_on:
- db
db:
image: postgres:12.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
env_file:
- ./.env.prod.db
nginx:
build: ./nginx
volumes:
- static_volume:/home/app/web/static
- media_volume:/home/app/web/media
ports:
- 1337:80
depends_on:
- web
volumes:
postgres_data:
static_volume:
media_volume:
Pls help :(
You could try setting hostname and/or domainname for service web in your docker-compose.yml file.
See documentation.
I'm trying to learn docker and docker-compose, and have run into a roadblock.
The stack is a python:2.7 base image serving up django pages, and this part works fine. I now want to put nginx in front of it as a reverse proxy. When I access localhost:8000 I get django pages as expected. When I load up localhost with no port, I get nothing ("Problem loading page"). I assume the connection between django container's port 8080 and nginx port 80 isn't happening, and I'm so new to docker that it's probably something simple that I'm not seeing.
docker-compose.yml
version: '2'
services:
web:
build: ./app/
command: python manage.py runserver 0.0.0.0:8000
volumes:
- ./app/:/code
ports:
- "8000:8000"
depends_on:
- db
nginx:
restart: always
build: ./nginx/
ports:
- "80:80"
volumes:
- /www/static
volumes_from:
- web
links:
- web:web
# probably not relevant to my issue
db:
image: postgres
redis:
restart: always
image: redis:latest
ports:
- "6380:6380"
volumes:
- ./redisdata/:/data
nginx config:
server {
listen 80;
server_name 127.0.0.1;
charset utf-8;
location /static {
alias /code/static;
}
location / {
proxy_pass http://web:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
The nginx service starts successfully and appears to be running. I checked it's log using
docker-compose logs nginx
And it was empty.