Django Docker app container don't open on browser after run [duplicate] - python

This question already has answers here:
Docker Toolbox - Localhost not working
(7 answers)
Closed 3 years ago.
I dockerize my django app, this is my Dockerfile:
FROM python:3.6-alpine
EXPOSE 8000
RUN apk add --no-cache make linux-headers libffi-dev jpeg-dev zlib-dev
RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev
RUN mkdir /Code
WORKDIR /Code
COPY ./requirements.txt .
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
ENV PYTHONUNBUFFERED 1
COPY . /Code/
ENTRYPOINT python /Code/core/manage.py runserver 0.0.0.0:8000
well, i build my image and I run it:
docker run -p 8000:8000 --link postgres:postgres cath11/test_app
all seems to be done:
but when I open my browser on http://0.0.0.0:8000/ or http://127.0.0.1:8000/ my app does not open
Why my django app seems running on my container, I expose port but I cannot see it in my hosted browser?
So many thanks in advance

Use docker-machine ip default to check for the IP, and use it instead of localhost.
Technically, the django app is served inside the container, not your local host.
So in your case, you should try http://<ip from the command>:8000/

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.

Can't open localhost in the browser on port given in docker-compose

I am trying to build and run django application with docker and docker-compose.
docker-compose build example_app and docker-compose run example_app run without errors, but when I go to http://127.0.0.1:8000/ page doesn't open, I'm just getting "page is unavailable" error in the browser.
Here is my Dockeffile, docker-compose.yml and project structure
Dockerfile
FROM python:3.9-buster
RUN mkdir app
WORKDIR /app
COPY ./requirements.txt /app/requirements.txt
COPY ./requirements_dev.txt /app/requirements_dev.txt
RUN pip install --upgrade pip
RUN pip install -r /app/requirements.txt
docker-compose.yml
version: '3'
services:
example_app:
image: example_app
build:
context: ../
dockerfile: ./docker/Dockerfile
command: bash -c "cd app_examples/drf_example && python manage.py runserver"
volumes:
- ..:/app
ports:
- 8000:8000
project structure:
──app
──app_examples/drf_example/
────manage.py
────api
────drf_example
──requirements.txt
──requirements_dev.txt
──docker/
────docker-compose.yml
────Dockerfile
By default, Django apps bind to 127.0.0.1 meaning that they'll only accept connections from the local machine. In a container context, the local machine is the container, so your app won't accept connections from outside the container.
To get it to accept connections from anywhere, you add the bind address to the runserver command. In your case, you'd change the command in your docker-compose.yml file to
command: bash -c "cd app_examples/drf_example && python manage.py runserver 0.0.0.0:8000"
you need to expose port 8000 in your Docker file
FROM python:3.9-buster
EXPOSE 8000
RUN mkdir app
WORKDIR /app
COPY ./requirements.txt /app/requirements.txt
COPY ./requirements_dev.txt /app/requirements_dev.txt
RUN pip install --upgrade pip
RUN pip install -r /app/requirements.txt

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.

Docker create volumes in Dockerfile and compile app on it

In my docker django project i need for read/write purpose to create a volumes in my Dockerile and install/run app on it.
i found this article : DockerFile on StackOverflow but sincerly i don't understand more about it.
Here 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
RUN mkdir /Code
VOLUME /var/lib/cathstudio/data
WORKDIR /Code
COPY ./requirements.txt .
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
ENV PYTHONUNBUFFERED 1
COPY . /Code/
ENTRYPOINT python /Code/core/manage.py runserver 0.0.0.0:8000
at my original file i add the VOLUME /var/lib/cathstudio/data instruction, but after that how can i say to the rest of my code to use that volumes for WORKDIR, install requirements.txt, copy code and run app?
i don't what to specify it in RUN statement with -v directive after build, i would integrate the volume creation and manage directly in dockerfile.
So many thanks in advance
for anything expect pip you may specify workdir once:
WORKDIR /var/lib/cathstudio/data
for pip use -t or --target:
pip install -t /var/lib/cathstudio/data
-t, --target
Install packages into <dir>. By default this will not replace existing files/folders in <dir>. Use --upgrade to replace existing
packages in with new versions

how to run exsisting django project in docker?

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

Categories

Resources