Docker Compose Nginx Internal Server Error - python

I have a flask app that I want to host on nginx in my docker compose file but when I do it gives me an Internal Server error
Here are some important files:
docker-compose.yml
version: "3.8"
services:
th3pl4gu3:
container_name: portfolix
build: ./
networks:
- portfolix_net
ports:
- 8084:8084
restart: always
server:
image: nginx:1.17.10
container_name: nginx
depends_on:
- th3pl4gu3
volumes:
- ./reverse_proxy/nginx.conf:/etc/nginx/nginx.conf
ports:
- 80:80
networks:
- portfolix_net
networks:
portfolix_net:
name: portfolix_network
driver: bridge
nginx.conf:
server {
listen 80;
location / {
include uwsgi_params;
uwsgi_pass th3pl4gu3:8084;
}
}
Flask Dockerfile
# Using python 3.8 in Alpine
FROM python:3.8-alpine3.11
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Dependencies for uWSGI
RUN apk add python3-dev build-base linux-headers pcre-dev && pip install -r requirements.txt && apk update
# In case bash is needed
#RUN apk add --no-cache bash
# Tell the port number the container should expose
EXPOSE 8084
# Run the command
ENTRYPOINT ["uwsgi", "app.ini"]
app.ini
[uwsgi]
module = run:app
master = true
processes = 5
http-socket = 0.0.0.0:8084
chmod-socket = 660
vacuum = true
die-on-term = true
Now when I run this docker-compose without the nginx service, it works, but i want it to run on nginx server. Any idea why i am geting the Internal Server Error?

I was able to solve it with the following docker-compose:
version: "3.8"
services:
th3pl4gu3:
container_name: portfolix
build: ./
networks:
- portfolix_net
expose:
- 8084
restart: always
server:
image: nginx:1.17.10
container_name: nginx
depends_on:
- th3pl4gu3
volumes:
- ./reverse_proxy/nginx.conf:/etc/nginx/nginx.conf
ports:
- 8084:80
networks:
- portfolix_net
networks:
portfolix_net:
name: portfolix_network
driver: bridge
The issue was with my 8084:8084

Related

Django crontab can’t connect database(Postgresql) with docker; no such table err

I am using django and postgresql. I am using django-crontab to change the data.
It runs well in the local environment, but we use docker to deploy and watch, and I confirmed that when cron runs, we refer to sqlite3.
I also made a separate cron container in docker composite and ran it, I am using it incorrectly because I am a beginner. Help me
#goods/cron.py
from goods.models import Goods
def test():
print(Goods.objects.all())
./docker-compose.yml
version: '3.8'
volumes:
postgres: {}
django_media: {}
django_static: {}
static_volume: {}
services:
postgres:
container_name: postgres
image: postgres:14.5
volumes:
- postgres:/var/lib/postgresql/data/
environment:
- POSTGRES_USER
- POSTGRES_PASSWORD
- POSTGRES_DB
restart: always
nginx:
container_name: nginx
image: nginx:1.23.2
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
- django_media:/media/
- django_static:/static/
depends_on:
- asgiserver
- backend
restart: always
django_backend:/app/media
backend: host:container
container_name: django_backend
build: .
entrypoint: sh -c "python manage.py migrate && gunicorn handsup.wsgi --workers=5 -b 0.0.0.0:8000"
restart: always
volumes:
- ./:/app/
- /etc/localtime:/etc/localtime:ro
- django_media:/app/media/
- django_static:/app/static/
environment: #
- DEBUG
- POSTGRES_DB
- POSTGRES_USER
- POSTGRES_PASSWORD
- POSTGRES_HOST
- POSTGRES_PORT
depends_on:
- postgres
redis:
image: redis:5
asgiserver:
build: .
command: daphne -b 0.0.0.0 -p 8080 handsup.asgi:application
volumes:
- ./:/app/
restart: always
environment:
- DEBUG
- POSTGRES_DB
- POSTGRES_USER
- POSTGRES_PASSWORD
- POSTGRES_HOST
- POSTGRES_PORT
depends_on:
- redis
- postgres
cron:
build: .
restart: always
volumes:
- ./:/app/
depends_on:
- postgres
- backend
environment: #
- DEBUG
- POSTGRES_DB
- POSTGRES_USER
- POSTGRES_PASSWORD
- POSTGRES_HOST
- POSTGRES_PORT
command: cron -f # as a long-running foreground process
./Dockerfile
FROM python:3.10.8
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN mkdir /app/
WORKDIR /app/
RUN apt-get update -y
RUN apt-get install -y cron
COPY ./requirements.txt .
COPY ./ /app/
RUN pip install --no-cache-dir -r requirements.txt
# RUN service cron start
ENTRYPOINT ["./docker-entrypoint.sh"]
RUN pip install gunicorn psycopg2
./docker-entrypoint.sh
# If this is going to be a cron container, set up the crontab.
if [ "$1" = cron ]; then
./manage.py crontab add
fi
# Launch the main container command passed as arguments.
exec "$#"
I referred to the contents below.
How to make django-crontab execute commands in Docker container?

