Running django app with docker, unrecognized runserver arguments - python

I'm having a problem with starting my django project in docker container. I have tried this in multiple ways, for example:
docker-compose run web python manage.py runserver
or just
docker-compose up
first time i have tried this i got no error, but i couldn't open the app in browser so i tried it again and then it stopped working completely.
I'm getting this error:
manage.py runserver: error: unrecognized arguments: python manage.py runserver
My docker-compose.yml
version: "3.9"
services:
db:
image: postgres:alpine
volumes:
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=gitlabdumptables
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
environment:
- POSTGRES_NAME=gitlabdumptables
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
depends_on:
- db
My Dockerfile
# syntax=docker/dockerfile:1
FROM python:3
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN pip3 install -r requirements.txt
CMD python manage.py makemigrations
CMD python manage.py migrate
COPY . /code/
ENTRYPOINT [ "python", "manage.py", "runserver", "0.0.0.0:8000" ]

The main process run in a Docker container is made up of two parts. You're providing an ENTRYPOINT in the Dockerfile, and then the equivalent of a CMD with the Compose command: or docker run ./manage.py ... arguments. When you have both an ENTRYPOINT and a CMD, the CMD gets passed as additional arguments to the ENTRYPOINT; so in this case, you're running python manage.py runserver 0.0.0.0:8000 python manage.py runserver 0.0.0.0:8000.
For this setup I'd suggest:
If you want to be able to override the command when you launch the container (which is useful), prefer CMD to ENTRYPOINT in your Dockerfile. Then these command overrides will replace the command.
If you have a useful default command (which you do), put it as the Dockerfile CMD. You do not need to specify a Compose command:.
You're also trying to run migrations in a way that won't work; again, a container only runs a single process, and the last CMD or the startup-time override wins. I'd use an entrypoint wrapper script to run migrations:
#!/bin/sh
# entrypoint.sh
# Run migrations
python manage.py migrate
# Then run the main container command (passed to us as arguments)
exec "$#"
In your Dockerfile make sure this is COPYed in (the existing COPY command you have will do it) and make this script be the ENTRYPOINT (with JSON-array syntax).
FROM python:3.10
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY ./ ./
ENTRYPOINT ["./entrypoint.sh"] # must be JSON-array form
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"] # not ENTRYPOINT
In your Compose file, you don't need to inject the code with volumes: or replace the command:, these are already part of your Dockerfile.
version: '3.8'
services:
db: { ... }
web:
build: .
ports:
- '8000:8000'
environment: { ... }
depends_on:
- db
# but no volumes: or command:

Related

Run an additional script after django server is started

I have a simple Django API that runs via docker-compose and I want to run a script bot.py after the server starts. How I can do this using docker?
My Dockerfile
FROM python:3.8
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY . /code/
And docker-compose.yml:
version: '3'
services:
web:
build: .
command: bash -c 'python3 network/manage.py makemigrations &&
python3 network/manage.py migrate &&
python3 network/manage.py runserver 0.0.0.0:8000'
volumes:
- .:/code
ports:
- "8000:8000"
I tried to add CMD [ "python", "./bot_new/bot.py" ] but it doesn't work actually
You can convert your script in a Django command following this tutorial and then in your main app execute the command after the app is ready like:
from django.core.management import call_command
from django import apps
class MyAppConfig(apps.AppConfig):
name = 'myapp'
def ready(self):
call_command('bot')

Permission denied after creating django app inside docker container

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.

'docker-compose run web' on Django in Docker container doesn't run website at localhost

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

DjangoProject doesn't appear in my Docker-Image (Docker toolbox for windows home)

I'm trying to build a django project using docker-compose, such as it is in the Docker Documentation (i use Docker toolbox for Windows 10 Home).
But when i execute the command:
sudo docker-compose run web django-admin startproject composeexample .
I get an bash: sudo: command not found
Then if i execute the same command without "sudo":
docker-compose run web django-admin startproject composeexample .
The image is created but the django project folder is not displayed:
Finally, when i run the command again i get:
Indicating that the project was created in the previous step.
The Dockerfile is:
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
docker-compose.yml
version: '3'
services:
db:
image: postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
and the requirements.txt:
Django>=2.0,<3.0
psycopg2>=2.7,<3.0

Issue with Dockerising Django app using docker-compose

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

Categories