how to run exsisting django project in docker? - python

I'm trying to containerize my existing django project, which works just fine in my local machine.
dockerfile is as follows:
FROM django
ADD . /
WORKDIR /site
RUN pip install django-elasticsearch-dsl==0.5.1
RUN pip install tika==1.19
CMD python manage.py runserver 0.0.0.0:8000
i was able to create an image using :
docker build -t test1 .
and was able to create a container using the image by command :
docker run -d --name test1 -p 8000:8000 test1
as a result, i can see that the container test1 is up and running
Now, my understanding is if i do localhost:8000 in my browser, i should be able to see the view the required pages.
However, I don't see it.
I've have tried similar solutions available in stackoverflow, yet no success.

This image is officially deprecated in favor of the standard python
image, and will receive no further updates after 2016-12-31 (Dec 31,
2016). Please adjust your usage accordingly.
For most usages of this image, it was already not bringing in django
from this image, but actually from your project's requirements.txt, so
the only "value" being added here was the pre-installing of
mysql-client, postgresql-client, and sqlite3 for various uses of the
django framework.
For example, a Dockerfile similar to the following would be a good starting point for a Django project using PostgreSQL:
FROM python:3.4
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
postgresql-client \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
source
PS: your Dockerfile is complaining about manage.py is not found

Related

I'm trying to figure out how to run a docker container with a django project

I've been trying to make a docker container with a django project and have tried different solutions I've found but none of them work. My current Dockerfile
So far I've tried different youtube tutorials for setting up python and django docker containers but none of them have worked ending me up with different errors.
I see at least the following problems with your Dockerfile:
if you use /app as WORKDIR but copy files into the parent directory
you can run python instead of python3 since you use python 3 as your base image
So the file could look like this:
# syntax=docker/dockerfile:1
FROM python:3-alpine
ENV PYTHONUNBUFFERED=1
COPY requirements.txt /requirements.txt
RUN set -ex \
&& pip install --upgrade pip \
&& pip install --no-cache-dir -r /requirements.txt \
&& # your other commands ...
COPY . /app
WORKDIR /app
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
I also recommend creating a user in the container instead of using the root user. And of course runserver should not be used in production.

Unable to run Flask App using Docker in Windows-10

I've installed and configured docker (as per documentation) and I am trying to build a flask application using tiangolo/uwsgi-nginx-flask:python3.8. I've built a hello-world application, and have tested it locally by running python manage.py and the application runs successfully. Link to full Code-File.
My docker version and installation is as below:
Dockerfile:
FROM tiangolo/uwsgi-nginx-flask:python3.8
ENV INSTALL_PATH /usr/src/helloworld
RUN mkdir -p $INSTALL_PATH
# install net-tools
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y \
net-tools \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# set working directory
WORKDIR $INSTALL_PATH
# setup flask environment
# install all requirements
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
# copy all files and folder to docker
COPY . .
# run the application in docker environment
CMD [ "python", "./manage.py" ]
I built the application with docker build --tag hello-world:test . and running the application as: docker run -d -p 5000:5000 hello-world:test successfully.
However, I'm unable to open the application in localhost:5000 or 0.0.0.0:5000 or any other port. The application is running, as I can see it from the CLI:
But, from browser the page is not reachable:
The question mentions to check the IP address:
docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" hungry_engelbart
>> <no value>
Found another solution at this link, but docker-machine is currently deprecated.
I'm new to docker, but I have tried to run the same thing following this tutorial, but faced similar issues.
Finally, I am able to solve this. I had to configure a new inbound rules under Windows Firewall > Advanced Settings > Inbound Rules > New Inbound Rules. Create a new rule that will allow a range of local IP addresses, which in my case was 198.168.0.1:198.168.0.100. Finally, you need to run the application at 0.0.0.0 as pointed by #tentative in the comments. :)

Problem with multi-stage Dockerfile (Python - venv)

I'm trying to create a Python webapp docker image using multi-stage, to shrink the image size... right now it's around 300mb... it's also using virtual enviroment.
The docker image builds and runs fine up untill the point I need to add multi-stage so I know something is going wrong after that.... Could you help me out identifying what's wrong?
FROM python:3.8.3-alpine AS origin
RUN apk update && apk add git
RUN apk --no-cache add py3-pip build-base
RUN pip install -U pip
RUN pip install virtualenv
RUN virtualenv venv
RUN source venv/bin/activate
WORKDIR /opt/app
COPY . .
RUN pip install -r requirements.txt
## Works fine until this point ""
FROM alpine:latest
WORKDIR /opt/app
COPY --from=origin /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH" VIRTUAL_ENV="/opt/venv"
COPY . /opt/app/
CMD [ "file.py" ]
ENTRYPOINT ["python"]
Without the VENV it looks something like this (still throwing error "sh: python: not found"):
FROM python:3.8.3-alpine AS origin
WORKDIR /opt/app
RUN apk update && apk add git
RUN apk --no-cache add py3-pip build-base
RUN pip install -U pip
COPY . .
RUN pip install -r requirements.txt
FROM alpine:latest
WORKDIR /home
COPY --from=origin /opt/app .
CMD sh -c 'python file.py'
You still need pyhton in your runtime container, since you changed your last image to just alpine it wouldn't work. Just a tip, combine your CMD and ENTRYPOINT under one of them, there is generally no need for having two of them. Try to use only ENTRYPOINT since you can pass CMD easily in runtime for example to activate debug mode more easily.
EDIT: Please stay away from alpine for python apps as you can get some weird issues about it. You can use "python_version-slim-buster" images, they are small enough.

