Hello Docker Community,
I have a Django-based project that I containerized using Docker and Docker Compose. The Docker image works perfectly on my local machine (Docker Desktop), but when I deploy the same image on an AWS EC2 instance, I encounter a “Page Not Found” error when trying to access it through the public IP.
Here are the steps I’ve followed so far:
- Project Setup:
- The project is a Django application, and I used Docker to containerize it.
- I used a
docker-compose.yml
file to set up the application along with MySQL.
- Working on Local Machine:
- On Docker Desktop, the project works as expected, and I can access it via
http://localhost:8000
.
- AWS EC2 Setup:
- I deployed the same Docker image on an AWS EC2 instance (Ubuntu server) using Docker and Docker Compose.
- The container starts successfully, but when I try to access it via the EC2 public IP (e.g.,
http://<public-ip>:8000
), I get a “Page Not Found” error.
- Things I Have Checked:
- Docker Port Mapping: I have used the
-p
flag in the Docker Compose file to map the necessary ports (e.g.,8000:8000
). - Django Settings: I updated the
ALLOWED_HOSTS
in Django’ssettings.py
to include the EC2 public IP (e.g.,ALLOWED_HOSTS = ['<public-ip>', 'localhost']
). - Security Groups: I confirmed that my EC2 security group allows inbound traffic on port 8000.
- Application Logs: The logs don’t show any errors, and the container is running fine, indicating that the Django application inside the container is starting without issues.
- Docker Compose and Dockerfile:
docker-compose.yml
:
version: '3.8'
services:
mysql:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: myproject
ports:
- "3306:3306"
networks:
- app-network
app:
build: .
environment:
DATABASE_HOST: mysql
DATABASE_USER: root
DATABASE_PASSWORD: root
DATABASE_NAME: myproject
ports:
- "8000:8000"
depends_on:
- mysql
networks:
- app-network
command: >
sh -c "
python manage.py migrate &&
python manage.py collectstatic --noinput &&
python manage.py runserver 0.0.0.0:8000
"
networks:
app-network:
driver: bridge
Dockerfile
:
FROM python:3.12-slim
WORKDIR /app
RUN apt-get clean && \
rm -rf /var/lib/apt/lists/* && \
apt-get update --fix-missing && \
apt-get upgrade -y && \
apt-get install -y \
pkg-config \
default-libmysqlclient-dev \
libssl-dev \
libc6-dev \
gcc \
python3-dev && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install -y \
default-libmysqlclient-dev \
gcc \
&& pip install numpy \
&& pip install mysqlclient pikepdf
RUN apt-get install -y ca-certificates
RUN pip install Pillow==10.4.0
RUN pip install cryptography h5py
COPY requirements.txt /app/
RUN pip install --upgrade pip
RUN pip install django-extensions
RUN pip install --no-cache-dir -r requirements.txt
COPY . /app/
- What I Need Help With:
- Why does the application work on my local Docker desktop environment but not on AWS EC2?
- What could be causing the “Page Not Found” error when accessing the app via the public IP on EC2?
- Are there any additional configuration settings I may have missed, such as networking, environment variables, or Docker Compose configuration?
I would greatly appreciate any insights or suggestions to resolve this issue.
Thank you in advance for your help!