0

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'
mike_gundy123
  • 469
  • 5
  • 18
  • Since you are getting HTTP responses, you are successfully connecting to the container and the Docker-level wiring is correct. Docker problems tend to look more like "connection refused" or "connection reset by peer" errors that don't get as far as returning an HTTP status code. – David Maze Apr 23 '23 at 10:00
  • Okay, so things are going as expected? Does that mean the docker container port is useless to connect to? – mike_gundy123 Apr 23 '23 at 16:02
  • The Docker-level setup seems to be okay (a couple of oddities in the Dockerfile but nothing that would obviously cause this), which would suggest that a [mcve] needs to include enough of your application code to demonstrate the issue. Can you [edit] the question to include this? – David Maze Apr 23 '23 at 16:38
  • I've added part of my `settings.py` which I assume is what is needed – mike_gundy123 Apr 23 '23 at 16:48

1 Answers1

-1

Mike, localhost, and 127.0.0.1 are not the same.

A very good explanation I found on Stackoverflow:

Docker app server ip address 127.0.0.1 difference of 0.0.0.0 ip

From inside of a Docker container, how do I connect to the localhost of the machine?

This will solve your query. Hope this helps.

Suchandra T
  • 569
  • 4
  • 8