Cannot run Django management command using Docker exec - django

I've got a Django server running nicely in a Docker container called loc-vol-web.
When I try to run Django management commands straight from the host CLI, it just doesn't work:
>> docker exec -it loc-vol-web "python /app/src/manage.py migrate"
OCI runtime exec failed: exec failed: container_linux.go:346: starting container process caused "exec: \"python /app/src/manage.py migrate\": stat python /app/src/manage.py migrate: no such file or directory": unknown
However, all of the following work fine:
>> docker exec -it loc-vol-web "python"
Python 3.7.6 (default, Jan 3 2020, 23:35:31)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>> docker exec -it loc-vol-web "/bin/bash"
some_user#ce1b1c2ac208:/app$ python /app/src/manage.py
Type 'manage.py help <subcommand>' for help on a specific subcommand.
Available subcommands:
[auth]
changepassword
createsuperuser
[contenttypes]
remove_stale_contenttypes
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
[sessions]
clearsessions
[staticfiles]
collectstatic
findstatic
runserver
some_user#ce1b1c2ac208:/app$
I am not sure why I can't just run manage.py.

The problem is the way you run docker:
docker exec -it loc-vol-web "python /app/src/manage.py migrate"
It is taking everything in double quotes as the command.
It should be run without double quotes:
docker exec -it loc-vol-web python /app/src/manage.py migrate

Related

run telegram bot inside a docker container with django app

I have a Django application and using python manage.py name_bot_job, I start the bot, but when I add this task to the docker-compose.yml file and make build, my bot just refuses to start, how can I do it with best practices?
entrypoint: ["/bin/sh","-c"]
command:
- |
python manage.py collectstatic --no-input --clear
python manage.py migrate --noinput
python manage.py createsuperuserwithpassword --username $${DJANGO_SUPERUSER_USERNAME} --password $${DJANGO_SUPERUSER_PASSWORD} --email $${DJANGO_SUPERUSER_EMAIL} --preserve
gunicorn job_filter_app.wsgi:application --bind 0.0.0.0:8000
python manage.py poolbot

Docker container not running on Linux

In our project we use docker to keep our development environments similar. When running it on macOS it works, however on Linux (currently using Manjaro) it states that the container doesn't exist:
docker-compose -f docker-compose.yml exec web python manage.py migrate --noinput
Error response from daemon: Container 43f405eac00706b22dc075cbcaf33dbbf595971be3ce1955034302cb1284aa5b is not running
make: *** [Makefile:9: build] Error 1
This is the makefile for our project:
build:
make down
docker-compose -f docker-compose.yml up -d --build
docker-compose -f docker-compose.yml exec web python manage.py migrate --noinput
docker-compose -f docker-compose.yml exec web python manage.py createsuperuser --noinput --email name#testmail.org
docker-compose -f docker-compose.yml exec web python manage.py startup
We tried different systems and different commands, but it always fails when trying to execute the second command.

How to run Django on Docker container

I'm setting up a django server application on docker. docker runs the container well but the command to run django is not taking by docker.
I've already gone through few youtube videos but none of them worked for me
Dockerfile
FROM python:3.6-stretch
MAINTAINER ***
ENV PYTHONBUFFERED 1
COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt
RUN mkdir /specfolder
WORKDIR /specfolder
COPY ./myfolder /specfolder
EXPOSE 8000
CMD ["python", "manage.py runserver"]
i've tried placing the command under docker-compose.yml file under
commands: sh -c "python manager.py runserver"
but none of them worked
docker-compose.yml file
version: "3"
services:
myapp:
build:
context: .
ports:
- "8000:8000"
volumes:
- ./myfolder:/specfolder
requriements.txt
django==2.2.4
pandas
xlrd
xlsxwriter
as of now under kinematics i am getting the python shell
Type "help", "copyright", "credits" or "license" for more information.
>>> 2019-08-31T13:34:51.844192800Z Python 3.6.9 (default, Aug 14 2019,
13:02:21)
[GCC 6.3.0 20170516] on linux
unable to access the 127.0.0.1:8000/myapp/login in the browser.
You need to make changes in your runserver command.
Change it to
python manage.py runserver 0.0.0.0:8000
Your Dockerfile will be something like this
# other command
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Make changes accordingly if you are using Dockerfile or docker-compose.
I see 2 problems in your implementation.
In Dockerfile you have, CMD ["python", "manage.py runserver"]. It should be CMD ["python", "manage.py", "runserver"]
In compose file commands: sh -c "python manager.py runserver" should be commands: python manager.py runserver.
a. If you use compose file then CMD should be removed from Dockerfile

Error while running the django server in eclipse

Type 'manage.py help <subcommand>' for help on a specific subcommand.
Available subcommands:
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
runserver
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
Note that only Django core commands are listed as settings are not properly configured (error: The SECRET_KEY setting must not be empty.).
You need to include the “runserver” flag in what ever setting determines to run manage.py to actually start the server.
E.g
python manage.py runserver
Or to run on a specific port.
python manage.py runserver 1234

django-cookiecutter, docker-compose run: can't type in terminal

I'm having an issue with a project started with django cookiecutter.
To verify that the issue was not with my project, I'm testing on a blank django cookiecutter project.
The issue is that when I run:
docker-compose -f production.yml run --rm django python manage.py createsuperuser
I get the prompt but can't type in the terminal.
Same thing when I run:
docker-compose -f production.yml run --rm django python manage.py shell
I get the shell prompt, but I can't type.
The app is running on a machine on DigitalOcean created with the docker-machine create command.
Any thoughts on what the issue could be and how I could debug this?
to enable typing in docker-compose terminal, you need to specify the terminal session is interactive on the docker-compose.yml. Because by default, docker console is not interactive.
stdin_open: true
tty: true
bash can be accessed inside the docker container using the following command.
docker-compose -f production.yml exec django bash
This will give you full access to the container of your django server.
There you can run all your normal django commands with full interactivity.
Create Superuser
python manage.py createsuperuser
For Local ENV
docker-compose -f local.yml exec django bash