Disclaimer: I am a docker noob and I'm trying to learn. I am also running this on Windows 10
Here's my Dockerfile.yaml
FROM python:3.11
# setup env variables
ENV PYTHONBUFFERED=1
ENV DockerHOME=/app/django-app
# Expose port
EXPOSE 8000
# create work dir
RUN mkdir -p $DockerHOME
# set work dir
WORKDIR $DockerHOME
# copy code to work dir
COPY . $DockerHOME
# install dependencies
RUN pip install -r requirements.txt
# move working dir to where manage.py is
WORKDIR $DockerHOME/flag_games
# set default command (I thinkk)
ENTRYPOINT ["python"]
# run commands for app to run
CMD ["manage.py", "collectstatic", "--noinput"]
CMD ["manage.py", "runserver", "0.0.0.0:8000"]
Here are the commands I use (I run make docker-build
and make docker-run
)
docker-build:
docker build --tag docker-django .
docker-run:
docker run -d -p 8000:8000 --name flag-game docker-django
My container runs fine
(venv) PS C:\Users\Admin\Projects\flag-games> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
175c38b4ea9e docker-django "python manage.py ru…" 26 minutes ago Up 26 minutes 0.0.0.0:8000->8000/tcp flag-game
When I try to hit the website I get:
(venv) PS C:\Users\Admin\Projects\flag-games> curl.exe -v localhost:8000
* Trying 127.0.0.1:8000...
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET / HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/8.0.1
> Accept: */*
>
< HTTP/1.1 400 Bad Request
< Date: Sun, 23 Apr 2023 06:16:31 GMT
< Server: WSGIServer/0.2 CPython/3.11.3
< Content-Type: text/html
< X-Content-Type-Options: nosniff
< Referrer-Policy: same-origin
< Cross-Origin-Opener-Policy: same-origin
< Connection: close
<
<!doctype html>
<html lang="en">
<head>
<title>Bad Request (400)</title>
</head>
<body>
<h1>Bad Request (400)</h1><p></p>
</body>
</html>
* Closing connection 0
Even worse for 0.0.0.0:8000
(venv) PS C:\Users\Admin\Projects\flag-games> curl.exe -v 0.0.0.0:8000
* Trying 0.0.0.0:8000...
* connect to 0.0.0.0 port 8000 failed: Address not available
* Failed to connect to 0.0.0.0 port 8000 after 0 ms: Couldn't connect to server
* Closing connection 0
curl: (7) Failed to connect to 0.0.0.0 port 8000 after 0 ms: Couldn't connect to server
but http://127.0.0.1:8000/
works fine.
curl.exe -v http://127.0.0.1:8000/
* Trying 127.0.0.1:8000...
* Connected to 127.0.0.1 (127.0.0.1) port 8000 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:8000
> User-Agent: curl/8.0.1
> Accept: */*
>
< HTTP/1.1 200 OK
Am I misunderstanding how docker works? and how all the ports/network communicates with each other?
EDIT: Adding part ofsettings.py
ALLOWED_HOSTS = ['127.0.0.1', '0.0.0.0']
# Application definition
INSTALLED_APPS = [
'world_flags.apps.WorldFlagsConfig',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
"whitenoise.runserver_nostatic",
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
"whitenoise.middleware.WhiteNoiseMiddleware",
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
ROOT_URLCONF = 'flag_games.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'flag_games.wsgi.application'