docker nginx django gunicorn not serving static files in production

Trying to deploy website with nginx + gunicorn + docker + django. But ngingx isn't serving static files. Following are the configurations:
Django project structure
settings file production.py
STATIC_URL = "/static/"
"""STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)"""
STATIC_ROOT = "/app/forex/static/admin/"
Docker file for nginx
FROM nginx:1.19.0
COPY ./default.conf /etc/nginx/conf.d/default.conf
nginx configurations
upstream django {
server website:8000;
}
server {
listen 80;
client_max_body_size 100M;
proxy_set_header X-Forwarded-Proto $scheme;
location / {
proxy_pass http://django;
}
location /media/ {
alias /app/media/;
}
location /static/ {
alias /app/forex/static/admin/;
}
}
Gunicorn docker file
FROM python:3
ADD requirements.txt /app/requirements.txt
ADD . /app
WORKDIR /app
EXPOSE 8000:8000
RUN pip install --upgrade pip && pip install -r /app/requirements.txt
RUN python manage.py collectstatic --no-input --settings=forex.settings.production
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "3", "forex.wsgi:application", "DJANGO_SETTINGS_MODULE=forex.settings.production"]
docker-compose.yml
services:
website:
build:
context: .
dockerfile: Dockerfile.app
env_file:
- env
container_name: website_container_8
nginx:
build: ./nginx
volumes:
- static:/app/forex/static/admin/
ports:
- "80:80"
depends_on:
- website
volumes:
static:
FROM nginx container, it isn't copying static files.
What do I need to change to make it working?
Your files are located at your website container, you need to share it, with the nginx container:
services:
website:
build:
context: .
dockerfile: Dockerfile.app
env_file:
- env
container_name: website_container_8
volumes:
- static:/app/forex/static/admin/ #<-- you want to share this
nginx:
build: ./nginx
volumes:
- static:/app/forex/static/admin/ #<-- with this folder
ports:
- "80:80"
depends_on:
- website
volumes:
static: #<-- you can do it through this

Error -2 connecting to redis://redis:6379:6379. Name or service not known

I was hoping to get some insight to what I am missing, currently trying to run a docker-compose config with python (walrus for db wrapper) and redis image, but I keep receiving the same error:
redis.exceptions.ConnectionError: Error -2 connecting to redis://redis:6379. Name or service not known.
I tried different solutions on stack overflow to fix this but still nothing is working.
Here are the related docker-compose config:
version: '3.3'
services:
redis:
image: redis:latest
container_name: redis
ports:
- "6379:6379"
command: ["redis-server"]
entrypoint: redis-server --appendonly yes
consumers:
build: ./consumers
container_name: consumers
environment:
- REDIS_HOST=redis://redis
command: "./run.sh"
depends_on:
- redis
restart: always
tty: true
networks:
default:
driver: bridge
Dockerfile:
FROM python:3.10
WORKDIR /consumers
# Copy Dependencies
COPY requirements.txt requirements.txt
COPY run.sh .
# Install Dependencies
RUN pip install -r requirements.txt
COPY . .
ENV REDIS_HOST=redis://redis
RUN chmod a+x run.sh
# Run executable consumer.py
CMD [ "./run.sh"]
And connection in python using walrus to redis:
rdb = Database(host=os.getenv("REDIS_HOST", "localhost"), port=6379)
Locally without docker the setup works fine. Any direction in this case would be really appreciated.
Thank you
The following configuration made it work, removed entrypoint, created a new custom network and exposed port. REDIS_HOST was modified to redis i.e. container name. All together made it work but while trying only one of these the problem persisted.
version: '3.5'
services:
redis:
image: redis:latest
container_name: redis
ports:
- "6379:6379"
expose:
- 6379:6379
command: ["redis-server"]
networks:
- connections
consumers-g1:
build: ./consumers
container_name: consumers-g1
environment:
- REDIS_HOST=redis
command: "./run.sh"
expose:
- 6379:6379
networks:
- connections
restart: always
tty: true
networks:
connections:
name: connections
driver: bridge

