Django web app Docker - unable to connect - django

I am new on Django and Docker and I have a problem to enter site localhost:8000.
I built django app and it is working on my local server but I'd like to dockerize my app. So I created two files:
Dockerfile :
RUN python:3.6.7-alpine
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD ./ /code/
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
and docker-compose.yml
version: '3'
services:
web:
build: .
command: python mysite/manage.py runserver 8000
ports:
- "8000:8000"
My next steps:
docker built --tag django_docker:latest .
and:
docker run django_docker
It's open server, but when I want to open localhost:8000 from my browser I can't because of "Unable to connect"
Where is my fault?
More about django app : it's project from book Python Crash Course : Learning_log. I'd like to build an image and push it to hub docker, but I am stuck. Thanks for help!

You are using a docker-compose.yml file, therefore you need to use the docker-compose command to run it:
docker-compose up
That's all you need, and you can read more about it in the official docs.
To run it without using docker compose, then your docker command needs to be:
docker run --publish 8000:8000 django_docker
If you want to restrict the site to be available only on your localhost, then bind to 127.0.0.1:
docker run --publish 127.0.0.1:8000:8000 django_docker

Try these
update dockerfile
# Pull base image
FROM python:3.7
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set work directory
WORKDIR /code
# Install dependencies
COPY Pipfile Pipfile.lock /code/
RUN pip install pipenv && pipenv install --system
# Copy project
COPY . /code/
update dockor-compose.yml
version: '3.7'
services:
web:
build: .
command: python /code/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- 8000:8000
after updating just run one commands in terminal
docker-compose up -d --build
to stop it use
docker-compose down

Related

My local API 127.0.0.1:8000 doesn't work with docker

I have simple django project with one html page and I try to deploy it with docker. My Dockerfile you can see below:
FROM python:3.10.9
ENV PYTHONUNBUFFERED 1
RUN mkdir /app
WORKDIR /app
COPY requirements.txt /app/
RUN pip install -r requirements.txt
COPY . /app/
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
I run my image with next command:
docker run -p 8000:8000 --rm 751aa7a2d66f
But when I open my browser the localhost with API 127.0.0.1:8000 doesn't work.
In the same time I run command docker ps and it shows the following:
docker ps result
What's the problem?
Thanks in advance for your help.
For your information I work on windows 7.
I tried to run docker run -p 127.0.0.1:8000:8000 --rm 751aa7a2d66f but it didn't help.
I also tried to change port of my local machine to 8001 with the same result.
You can run django with docker-compose.yml below:
version: '3'
services:
my_django_service:
build:
context: .
dockerfile: Dockerfile
command: 'python manage.py runserver 0.0.0.0:8000'
ports:
- 8000:8000

Why is CSS not working even I use 'runserver' command in Docker?

I am trying to develop a web application using Django in Docker.
I made a DockerFile and a docker-compose.yml file as below to check the code in the development environment.
Even though I'm using the runserver command in the file, when I start the container and access the localhost, the CSS for the admin page isn't working.
How should I change the codes to make it work?
Here are the codes:
Dockerfile
FROM python:3.8.3-alpine
WORKDIR /usr/src/app
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
COPY . .
docker-compose.yml
version: '3.7'
services:
web:
build: ./django_project
command: python manage.py runserver 0.0.0.0:8000
volumes:
- ./django_project/:/usr/src/app/
ports:
- 8000:8000
docker desktop: 4.1.1
Python: 3.8
Django: 3.0

docker django: Why to copy the context into docker image, if we are overwriting it by mounting something in its place in docker compose

I was following the tutorial for docker and django. https://devopstuto-docker.readthedocs.io/en/latest/compose/django/django.html
I see the following docker file
FROM python:2.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/ <---- WHATS THE NEED OF THIS, SINCE WE ARE MOUNTING IT IN DOCKER COMPOSE
Docker compose file as
version: '3'
services:
db:
image: postgres
web:
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/code <---------- WHY TO MOUNT THE CODE HERE. THE IMAGE HAS THE CODE
ports:
- "8000:8000"
depends_on:
- db
I see the in docker-compose.yml that its mounting the current folder into code - .:/code
and in the Dockerfile also we see ADD . /code/
Whats the need of ADD . /code/ if we are anyhow depend on the mounting in docker-compose which is going to overwrite the files
there's no need for ADD . /code if you are using this docker-compose, this is used in local development
Dockerfile with ADD . /code results in an image with the django app you can run anywhere, this is used in deployment

How can I install Django in my Docker file?

I am a newbie to Docker. I have created one Django project and can run it in Docker. However, I have started a second project and have encountered a problem.
I created a virtual env and entered it
pipenv install django~=3.1.0 && pipenv shell
I created a Django project
django-admin startproject config .
I ran it within the virtualenv
python manage.py runserver
and could see the Django spaceship
I then exited the virtualenv and created a Dockerfile
Dockerfile
# Pull base image
FROM python:3.8
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set work directory
WORKDIR /code
# Install dependencies
COPY Pipfile Pipfile.lock /code/
RUN pip install pipenv && pipenv install --system
# Copy project
COPY . /code/
I ran
docker build .
and it reported a successful build
I created a docker-compose.yml file
docker-compose.yml
version: '3.8'
services:
web:
build: .
command: python /code/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- 8000:8000
When I run
docker-compose up
it complains
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
I have read in the comments to this question that virtual envs should not be used in docker files, so I replaced
RUN pip install pipenv && pipenv install --system
with
RUN pip install django~=3.1.0
but I still get the same error.
What is wrong?
Have you tried installing your list of requirements from a separate file, something like this?
COPY requirements.txt /code/requirements.txt
WORKDIR /code
RUN pip install -r requirements.txt
Once you have it installed you can run docker-compose run web /bin/sh to start a shell and then use django-admin startproject to create a django project. You may need to change the path in the docker-compose file so that it reflects where your manage.py file ended up (I moved mine to the root). I was able to get it working with the following:
requirements.txt
django==3.1.0
docker-compose.yml
version: '3.8'
services:
web:
build:
context: .
dockerfile: Dockerfile
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- 8000:8000
Dockerfile
# Pull base image
FROM python:3.8
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set work directory
WORKDIR /code
# Install dependencies
COPY requirements.txt /code/requirements.txt
WORKDIR /code
RUN pip install -r requirements.txt
# Copy project
COPY . /code/
File tree looks like this:

'docker-compose up' hangs when attaching files for running Django server in virtualization

I'm trying to run Django inside a Docker container and connect to it using VS code remote, but it keeps hanging at the attach part.
Here is my Dockerfile
FROM registry.gitlab.com/datadrivendiscovery/images/primitives:ubuntu-bionic-python36-v2020.1.9
ENV PYTHONPATH=$PYTHONPATH:/app
WORKDIR /app/
EXPOSE 8000
COPY requirements.txt /app/requirements.txt
WORKDIR /app
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY . .
CMD ["sh", "-c", "python3 /app/manage.py runserver"]
My docker-compose.yml
version: '3'
services:
web:
build: .\
volumes:
- .:/app
ports:
- "8000:8000"
I don't have a database setup so I don't have that in the yml.