Jul 23, 2024
django-docker-compose-deployment)..gitignore using Python settings and a README.requirements.txt file in the project root with dependencies; start with:
django>=3.2.3,<3.3Dockerfile in the project root:
python:3.9-alpine3.13 for a lightweight setup.requirements.txt and app source files appropriately./app and expose port 8000.docker-compose.yml file:
.dockerignore file to exclude unnecessary files from the Docker build context.docker-compose build
docker-compose run --rm app sh -c "django-admin startproject app ."
2. Modify `settings.py` to handle secret keys using environment variables.
- Set `DEBUG` environment variable.
- Manage `ALLOWED_HOSTS` with external hostnames.
3. Add PostgreSQL configuration to use as your database.
4. Ensure migrations occur correctly.
### Setting Up NGINX Reverse Proxy
1. Create a directory called `proxy` for NGINX setup.
2. Create necessary NGINX configuration files: `uwsgi_params` and `default.conf.tpl`. This introduces static/media file handling for efficient deployment.
3. Establish `run.sh` script to set up NGINX and start the server.
## Finalizing Docker Configuration
- Modify existing Dockerfiles as necessary to copy in scripts/configurations.
- Create a separate deployment `docker-compose-deploy.yml` with changes for a production environment.
### Setting Up AWS EC2 Instance
1. Create an EC2 instance in the AWS management console with default configurations (t2.micro for free tier).
2. Set up security groups to allow HTTP and SSH traffic.
3. Install Git and Docker on the EC2 instance after connecting via SSH.
4. Clone your GitHub repository onto the server, including setting up deploy keys for private repos if needed.
### Deploying the Application
1. Upload code changes and run `docker-compose -f docker-compose-deploy.yml up -d` to launch services in a detached mode.
2. Attach ports properly for external access and static file handling.
3. Test the application in your browser via the EC2 public DNS.
## Conclusion
- This method allows for rapid deployment but requires best practices in managing sensitive information via environment variables.
- For ongoing updates, use git commands to pull changes, followed by building/updating containers as necessary.
## Notes
- If you encounter issues related to port conflicts, check existing applications using the port on your local machine or AWS instance.
- This process leverages Docker for consistent deployments across environments and facilitates easy scaling in production via better practices.