I am practicing in deploying a multi-container application using Django Cookiecutter (https://github.com/pydanny/cookiecutter-django) with TravisCI, Docker, and also using Traefik as router. I already successfully deployed my application to an EBS (Elastic Beanstalk Service) application in AWS with a green health check. The only problem I am currently encountering is if I try to access the hostname or domain that I assigned, I am getting a 502 bad gateway
error. My suspicion is that there is a misconfiguration in either my traefik.yml
file or my port mappings in Dockerrunaws.json
file. Please help me find the error.
Here are the configurations:
Please note that "myhostname.com" refers to my actual dns hostname and "myusername" refers to my actual docker hub username.
Also, I used Route 53 to map my hostname with an A name record to my ELB instance.
traefik.yml:
log:
level: INFO
entryPoints:
web:
# http
address: ":80"
flower:
address: ":5555"
http:
routers:
web-router:
rule: "Host(`myhostname.com`)"
entryPoints:
- web
middlewares:
- csrf
service: django
flower-router:
rule: "Host(`myhostname.com`)"
entryPoints:
- flower
service: flower
middlewares:
csrf:
# https://docs.traefik.io/master/middlewares/headers/#hostsproxyheaders
# https://docs.djangoproject.com/en/dev/ref/csrf/#ajax
headers:
hostsProxyHeaders: ["X-CSRFToken"]
services:
django:
loadBalancer:
servers:
- url: http://django:5000
flower:
loadBalancer:
servers:
- url: http://flower:5555
providers:
# https://docs.traefik.io/master/providers/file/
file:
filename: /etc/traefik/traefik.yml
watch: true
docker-compose.yml:
version: "3"
volumes:
production_traefik: {}
services:
django: &django
build:
context: .
dockerfile: ./compose/production/django/Dockerfile
image: vincegnzls/test-django
command: /start
traefik:
build:
context: .
dockerfile: ./compose/production/traefik/Dockerfile
image: vincegnzls/test-traefik
depends_on:
- django
volumes:
- production_traefik:/etc/traefik/acme
ports:
- "0.0.0.0:80:80"
- "0.0.0.0:443:443"
- "0.0.0.0:5555:5555"
celerybeat:
<<: *django
image: vincegnzls/test-celerybeat
command: /start-celerybeat
flower:
<<: *django
image: vincegnzls/test-flower
command: /start-flower
Dockerrun.aws.json
{
"AWSEBDockerrunVersion": 2,
"containerDefinitions": [
{
"command": [
"/start"
],
"image": "myusername/test-1",
"name": "django",
"memory": 256
},
{
"essential": true,
"image": "myusername/test-2",
"name": "traefik",
"portMappings": [
{
"containerPort": 80,
"hostPort": 80
},
{
"containerPort": 443,
"hostPort": 443
},
{
"containerPort": 5555,
"hostPort": 5555
}
],
"memory": 128
},
{
"command": [
"/start-celerybeat"
],
"image": "myusername/test-3",
"name": "celerybeat",
"memory": 128
},
{
"command": [
"/start-flower"
],
"image": "myusername/test-4",
"name": "flower",
"memory": 128
}
]
}
Traefik Dockerfile
FROM traefik:v2.0
RUN mkdir -p /etc/traefik/acme
RUN touch /etc/traefik/acme/acme.json
RUN chmod 600 /etc/traefik/acme/acme.json
COPY ./compose/production/traefik/traefik.yml /etc/traefik
Django Relevant Settings:
ALLOWED_HOSTS=.myhostname.com
DJANGO_SECURE_SSL_REDIRECT=False
The rest of my Django environment settings are already in the environment properties of my ELB.