I what to dockerize my django app,
i create my Dockerfile :
FROM python:3.6-alpine
RUN apk add --no-cache linux-headers libffi-dev jpeg-dev zlib-dev
RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev
RUN mkdir /DEV
WORKDIR /DEV
COPY ./requirements.txt .
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
ENV PYTHONUNBUFFERED 1
COPY . .
at this point i create my docker-compose.yml:
version: '3'
networks:
mynetwork:
driver: bridge
services:
db:
image: postgres
restart: always
ports:
- "5432:5432"
networks:
- mynetwork
environment:
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypass
POSTGRES_DB: mydb
volumes:
- ./data:/var/lib/postgresql/data
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
networks:
- mynetwork
volumes:
- .:/DEV
ports:
- "8000:8000"
depends_on:
- db
then i create a .dockerignore file:
# Ignore
.DS_Store
.idea
.venv2
__pycache__
!manage.py
*.py[cod]
*$py.class
*.so
.Python
*.log
docker-compose.yml
Dockerfile
geckodriver.log
golog.py
golog.pyc
log.html
media
out
output.xml
report.html
startup.sh
templates
testlibs
.dockerignore
well, at this point i run:
docker-compose build --no-cache
at the end image was build correctly, but when i run:
docker-compose up
system return 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
Someone can help me about the issue?
so many thanks in advance
Try making your Dockerfile more explicit with the locations and then change your docker-compose as well:
FROM python:3.6-alpine
RUN apk add --no-cache linux-headers libffi-dev jpeg-dev zlib-dev
RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev
RUN mkdir /DEV
WORKDIR /DEV
COPY ./requirements.txt /DEV/
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
ENV PYTHONUNBUFFERED 1
COPY . /DEV/
web:
build: .
command: python /DEV/manage.py runserver 0.0.0.0:8000
networks:
- mynetwork
Related
During working with docker where I dockerised Django PostgreSQL, I've entered in such problems as when I change some model and migrate it, after entering to the page, it says there is no such relationship in the database. After some research, I found that problem can be due to creating every time new migration and deleting the old.
How can I fix this problem?
Below you can see my configurations
docker-compose-prod.yml
services:
app:
volumes:
- static_data:/app/staticfiles
- media_data:/app/mediafiles
env_file:
- django.env
- words_az.env
- words_en.env
build:
context: .
ports:
- "8000:8000"
entrypoint: /app/script/entrypoint.sh
command: sh -c "python manage.py collectstatic --no-input &&
gunicorn --workers=3 --bind 0.0.0.0:8000 django.wsgi:application"
depends_on:
- db
nginx:
build: ./nginx
volumes:
- static_data:/app/staticfiles
- media_data:/app/mediafiles
ports:
- "80:80"
- "443:443"
depends_on:
- app
- flower
db:
image: postgres:14.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
env_file:
- db.env
ports:
- "5432:5432"
redis:
image: redis:alpine
ports:
- "6379:6379"
worker:
build:
context: .
command: celery -A django worker -l info
env_file:
- django.env
depends_on:
- db
- redis
- app
flower:
build: ./
command: celery -A django flower --basic_auth=$user:$password --address=0.0.0.0 --port=5555 --url-prefix=flower
env_file:
- django.env
ports:
- "5555:5555"
depends_on:
- redis
- worker
volumes:
postgres_data:
static_data:
media_data:
Dockerfile
FROM python:3.9-alpine
ENV PATH = "/script:${PATH}"
COPY ./requirements.txt /requirements.txt
RUN apk add --update --no-cache --virtual .tmp gcc g++ libc-dev linux-headers \
&& apk add --virtual build-deps gcc python3-dev musl-dev \
&& apk add postgresql \
&& apk add postgresql-dev \
&& pip install psycopg2 \
&& apk add jpeg-dev zlib-dev libjpeg \
&& pip install Pillow \
&& apk del build-deps
RUN pip install --upgrade pip
RUN pip install -r /requirements.txt
RUN apk del .tmp
RUN mkdir /app
COPY /src /app
RUN mkdir /app/staticfiles
COPY /script /app/script
RUN chmod +x /app/script/*
WORKDIR /app
COPY django.env /app
RUN adduser -D user
RUN chown -R user:user /app
RUN chown -R user:user /var
RUN chmod -R 755 /var/
RUN chmod +x script/entrypoint.sh
USER user
CMD ["/script/entrypoint.sh"]
How can I connect my postgreSQL database container with my django application.
How can I create a database in postgreSQL while building the image but the case is I have separate container for postgreSQL and in this case How can I connect my postgreSQL.
Dockerfile
FROM ubuntu
ENV PATH="/scripts:${PATH}"
RUN apt update -y
RUN apt-get install debconf-utils
RUN apt install python3.8 -y
RUN apt install python3-pip -y
RUN echo 'tzdata tzdata/Areas select Asia' | debconf-set-selections
RUN echo 'tzdata tzdata/Zones/Asia select Kolkata' | debconf-set-selections
RUN DEBIAN_FRONTEND="noninteractive" apt install -y tzdata
RUN apt-get install -y gdal-bin
RUN apt-get install -y libgdal-dev
COPY ./requirements.txt /requirements.txt
RUN pip install -r requirements.txt
RUN mkdir /app
COPY ./app /app
WORKDIR /app
COPY ./scripts /scripts
RUN chmod +x /scripts/*
# RUN mkdir -p /vol/web/media
# RUN mkdir -p /vol/web/static
# RUN mkdir -p /vol/web/media
# RUN adduser --disabled-password user
# RUN chown -R user:user /vol
# RUN chmod -R 755 /vol/web
# USER user
CMD ["entrypoint.sh"]
docker-compose.yml
version: '3.8'
services:
app:
build:
context: .
environment:
- SECRET_KEY=changeme
- ALLOWED_HOSTS=127.0.0.1,localhost
depends_on:
- db
db:
image: postgres
restart: always
volumes:
- static_data:/static/db
ports:
- 5432:5432
container_name: ae73234b58e8
proxy:
build:
context: ./proxy
volumes:
- static_data:/vol/static
ports:
- 80:8080
depends_on:
- app
volumes:
static_data:
So, here I need to create a database while I build the Dockerfile image and How can I do that?
You can add environment variables in db.
Set the POSTGRES_DB environment variable with name of database you use in django. It will be created once postgres is built.
Dockerfile:
# syntax=docker/dockerfile:1
FROM python:alpine3.14 AS cython-compile
WORKDIR /tmp/cython
COPY /data/python .
RUN pip3 install --upgrade pip && \
pip3 install --no-cache-dir cython && \
apk add --no-cache --virtual .build-dependencies gcc musl-dev && \
python3 setup.py build
FROM alpine:latest
WORKDIR /data
COPY --from=cython-compile /tmp/cython .
docker-compose.yml:
version: "3.9"
services:
testtest:
container_name: ztz-test
build:
context: .
dockerfile: Dockerfile
ports:
- "7776:7776"
volumes:
- .:/data
When I run the command docker-compose build there is no error at all but the file compiled by cython is not copied. I have confirmed that the file is in /tmp/cython by commenting this line:
FROM alpine:latest
WORKDIR /data
COPY --from=cython-compile /tmp/cython .
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.