0

I am currently trying to get my Django application ready for deployment. I am trying to 'dockerize' my application by creating an image of my application using docker, dockerfile and uwsgi. I can do this with the following command:

sudo docker create --name container --network network --network-alias container -t -p 40001:8001 image_name 

docker build -t image_name .

The image gets built, I can start it and I can access the home page of the containerised application. However, when I try to navigate to other pages within the application, i get a Server Error (500) and in the console (Firefox), i get this error:

 The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol.

I have this in my base.html:

<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8">

And every webpage extends this page.

I believe this could be an issue with my static files configuration as I am using WhiteNoise. But I was having issues with my dockerfile and uwsgi.ini file.

settings.py

import os
import socket
from pathlib import Path
from whitenoise.storage import CompressedManifestStaticFilesStorage
import docker_config

BASE_DIR = Path(__file__).resolve().parent.parent
DEBUG = True
ALLOWED_HOSTS = []

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'LisgreyWebApp',
    'reservations',
    'takeaway',
    'food_menus',
    'crispy_forms',
    'bootstrap4',
    'corsheaders'
]

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',
    'corsheaders.middleware.CorsMiddleware',
]

ROOT_URLCONF = 'LisgreyWebApp_FYP.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [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 = 'LisgreyWebApp_FYP.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'lisgrey_database',
        'USER': 'postgres',
        'PASSWORD': 'password',
        'HOST': '127.0.0.1',
        'PORT': '25432'
    }
}

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

CRISPY_TEMPLATE_PACK = 'bootstrap4'
CRISPY_FAIL_SILENTLY = not DEBUG

BOOTSTRAP4 = {
    'include_jquery': True,
}

LOGIN_REDIRECT_URL = 'home'
LOGOUT_REDIRECT_URL = 'home'  # new

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = False
USE_TZ = True

STATIC_URL = "/static/"
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'dist')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

TIME_INPUT_FORMATS = [
    '%H:%M %p'
]

DATE_INPUT_FORMATS = [
    '%d-%m-%Y'
]

Dockerfile

FROM python:3.8

RUN apt-get -y update && apt-get -y upgrade && apt-get -y install libgdal-dev

# Make a working directoir in the image and set it as working dir.
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# make sure that pip & setuptools are installed and to date
RUN pip install --upgrade pip setuptools wheel

# Get the following libraries. We caan install them "globally" on the image as it will contain only our project
RUN apt-get -y install build-essential python-cffi libcairo2 libpango-1.0-0 libpangocairo-1.0-0 libgdk-pixbuf2.0-0 libffi-dev shared-mime-info

# You should have already exported your Python library reuirements to a "requiremnts.txt" file using pip.
# Now copy this to the image and install everything in it.
COPY requirements.txt /usr/src/app
RUN pip install -r requirements.txt

# Copy everything in your Django project to the image.
COPY . /usr/src/app

# Make sure that static files are up to date and available
RUN python manage.py collectstatic --no-input

# Expose port 8001 on the image. We'll map a localhost port to this later.
EXPOSE 8001

# Run "uwsgi". uWSGI is a Web Server Gateway Interface (WSGI) server implementation that is typically used to run Python
# web applications.
CMD ["uwsgi", "--ini", "uwsgi.ini"]

uwsgi.ini

[uwsgi]
chdir = %d
# %d is the dir this configuration file is in
#socket = %dapp.sock
http = :8001
module = LisgreyWebApp_FYP.wsgi:application
chmod-socket=664

master = true
processes = 4
vacuum = true
enable-threads = true

Container logs

[uWSGI] getting INI configuration from uwsgi.ini
*** Starting uWSGI 2.0.19.1 (64bit) on [Fri Mar  5 18:59:51 2021] ***
compiled with version: 8.3.0 on 05 March 2021 15:28:29
os: Linux-5.8.0-44-generic #50-Ubuntu SMP Tue Feb 9 06:29:41 UTC 2021
nodename: ebc63a00d1a7
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 8
current working directory: /usr/src/app
detected binary path: /usr/local/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
chdir() to /usr/src/app/
your memory page size is 4096 bytes
detected max file descriptor number: 1048576
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :8001 fd 4
uwsgi socket 0 bound to TCP address 127.0.0.1:33037 (port auto-assigned) fd 3
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
Python version: 3.8.8 (default, Feb 19 2021, 17:55:44)  [GCC 8.3.0]
Python main interpreter initialized at 0x559f4a88c290
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 364600 bytes (356 KB) for 4 cores
*** Operational MODE: preforking ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x559f4a88c290 pid: 1 (default app)
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
*** uWSGI is running in multiple interpreter mode ***
Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
Matthew Magee
  • 95
  • 1
  • 6

1 Answers1

0

Found out that I was trying to connect to a database of a different name. Checked the logs for my database container and application container to find this out.

Matthew Magee
  • 95
  • 1
  • 6