Docker run application in a volume

In my django docker app i would to use a volume for manage my application file here is my Dockerfile:
FROM python:3.6-alpine
EXPOSE 8000
RUN apk update
RUN apk add --no-cache make linux-headers libffi-dev jpeg-dev zlib-dev
RUN apk add postgresql-dev gcc python3-dev musl-dev
VOLUME /var/lib/cathstudio/data
WORKDIR /var/lib/cathstudio/data
COPY ./requirements.txt .
RUN pip install --upgrade pip
RUN pip install -t /var/lib/cathstudio/data -r requirements.txt
ENV PYTHONUNBUFFERED 1
ENV PYTHONPATH /var/lib/cathstudio/data
COPY . /var/lib/cathstudio/data
ENTRYPOINT python /var/lib/cathstudio/data/manage.py runserver 0.0.0.0:8000
but when i run my app:
docker run -d -it --rm --link postgres:postgres --name=cathstudio myrepo/app_studio:latest
i get
python: can't open file '/var/lib/cathstudio/data/manage.py': [Errno 2] No such file or directory
the same also if i in my Dockerfile write just ENTRYPOINT python manage.py runserver 0.0.0.0:8000
where is my file wrong? how can i run my app using a volume for storing app files?
So many thanks in advance
Can you try to use the -v flag rather than using VOLUME in dockerfile , im not sure why but VOLUME creates the exact empty volume rather than mounting along with all of the data :
remove VOLUME section from docker file and try the following
docker run -d -it --rm -v /var/lib/cathstudio/data/:/var/lib/cathstudio/data --link postgres:postgres --name=cathstudio myrepo/app_studio:latest
Volumes are not intended to hold code. Especially given your comment to #i4nk1t's answer ("I want to distribute my Docker image"), the code should be in the image itself, not in a volume.
You should just delete the VOLUME line.
Declaring VOLUME in a Dockerfile has some key side effects. One of them is that it prevents any RUN command on that directory from having any future effect, which is keeping your application from working.
I tend to recommend a workflow where you make sure your application works locally and then package it in Docker. If you can't use your host Python and really have to do live development in the container environment, you can use the docker run -v option (or equivalent) to mount host content on to any directory in any container, regardless of whether or not it was declared as a VOLUME.

Dockerize existing Django project

I can't wrap my head around how to dockerize existing Django app.
I've read this official manual by Docker explaining how to create Django project during the creation of Docker image, but what I need is to dockerize existing project using the same method.
The main purpose of this approach is that I have no need to build docker images locally all the time, instead what I want to achieve is to push my code to a remote repository which has docker-hub watcher attached to it and as soon as the code base is updated it's being built automatically on the server.
For now my Dockerfile looks like:
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install Django
RUN pip install djangorestframework
RUN pip install PyQRCode
ADD . /code/
Can anyone please explain how should I compose Dockerfile and do I need to use docker-compose.yml (if yes: how?) to achieve functionality I've described?
Solution for this question:
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
RUN pip install *name of package*
RUN pip install *name of another package*
ADD . /code/
EXPOSE 8000
CMD python3 manage.py runserver 0.0.0.0:8000
OR
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
EXPOSE 8000
CMD python3 manage.py runserver 0.0.0.0:8000
requirements.txt should be a plain list of packages, for example:
Django==1.11
djangorestframework
pyqrcode
pypng
This question is too broad. What happens with the Dockerfile you've created?
You don't need docker compose unless you have multiple containers that need to interact.
Some general observations from your current Dockerfile:
It would be better to collapse the pip install commands into a single statement. In docker, each statement creates a file system layer, and the layers in between the pip install commmands probably serve no useful purpose.
It's better to declare dependencies in setup.py or a requirements.txt file (pip install -r requirements.txt), with fixed version numbers (foopackage==0.0.1) to ensure a repeatable build.
I'd recommend packaging your Django app into a python package and installing it with pip (cd /code/; pip install .) rather than directly adding the code directory.
You're missing a statement (CMD or ENTRYPOINT) to execute the app. See https://docs.docker.com/engine/reference/builder/#cmd
Warning: -onbuild images have been deprecated.
#AlexForbes raised very good points. But if you want a super simple Dockerfile for Django, you can probably just do:
FROM python:3-onbuild
RUN python manage.py collectstatic
CMD ["python", "manage.py"]
You then run your container with:
docker run myimagename runserver
The little -onbuild modifier does most of what you need. It creates /usr/src/app, sets it as the working directory, copies all your source code inside, and runs pip install -r requirements.txt (which you forgot to run). Finally we collect statics (might not be required in your case if statics are hosted somewhere), and set the default command to manage.py so everything is easy to run.
You would need docker-compose if you had to run other containers like Celery, Redis or any other background task or server not supplied by your environment.
I actually wrote an article about this in https://rehalcon.blogspot.mx/2018/03/dockerize-your-django-app-for-local.html
My case is very similar, but it adds a MySQL db service and environment variables for code secrets, as well as the use of docker-compose (needed in macOS). I also use the python:2.7-slim docker parten image instead, to make the image much maller (under 150MB).

Categories

Resources