I'm running a Django application from a Docker container using docker compose. My goal is to run the application from a custom hostname, which I'll call myapp.com. Here are the steps I've taken thus far:
- Added
127.0.0.1 myapp.comto my /etc/hosts file - Added a local CA using
mkcert -install - Created cert and key files using
mkcert -cert-file cert.pem -key-file key.pem myapp.com 127.0.0.1 - Added
'django_extensions'to the INSTALLED_APPS list in settings.py - Added
'myapp.com'to the ALLOWED_HOSTS list in settings.py - Set the startup command in docker-compose.yaml to
python manage.py runserver_plus --cert-file cert.pem --key-file key.pem myapp.com:8000
When I start up the project with docker compose up, the server boots up successfully and the console claims that it's running with no issues at https://myapp.com:8000. However, attempting to actually access my application by visiting that URL does not work, giving an error page that says this:
This site can't be reached
myapp.com unexpectedly closed the connection.
Attempting to curl that URL from my machine's command line also fails with this message, no matter what flags I use:
curl: (35) schannel: failed to receive handshake, SSL/TLS connection failed
However, running the curl command from within the docker container gives a different message (below), and running the command with the --insecure flag makes it work fine and return the main page of the app.
curl: (60) SSL certificate problem: unable to get local issuer certificate
Any ideas on how to debug this and/or solve this issue? The curl error messages mentioning SSL suggest that something might be wrong with the cert files, but I'm not sure what I would need to do to fix that.
The full contents of my settings.py and docker-compose.yaml files can be found below.
"""
Django settings for cas_testing_project project.
Generated by 'django-admin startproject' using Django 3.2.16.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'CENSORED'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = [
'localhost',
'myapp.com',
'.ngrok-free.app',
]
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'livereload',
'uniauth',
'django_extensions',
'cas_testing',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'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',
]
AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
'uniauth.backends.CASBackend',
]
ROOT_URLCONF = 'cas_testing_project.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'cas_testing_project.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
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',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_URL = '/static/'
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
LOGIN_URL = "/accounts/login/"
UNIAUTH_LOGIN_DISPLAY_STANDARD = False
UNIAUTH_LOGOUT_CAS_COMPLETELY = True
version: "3.9"
services:
mssql_db:
build:
context: .
dockerfile: mssql.Dockerfile
volumes:
- ./mssqldata:/var/opt/mssql/data
ports:
- "1433:1433"
environment:
- MSSQL_SA_PASSWORD=${SA_PASSWORD}
env_file:
- ./.env
db:
image: mysql
volumes:
- ./mysqldata/db:/var/lib/mysql
restart: unless-stopped
environment:
- MYSQL_DATABASE=${RDS_GSB_DB}
- MYSQL_USER=${RDS_GSB_USER}
- MYSQL_PASSWORD=${RDS_GSB_PASS}
- MYSQL_ROOT_PASSWORD=${RDS_GSB_PASS}
web:
build: .
command: python manage.py runserver_plus --cert-file cert.pem --key-file key.pem myapp.com:8000
volumes:
- .:/code
ports:
- "8000:8000"
env_file:
- ./.env
environment:
- FIRST_RUN=${FIRST_RUN}
depends_on:
- db
- mssql_db