I try to debug a Django app inside Docker container, the app is launched under uWSGI. Unfortunately, PyCharm debugger can't connect to the container and stops by timeout.
My run configuration:
I've added up --build to run all containers in debug mode.
docker-compose.yml:
version: "2.4"
services:
rabbitmq:
image: rabbitmq:3.10.7-management-alpine
container_name: bo-rabbitmq
rsyslog:
build:
context: .
dockerfile: docker/rsyslog/Dockerfile
image: bo/rsyslog:latest
container_name: bo-rsyslog
platform: linux/amd64
env_file:
- .env
volumes:
- shared:/app/mnt
api:
build:
context: .
dockerfile: docker/api/Dockerfile
image: bo/api:latest
container_name: bo-api
platform: linux/amd64
ports:
- "8081:8081"
- "8082:8082"
env_file:
- .env
volumes:
- shared:/app/mnt
apigw:
build:
context: .
dockerfile: docker/apigw/Dockerfile
image: bo/apigw:latest
container_name: bo-apigw
platform: linux/amd64
ports:
- "8080:8080"
env_file:
- .env
volumes:
- shared:/app/mnt
depends_on:
- api
volumes:
shared:
Dockerfile (for api):
FROM nexus.custom.ru/base/python27:2.7.17 # CentOS 7 with Python 2.7
# Environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV PYTHONPATH /app/
ENV PATH /app/:$PATH
ENV PIP_DEFAULT_TIMEOUT=100 \
PIP_DISABLE_PIP_VERSION_CHECK=1 \
PIP_NO_CACHE_DIR=1
# Install required software
RUN yum -y install enchant
# Working directory
WORKDIR /app
# Install and configure Poetry
RUN pip install --no-cache-dir poetry \
&& poetry config virtualenvs.create false
# Install project dependencies
COPY pyproject.toml .
COPY poetry.lock .
RUN poetry install --no-root --no-interaction
# Copy project files
COPY . .
COPY docker/api/manage.py ./
COPY docker/api/settings.py ./apps/adm/
COPY docker/api/config.py ./apps/adm/
COPY docker/api/config/development.yml ./config/
COPY docker/api/config/uwsgi/uwsgi.yml ./config/uwsgi/
COPY docker/api/entrypoint.sh ./
# Allow execution
RUN chmod +x /app/entrypoint.sh
# Entrypoint
ENTRYPOINT /app/entrypoint.sh
entrypoint.sh:
#!/bin/sh
# Create required directories
mkdir -p /app/mnt/spooler
mkdir -p /app/mnt/logs
mkdir -p /app/mnt/run
mkdir -p /app/mnt/shared/static
mkdir -p /app/mnt/protected_media
mkdir -p /app/mnt/htdocs
# Copy static
cp -r -n /app/static /app/mnt/shared/static
# Run uWSGI
uwsgi --yml=/app/config/uwsgi/uwsgi.yml
uwsgi.yml:
uwsgi:
chdir: /app
master: true
procname-master: b::master
procname: b::worker
processes: 2
threads: 4
listen: 128
max-requests: 1024
buffer-size: 16384
reload-on-exception: false
master-fifo: /app/mnt/run/running.fifo
vacuum: false
lazy-apps: true
enable-threads: true
pythonpath: /app
http: :8081
env: DJANGO_SETTINGS_MODULE=apps.adm.settings
module: apps.adm.wsgi
stats: :8082
stats-http: true
memory-report: 1
disable-logging: 0
log-5xx: true
log-4xx: true
log-slow: 500
What am I doing wrongly? Is it possible to connect PyCharm to Django app with uWSGI inside docker?
Related
I am working on a CLI App in python for AWS SQS(which is run on localstack) on docker. Here's my docker-compose.yml:
version: "3.8"
networks:
localstack-net:
name: localstack-net
driver: bridge
services:
localstack:
image: localstack/localstack
privileged: true
networks:
- localstack-net
ports:
- "4576:4576"
environment:
- DEBUG=1
- EDGE_PORT=4576
- DATA_DIR=/tmp/localstack/data
- SERVICES=sqs:4567
volumes:
- ./.temp/localstack:/tmp/localstack
- ./localstack_setup:/docker-entrypoint-initaws.d/
cli_app:
build:
dockerfile: Dockerfile
container_name: my_app
and here's my dockerfile:
FROM python:3.8-slim
RUN useradd --create-home --shell /bin/bash app_user
WORKDIR /home/app_user
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
USER app_user
COPY . .
CMD ["bash"]
The problem that occurs is that the service cli_app exits when I run the command docker-compose up.
What can I do to rectify this problem?
I'm getting this issue with my entrypoint.prod.sh file that it doesn't exist even though I have echoed "ls" command and it shows me that the file is present in the right location with the right permissions but still docker isn't able to find it. I have tried many solutions but none are working. Any suggestion/help would be much appreciated. Let me know if you guys need any extra information from me.
so this is my main docker-compose.staging.yml file: -
version: '3'
services:
django:
build:
context: ./
dockerfile: docker-compose/django/Dockerfile.prod
expose:
- 8000
volumes:
- ./backend:/app
- static_volume:/app/django/staticfiles
environment:
CHOKIDAR_USEPOLLING: "true"
depends_on:
- postgresql
stdin_open: true
tty: true
env_file:
- ./.env.staging
postgresql:
image: postgres:13.1
environment:
- POSTGRES_USER=sparrowteams
- POSTGRES_PASSWORD=sparrowteams
- POSTGRES_DB=sparrowteams
ports:
- 5432:5432
volumes:
- .:/data
nginx-proxy:
container_name: nginx-proxy
build: nginx
restart: always
ports:
- 443:443
- 80:80
volumes:
- static_volume:/app/django/staticfiles
- certs:/etc/nginx/certs
- html:/usr/share/nginx/html
- vhost:/etc/nginx/vhost.d
- /var/run/docker.sock:/tmp/docker.sock:ro
depends_on:
- django
nginx-proxy-letsencrypt:
image: jrcs/letsencrypt-nginx-proxy-companion
env_file:
- .env.staging.proxy-companion
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- certs:/etc/nginx/certs
- html:/usr/share/nginx/html
- vhost:/etc/nginx/vhost.d
depends_on:
- nginx-proxy
volumes:
static_volume:
certs:
html:
vhost:
Then I have my Dockerfile.prod: -
###########
# BUILDER #
###########
# pull official base image
FROM python:3.9.1-buster as builder
# set work directory
WORKDIR /app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install psycopg2 dependencies
RUN apt-get update && apt-get -y install libpq-dev gcc && pip install psycopg2 && apt-get -y install nginx
# lint
RUN pip install --upgrade pip
COPY ./backend .
# install dependencies
COPY backend/requirements.txt .
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /app/wheels -r requirements.txt
#########
# FINAL #
#########
# pull official base image
FROM python:3.9.1-buster
# create directory for the app user
RUN mkdir -p /app
# create the appropriate directories
ENV HOME=/app
ENV APP_HOME=/app/django
RUN mkdir $APP_HOME
RUN mkdir $APP_HOME/staticfiles
WORKDIR $APP_HOME
# install dependencies
RUN apt-get update && apt-get install -y libpq-dev
COPY --from=builder /app/wheels /wheels
COPY --from=builder /app/requirements.txt .
RUN pip install --no-cache /wheels/*
# copy entrypoint-prod.sh
COPY docker-compose/django/entrypoint.prod.sh $APP_HOME/entrypoint.prod.sh
RUN chmod +x $APP_HOME/entrypoint.prod.sh
# copy project
COPY ./backend $APP_HOME
RUN echo $(ls -la)
RUN sed -i 's/\r$//' $APP_HOME/entrypoint.prod.sh && \
chmod +x $APP_HOME/entrypoint.prod.sh
ENTRYPOINT ["/bin/bash", "/app/django/entrypoint.prod.sh"]
and then finally I have my entrypoint.prod.sh file (Which is actually giving an error that it doesn't exist.)
#!/bin/bash
set -e
gunicorn SparrowTeams.wsgi:application --bind 0.0.0.0:8000
My nginx/vhost.d/default file: -
location /staticfiles/ {
alias /app/django/staticfiles/;
add_header Access-Control-Allow-Origin *;
}
nginx/custom.conf: -
client_max_body_size 10M;
nginx/dockerfile: -
FROM jwilder/nginx-proxy
COPY vhost.d/default /etc/nginx/vhost.d/default
COPY custom.conf /etc/nginx/conf.d/custom.conf
My project structure looks something like this: -
- SparrowTeams (Main folder)
- backend
- SparrowTeams (Django project folder)
- docker-compose
- django
- Dockerfile.prod
- entrypoint.prod.sh
- nginx
- vhost.d
- default
- custom.conf
- dockerfile
- .env.staging
- docker-compose.staging.yml (Docker compose file that I'm running)
Your issue is that you have a volume that you mount to /app in your docker-compose file. That overrides the /app directory in your container and that's why it can't find the script.
django:
build:
context: ./
dockerfile: docker-compose/django/Dockerfile.prod
expose:
- 8000
volumes:
- ./backend:/app <==== This volume
- static_volume:/app/django/staticfiles
You can either change the name of the directory you mount ./backend to (that's what I'd do), or you can place your app in another directory in your final image. The problem is caused by both of them being called /app.
So I am following this tutorial and have gotten all the way to the 'media' section and when I run the command:
docker-compose exec web python manage.py startapp upload
it all works fine but when I open the newly created views.py file and edit and try to save I get a permission denied error. I can open the file as root and edit it but now thru my Atom code editor. I don't know where I am going wrong can someone help me? Here's my code:
Dockerfile:
# pull official base image
FROM python:3.8.3-alpine
# set work directory
WORKDIR /usr/src/app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install psycopg2 dependencies
RUN apk update \
&& apk add postgresql-dev gcc python3-dev musl-dev
# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
# copy entrypoint.sh
COPY ./entrypoint.sh .
# copy project
COPY . .
# run entrypoint.sh
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]
docker-compose.yml:
services:
web:
build: ./app
command: python manage.py runserver 0.0.0.0:8000
volumes:
- ./app/:/usr/src/app/
ports:
- 8000:8000
env_file:
- ./.env.dev
depends_on:
- db
db:
image: postgres:12.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=hello_django
- POSTGRES_PASSWORD=hello_django
- POSTGRES_DB=hello_django_dev
volumes:
postgres_data:
entrypoint.sh:
if [ "$DATABASE" = "postgres" ]
then
echo "Waiting for postgres..."
while ! nc -z $SQL_HOST $SQL_PORT; do
sleep 0.1
done
echo "PostgreSQL started"
fi
# python manage.py flush --no-input
# python manage.py migrate
exec "$#"
try to issue chmod 777 -R in the folder where it is located.
Dockerfile
FROM python:3.7
FROM registry.gitlab.com/datadrivendiscovery/images/primitives:ubuntu-bionic-python36-v2020.1.9
ENV PYTHONUNBUFFERED 1
RUN mkdir /bbml
WORKDIR /bbml
RUN pip install -r requirements.txt
RUN pip install --upgrade pip
ADD . /bbml/
CMD [ "python", "./manage.py runserver 0.0.0.0:8800" ]
docker-compose.yml
version: '3'
services:
web:
build: .
command: "python3 manage.py runserver 0.0.0.0:8800"
container_name: bbml
volumes:
- .:/bbml
ports:
- "8800:8800"
So I managed to get this to run properly by doing 'docker-compsoe run web' and go the standard "Starting development server" message at the bottom, but when I go to localhost:8800 it says "site can't be reached". What's going on?
Dockerfile
FROM python:3
FROM registry.gitlab.com/datadrivendiscovery/images/primitives:ubuntu-bionic-python36-v2020.1.9
ENV PYTHONUNBUFFERED 1
RUN mkdir /bbml
WORKDIR /bbml
COPY requirements.txt /bbml/
RUN pip install -r requirements.txt
COPY . /bbml
docker-compose.yml
version: '3'
services:
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
container_name: bbml
volumes:
- .:/app
ports:
- "8000:8000"
Check your folders Permission and Run :
sudo docker-compose up
I found the issue. Docker will be running on a port you actually need to go to :8800 rather than localhost:8800
I am new to Docker and I want to dockerise the Django app to run as a container. Followed as below.
Here is the Dockerfile
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
Here is docker-compose.yml conf
version: '3'
networks:
mynetwork:
driver: bridge
services:
db:
image: postgres
ports:
- "5432:5432"
networks:
- mynetwork
environment:
POSTGRES_USER: xxxxx
POSTGRES_PASSWORD: xxxxx
web:
build: .
networks:
- mynetwork
links:
- db
environment:
SEQ_DB: cath_local
SEQ_USER: xxxxx
SEQ_PW: xxxxx
PORT: 5432
DATABASE_URL: postgres://xxxxx:xxxxx#db:5432/cath_local
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
well on my docker shell i point to Dockerfile directory, if i run an ls command from y path i see the manage.py file, but if i run:
docker-compose up
i get this error:
web_1 | python: can't open file 'manage.py': [Errno 2] No such file or directory
core_web_1 exited with code 2
Why my app don't find manage.py file that is in the same position as the "docker-compose up" command is?
PS: No /code folder is created when i run docker-compose command. Is it correct?
So many thanks in advance
try to edit your Dockerfile like this:
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
and remove command: python manage.py runserver 0.0.0.0:8000 from compose
I assumed that the manage.py is in /code/ folder, since you have WORKDIR /code in the dockerfile then the server will be created in the build stage and the files will be copied to it
as error states - manage.py is not in that directory.
And as far as I can see you are copying requirements.txt twice.
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
# move following line above 'pip install' and make sure that `manage.py` exists on the same directory as `requirements.txt`
COPY . /code/
# remove following line
# COPY requirements.txt /code/
RUN pip install -r requirements.txt
# you can define CMD here, but for dev env it is much more convenient to define it on docker-compose.yml, so you do not need to rebuild the image in case of some changes of the COMMAND