When I run docker-compose up all working good. I've got problem with static file after reboot. All containers starts, but on static files request we got 404.
Yet another time, problems begins after server reboot. When I say:
docker-compose up
All working perfectly.
docker-compose.yml
web:
restart: always
build: .
command: /usr/local/bin/gunicorn ems3.wsgi:application -w 2 -b :8031
volumes:
- .:/code
ports:
- "8031:8031"
links:
- db
nginx:
restart: always
build: ./nginx/
ports:
- "8002:8002" # 443
- "8001:8001" # 80
volumes:
- /www/static
volumes_from:
- web
links:
- web:web
db:
restart: always
image: postgres
ports:
- "5555:5555"
environment:
- POSTGRES_PASSWORD=mysecr3333
- POSTGRES_USER=postgres
nginx_config
server {
listen 8002 ssl default;
location /static {
alias /code/static;
}
location / {
proxy_pass http://web:8031;
proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header Host $host:$server_port;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
My problems were decided by using docker-compose version 2 file format. Seems little differece, but it's working. docker-compose.yaml looks like this:
version: '2'
services:
web:
restart: always
build: .
command: /usr/local/bin/gunicorn proj.wsgi:application -w 2 -b :8031
volumes:
- web_code:/code
ports:
- "8031:8031"
links:
- db
volumes:
web_code:
driver: local
Added version 2, added named volume, and added volume with driver:local.
Few strings, but such painfull.
This is one more like me
https://stackoverflow.com/a/36726663/2837890
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
I am currently learning how to implement docker with Django and Postgres DB. Everytime I tried I run the following command:
docker compose up --build -d --remove-orphans
Although I can see all my images are started. Every time I tried to open my Django admin site, I cannot sign in using already registered superuser credentials. In Postgres DB PGAdmin all the data that are created previously are stored and saved correctly. But after closing my computer an I start Docker compose up previously saved data's are not recognized by the new start up even if the data still visible in my Postgres DB. How can I make the data to be recognized at start up?
Here is my docker-compose.yml configurations:
version: "3.9"
services:
api:
build:
context: .
dockerfile: ./docker/local/django/Dockerfile
# ports:
# - "8000:8000"
command: /start
volumes:
- .:/app
- ./staticfiles:/app/staticfiles
- ./mediafiles:/app/mediafiles
expose:
- "8000"
env_file:
- .env
depends_on:
- postgres-db
- redis
networks:
- estate-react
client:
build:
context: ./client
dockerfile: Dockerfile.dev
restart: on-failure
volumes:
- ./client:/app
- /app/node_modules
networks:
- estate-react
postgres-db:
image: postgres:12.0-alpine
ports:
- "5432:5432"
volumes:
- ./postgres_data:/var/lib/postgresql
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
networks:
- estate-react
redis:
image: redis:5-alpine
networks:
- estate-react
celery_worker:
build:
context: .
dockerfile: ./docker/local/django/Dockerfile
command: /start-celeryworker
volumes:
- .:/app
env_file:
- .env
depends_on:
- redis
- postgres-db
networks:
- estate-react
flower:
build:
context: .
dockerfile: ./docker/local/django/Dockerfile
command: /start-flower
volumes:
- .:/app
env_file:
- .env
ports:
- "5557:5555"
depends_on:
- redis
- postgres-db
networks:
- estate-react
nginx:
restart: always
depends_on:
- api
volumes:
- static_volume:/app/staticfiles
- media_volume:/app/mediafiles
build:
context: ./docker/local/nginx
dockerfile: Dockerfile
ports:
- "8080:80"
networks:
- estate-react
networks:
estate-react:
driver: bridge
volumes:
postgres_data:
static_volume:
media_volume:
Here is Docker nginx default.conf file setup:
upstream api {
server api:8000;
}
upstream client {
server client:3000;
}
server {
client_max_body_size 20M;
listen 80;
location /api/v1 {
proxy_pass http://api;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /admin {
proxy_pass http://api;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /staticfiles/ {
alias /app/staticfiles/;
}
location /mediafiles/ {
alias /app/mediafiles/;
}
location /sockjs-node {
proxy_pass http://client;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
location / {
proxy_pass http://client;
proxy_redirect off;
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-Host $server_name;
}
}
Your answer is highly appreciated!!!
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 have a django backend where I use docker-compose to deploy. This django application uses a nginx proxy in the front.
When I deploy it in a docker-machine and I go to the docker-machine ip I am redirected to the django site properly.
But when i deploy it in a ubuntu machine on docker, when i go to the ip of the nginx container I am given the default nginx page, I am not redirected to the django application. The nginx container doesn't log any error too. All services are running in docker without any error.
I am sharing the config file of nginx, docker-compose files below
nginx.conf
server {
listen 80;
server_name omaha;
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/ssl.crt;
ssl_certificate_key /etc/nginx/ssl/ssl.key;
charset utf-8;
client_max_body_size 200M;
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;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
nginx dockerfile
FROM nginx
COPY conf/nginx.conf /etc/nginx/conf.d/nginx.conf
COPY certs/ /etc/nginx/ssl
docker-compose.yml
version: '2'
services:
nginx:
restart: always
build:
context: ./nginx/
ports:
- "80:80"
- "443:443"
volumes_from:
- web
web:
restart: always
build:
context: ./web
depends_on:
- web_ffmpeg
- postgres
- redis
- rabbitmq
expose:
- "8000"
environment:
- DEBUG=True
command: /usr/local/bin/gunicorn wsgi:application -w 2 -b :8000
web_ffmpeg:
restart: always
build:
context: ./web
depends_on:
- postgres
- redis
- rabbitmq
expose:
- "8000"
command: /usr/local/bin/celery -A trigger worker -l info
postgres:
restart: always
image: postgres:latest
expose:
- "5432"
volumes:
- pgdata:/var/lib/postgresql/data/
redis:
restart: always
image: redis:latest
expose:
- "6379"
volumes:
- redisdata:/data
rabbitmq:
restart: always
image: rabbitmq:3-management
environment:
RABBITMQ_DEFAULT_USER: 'adminuser'
RABBITMQ_DEFAULT_PASS: 'xxxxxxxxx'
RABBITMQ_DEFAULT_VHOST: 'myvhost'
ports:
- "15672:15672"
expose:
- "5672"
volumes:
elk-data:
pgdata:
redisdata:
web-data:
web-ffmpeg-data:
software versions:
I even matched all the version in between the two workstation. The versions are
docker 1.12.5, 1.13.0
docker-compose 1.11.0, 1.11.1
I tried all combinations of the versions and still same problem exists.
What is different between using docker-compose in a docker-machine and directly on a ubuntu machine.
the problem was in gunicorn config.
I didnt bind it to 0.0.0.0:8000 instead I had just binded it to :8000. this was the problem. I still dont know why it worked in machine
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.