Dockerized Django On Ubuntu - python

I am using:
Ubuntu 16.04.
Docker version 1.12.6.
I want to containerize my existing Django app., knowing that everything goes well in this app. => no bugs, no errors...
My Dockerfile:
FROM django
ADD . /BackendServer
WORKDIR /BackendServer
RUN pip install -r requirements.txt
CMD [ "python", "BackendServer/manage.py runserver 0.0.0.0:8000" ]
requirements.txt
djangorestframework
gunicorn
Now everything goes well, except the last line when executing the manage.py python, it says: "python: can't open file 'BackendServer/manage.py runserver 0.0.0.0:8000': [Errno 2] No such file or directory".
So, I execute the following command: "sudo docker run backendserver ./BackendServer/manage.py runserver 0.0.0.0:8000"
I got no errors and still the server is not running!!
What shall I do so that I can access the django server !? Please help!!
Additional note:
thanks in advance!

You have already changed the directory into /BackendServer.
Use this instead:
CMD [ "python", "./manage.py runserver 0.0.0.0:8000" ]
Also note that docker run executes without a tty by default, which will suppress the output. Run with -it to use interactive terminal.

Related

Docker containers crashed: /bin/sh: 1: [uvicorn,: not found

I am new to Docker and trying to Dockerize my FastAPI application.
First I created a Dockerfile:
FROM python:3.9.9
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
Then ran the following command:
docker build -t fastapi .
The command ran successfully.
After that I created the following docker-compose.yml:
version: "3"
services:
api:
build: .
ports:
- 8000:8000
env_file:
./.env
Then ran the following command:
docker-compose up -d
Ran successfully:
Network fastapi_default Created 0.7s
- Container fastapi_api_1 Started
Then to check if its running properly I ran the following command:
docker ps -a
And it showed that Container exited few seconds after it was created.
Then I ran this command:
docker logs fastapi_api_1
And it says:
/bin/sh: 1: [uvicorn,: not found
Not sure what is the reason. Tried some solutions that I found online but nothing worked out. I do have uvicorn in my requirements.txt file.
Help will be appriciated. Please let me know if additional information is required.
Note: You don't need to do docker build -t fastapi . manually. Docker-compose will do it for you (because you set build: .) But! You must run up command with --build parameter (docker-compose up --build) to force rebuild image even if it exists.
And about your problem:
Here is a very good article (and one more) about RUN, ENTRYPOINT and CMD
Here is three forms for CMD:
CMD ["executable","param1","param2"] (exec form, preferred)
CMD ["param1","param2"] (sets additional default parameters for ENTRYPOINT in exec form)
CMD command param1 param2 (shell form)
According error, looks like Docker interpreting CMD as a shell form or additional parameters for default ENTRYPOINT
Actually still not sure why it happens, but changing CMD to
CMD uvicorn app.main:app --host 0.0.0.0 --port 8000
or
ENTRYPOINT ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
should solve your problem
Also it will be better to use full path to uvicorn executable (/usr/bin/uvicorn or where it installed by default?). It is just my opinion but, that is may be a reason why CMD is interpreted as parameters instead of command.
PS In addition here is note from docker docs:
Note
The exec form is parsed as a JSON array, which means that you must use double-quotes (“) around words not single-quotes (‘).
So exec form syntax must meet the conditions of JSON syntax.
So, basically there was something wrong with the docker. I had created mulitple images. I removed all of them and ran the same commands again and it worked. I don't know the exact reason but its working now.
What I think was happening is that instead of deleting the old images and creating new one. I was just doing
docker-compose down
and then
docker-compose up -t
I think that command was not taking the changes into consideration.
then i ran:
docker-compose up --build
and I think that created a new image and it worked.
Then I noticed that there were atleast 10 images created. I deleted all of them and ran the same commands:
docker build .
docker-compose up -t
and it worked fine again.
So basically instead of using creating new image it was using the old one which was not created correctly:
docker-compose up --build
In short you should use docker-compose up --build whenever you make changes in your dockerfile or docker-compose.yml instead of docker-compose up -t
It might be confusing but I am also very new to Docker.
Thanks for the help everyone!
I've had the same issue with a Dockerfile in my docker-compose environment containing
COPY ./requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
RUN pip install uvicorn==0.20.0
CMD ["uvicorn", "--host", "0.0.0.0", "--port", "6000", "app:app"]
So I don't need an extra command:line in my docker-compose.yml
It turned out that if you install uvicorn in your requirements.txt, as I like to do for testing purposes
then it gets installed locally, and
RUN pip install uvicorn==0.20.0 is skipped, which means,
there is no /usr/bin/uvicorn 'executable' available, just somewhere in site-packages and CMD will fail.
So, if you use uvicorn in your requirements.txt, and in Dockerfile as well, you can maybe
force the reinstallation
RUN pip install --ignore-installed uvicorn==0.20.0
in the Dockerfile,
or set the PATH to find it somewhere in the guts of python,
or - what I find is a better solution to keep the image size small -
is to remove uvicorn from requirements.txt...

I defined a command in compose.yml like command: "python manage.py runserver 0.0.0.0:8000" it works fine. But when I built t

I defined a command in compose.yml like command: "python manage.py runserver 0.0.0.0:8000" and a port mapping when I try docker-compose up it works fine.
But when I built the image and try to run docker run image it exit immediately.
Only things like docker run -d -p 8000:8000 twitch_test_app python manage.py runserver 0.0.0.0:8000 works even though I already defined the command and the port mapping in docker-compose.yml.
Is there any way to just run ```docker run image without repeating the command that is defined in compose.yml file?
Thanks alot.
I think you are confused between docker-compose and docker.
Think docker-compose as a helper utility that help you easily setup docker environment. When you do docker-compose up, it will create a new image and setup docker container for you with all the configuration that you have specified. Your application will run inside the docker container. Docker-compose just makes life little easier.
However, if you want to just use the docker command you need to specify the same port-mapping and other configurations in the dockerfile itself.
When you do docker run image it will create container for you. But you need to provide entrypoint or command to start your application otherwise it will just exit as docker does not know what to do next. In your case I suppose is a Django project and you need to specify "python manage.py runserver" to start your django server. In dockerfile you can specify it using CMD attribute:
CMD [ "python", "manage.py", "runserver" ]

docker stuck on django runserver

Dockerfile:
FROM python:3.6-slim
ENV root=/test
ENV django=$root/test
COPY ./code $root
WORKDIR $django
RUN pip install -r requirements.txt --no-cache-dir
CMD ["python3", "manage.py", "runserver", "--noreload"]
without --noreload it will stuck on
Watching for file changes with StatReloader
FYI, "docker run hello-world" is working fine.
FYI, running ubuntu on virtualbox on windows 10 home(as dev env)
UPDATE:
I have changed the base image to
FROM python:3.6
and it works, but the question is still there why it is not working with slim?
What is your DEBUG value in settings? Can you change to False.
It's not related to docker image slip or any other image per say. Django is looking for hot-reload, whenever there is change in code, used for development purpose. But inside Docker it's not required as, I believe, you aren't changing your code.
Also use a wsgi/u for deployments - Gunicorn, uvicorn etc.

Docker refusing connection

I'm trying to run my first ever docker. On console everything seems to be fine, but the chrome says connection is refused. It tried to turn off windows firewall - no effect. Running on win10home.
$ docker run -p 8000:8000 -v `pwd`:/data --rm -it mydjango mynewproject/manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
December 28, 2016 - 11:48:20
Django version 1.10.4, using settings 'mynewproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Dockerfile:
From python:3
RUN pip install django
EXPOSE 8000
RUN mkdir /data
WORKDIR /data
I had this issue when running the server with the command
python manage.py runserver 8080
Running the command
python manage.py runserver 0.0.0.0:8080
Fixed it for me. Note the 0.0.0.0
Could you please try adding "docker ip" in the ALLOWED HOSTS in the mynewproject/settings.py
The url you are checking with is "docker-ip":8000 right?
To get docker docker container IP,
You can use docker inspect
Some thing like this:
CID=$(docker run -d -p 4321 base nc -lk 4321);
docker inspect $CID
Run the following command in CMD for Windows OS by going to the directory of your project then execute these commands:
python manage.py migrate #run this command to sync your Database with Django project
python manage.py runserver # run the server on http://127.0.0.1:8000/
For the more tutorial watch this channel simple Tutorials of Django is available
https://www.youtube.com/playlist?list=PL6gx4Cwl9DGBlmzzFcLgDhKTTfNLfX1IK
I have also learned from here. If you find your answer a Thumbs up will be appreciated . Happy Coding

run django in docker container

I'm trying to make a simple example of Django app running in docker container.
using this image https://hub.docker.com/_/django/
just for simplicity. Don't tell me please that i shouldn't use it in production :) app is very simple and i'm ok with using very basic Django server.
So, the problem is i'm always getting this error when trying to run the container image
C:\Users\slipo\PycharmProjects\simple_blog>docker run -p 8000:8000 my-blog
python: can't open file './manage.py runserver 0.0.0.0:8000 --settings=mysite.settings.prod': [Errno 2] No such file or directory
however, ./manage.py and mysite.settings.prod both definitely existing in container.
container creation log showing the file exists:
Step 7 : RUN ls -a
---> Running in 932ed2ad3e4c
.
..
.idea
Dockerfile
blog
manage.py
mysite
requirements.txt
templates
---> e7f938c1cbf2
Removing intermediate container 932ed2ad3e4c
Step 8 : CMD python ./manage.py runserver 0.0.0.0:8000 --settings=mysite.settings.prod
---> Running in f99bcafbc269
---> aca534e9ccb6
Removing intermediate container f99bcafbc269
Successfully built aca534e9ccb6
Dockerfile:
FROM django
EXPOSE 8000
ADD . /simple_blog
WORKDIR /simple_blog
RUN pip install -r requirements.txt
RUN pip install django-tinymce
RUN ls -a
CMD [ "python", "./manage.py runserver 0.0.0.0:8000 --settings=mysite.settings.prod" ]
Thank you.
can't open file './manage.py runserver 0.0.0.0:8000 --settings=mysite.settings.prod'
This is telling you that it is treating that entire string as a single filename.
I assume something like this works:
CMD [ "python", "./manage.py", "runserver", "0.0.0.0:8000", "--settings=mysite.settings.prod" ]
Try to execute this code.
CMD [ "python", "../manage.py runserver 0.0.0.0:8080 --settings=my_site.settings.prd"

Categories