Publishing Waitress IP in docker container

I have a watiress API, I am trying to make it public in a docker contain, However i am receiving the following error:
return self.socket.bind(addr)
fleettracker_1 | OSError: [Errno 99] Cannot assign requested address
Here is my Docker-compose yml
version: '3'
services:
fleettracker:
build: ./fleettracker
ports:
- "5000:5017"
links:
- db
networks:
- fullstack
db:
image: mysql
ports:
- "3306:3306"
networks:
fullstack:
driver: bridge
Here is my dockerfile
FROM python:3
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD [ "python", "waitress API.py" ]
and here is my waitress API main:
if __name__ == "__main__":
waitress.serve(app=app, host="SOme IP", port=Someport)

serving flask via nginx and gunicorn in docker

Playing around with flask I would like to get a real setup up and running in docker. This means flask should be served via nginx and gunicorn. I set up a sample code repository https://github.com/geoHeil/pythonServing but so far can't get nginx to work properly.
Flask is served on application:5000, docker should resolve application to its respective name.
Nginx config is as follows:
server {
listen 8080;
server_name application;
charset utf-8;
location / {
proxy_pass http://application:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
which looks good to me. So far I cant find the problem.
edit
compose file is here. Command to start was
docker-compose build
docker-compose up
version: '2'
services:
application:
restart: always
build: ./application
command: gunicorn -w 4 --bind :5000 wsgi:application
links:
- db
expose:
- "5000"
ports:
- "5000:5000"
nginx:
restart: always
build: ./nginx
links:
- application
expose:
- 8080
ports:
- "8880:8080"
Your nginx config file is in a wrong location.
Steps to fix:
sudo docker-compose down
Delete nginx image:
sudo docker images
sudo docker rmi
REPOSITORY TAG IMAGE ID CREATED SIZE
pythonserving_nginx latest 152698f13c7a About a minute ago 54.3 MB
sudo docker rmi pythonserving_nginx
Now change the nginx Dockerfile:
FROM nginx:1.11.8-alpine
MAINTAINER geoheil
ADD sites-enabled.conf /etc/nginx/conf.d/sites-enabled.conf
Please note the location of nginx config.
Now try this docker-compose file (Using User-defined Networks):
version: '2'
services:
application:
restart: always
build: ./application
command: gunicorn -w 4 --bind :5000 wsgi:application
networks:
- testnetwork
expose:
- "5000"
ports:
- "5000:5000"
db:
restart: always
image: postgres:9.6.1-alpine
networks:
- testnetwork
ports:
- "5432:5432"
environment:
- POSTGRES_USER=d
- POSTGRES_PASSWORD=d
- POSTGRES_DB=d
volumes:
- ./postgres:/var/lib/postgresql
nginx:
restart: always
build: ./nginx
networks:
- testnetwork
expose:
- 8080
ports:
- "8880:8080"
networks:
testnetwork:
And Bring up containers:
sudo docker-compose up
Browse to http://localhost:8880
Smaple docker file
FROM python:3.5
RUN apt-get update
RUN apt-get install -y --no-install-recommends \
libatlas-base-dev gfortran nginx supervisor
RUN pip3 install uwsgi
COPY ./requirements.txt /project/requirements.txt
RUN pip3 install -r /project/requirements.txt
RUN useradd --no-create-home nginx
RUN rm /etc/nginx/sites-enabled/default
RUN rm -r /root/.cache
COPY nginx.conf /etc/nginx/
COPY flask-site-nginx.conf /etc/nginx/conf.d/
COPY uwsgi.ini /etc/uwsgi/
COPY supervisord.conf /etc/
COPY /app /project
WORKDIR /project
CMD ["/usr/bin/supervisord"]

Categories

Resources