Overview and Summary of Steps
This tutorial guides you through setting up a Django project for a REST API using Docker and Docker Compose, linking it to GitHub for version control, and pushing it to Docker Hub securely — all without needing Python
Summary of Steps:
- Start a Project on GitHub: Create and clone a repository.
- Add Docker Hub Secrets in GitHub: Store Docker Hub credentials securely.
- Set Up the Project Locally: Add Docker files and create a Django app in a container.
- Run the Server: Launch the app with Docker Compose.
- Push to Docker Hub: Share your Docker image online.
Let’s get started!
Step 1: Start a Project on GitHub for Our Apps
1.1 Create a GitHub Repository
- Go to GitHub and log in.
- Click + New Repository.
- Name it (e.g., my-django-project).
- Check Add a README file.
- Select Python under Add .gitignore.
- Click Create Repository.
1.2 Clone It Locally
- Copy the SSH URL (e.g., git@github.com:your-username/my-django-project.git).
- Open your terminal.
- Navigate to your folder:
cd /path/to/your/folder
4. Clone it:
git clone git@github.com:your-username/my-django-project.git
5. Enter the folder:
cd my-django-project
Step 2: Set Up Docker Hub Authentication
To push Docker images, we need authentication. Instead of using passwords (which are insecure), we use Access Tokens.
2.1 What is a Credential?
A credential is proof of identity (like a username and password or an API token).
- Docker Hub requires authentication to push/pull images.
- Instead of a password, we use an Access Token.
- This is safer, as we can revoke the token if needed.
2.2 Generate a Docker Hub Access Token
- Go to Docker Hub.
- Log in to your account.
- Go to your Docker Hub account settings (top-right corner > “Account Settings”).
- Navigate to “Personal access tokens” and click “Generate new token.”
- Give it a name (e.g., my-django-project), set permissions (e.g., “Read & Write”), Click Generate and copy the token.
- Copy the token immediately (you will not see it again).
🔹 Why do we need an Access Token?
- Security: Safer than passwords.
- Automation: Allows scripts and CI/CD pipelines to authenticate.
- Revocability: You can delete a token without affecting your account.
Step 3: Store Docker Credentials in GitHub Secrets
Now, we will store our credentials in GitHub Secrets for secure access in CI/CD pipelines.
3.1 Add Docker Hub Credentials to GitHub
- Go to your GitHub repository.
- Click Settings → Secrets and variables → Actions.
- Click New repository secret.
- Add the first secret:
- Name: DOCKERHUB_USERNAME
- Value: (Your Docker Hub username)
- Click Add Secret.
5. Add the second secret:
- Name: DOCKERHUB_TOKEN
- Value: (Paste the Docker Hub Access Token you copied earlier)
- Click Add Secret.
Step 4: Make a Project on Our Machine by Adding Dockerfile, Docker Compose File, Requirements File, and Creating a Blank Django App Using Django CLI
4.1 Ensure Docker is Running
- Install Docker Desktop.
- Start Docker Desktop.
4.2 Create a requirements.txt File
- In my-django-project, create requirements.txt and save it.
django==4.2
- Why? Lists packages for Docker to install.
4.3 Create a Dockerfile
- Create Dockerfile in my-django-project.
FROM python:3.9
# Starts with Python 3.9 from Docker Hub.
# Options: python:3.10, python:3.9-slim, python:3.9-alpine
WORKDIR /app
# Sets "/app" as the working folder.
# Options: WORKDIR /myproject, omit it
COPY requirements.txt .
# Copies requirements.txt to /app.
# Options: COPY ./requirements.txt /app/req.txt
RUN pip install --no-cache-dir -r requirements.txt
# Installs Django, keeps image small.
# Options: RUN pip install -r requirements.txt
COPY . .
# Copies all project files to /app.
# Options: COPY ./app /app
EXPOSE 8000
# Opens port 8000.
# Options: EXPOSE 5000
- Why? Builds the container for your app.
4.4 Create a docker-compose.yml File with Detailed Explanations
- Create docker-compose.yml in my-django-project.
version: '3.8'
# Sets Compose version.
# Options: '3.9', '2'
services:
app:
# Service name.
# Options: web, django
build: .
# Builds from Dockerfile.
# Options: build: ./docker, image: my-django-app
ports:
- "8000:8000"
# Maps port 8000.
# Options: "5000:5000", "8080:8000"
volumes:
- .:/app
# Syncs local folder with /app.
# Options: ./code:/app, omit it
command: python manage.py runserver 0.0.0.0:8000
# Runs Django server.
# Options: command: bash
- Why? Manages your app’s container.
4.5 Create a .dockerignore File
- Create .dockerignore in my-django-project.
__pycache__
*.pyc
.git
.gitignore
- Why? Keeps container clean.
Step 5: Create a New Django Project Inside the Container
- Build the Docker Image
Run the following command to build the Docker image:
docker-compose build
2. Create a New Django Project
Use docker-compose run to create a new Django project inside the container:
docker-compose run - rm app sh -c "django-admin startproject app ."
- This creates a new Django project named app in the /app directory inside the container, which is synced with your local project folder.
3. Verify the Project Structure
After running the command, you should see the following files in your local project folder:
my-django-app/
├── app/
│ ├── __init__.py
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
├── requirements.txt
├── Dockerfile
├── docker-compose.yml
└── .dockerignore
Step 6: Run the Django Development Server
- Start the Container
Run the following command to start the container:
docker-compose up
2. Access Your Django App
Open your browser and go to http://localhost:8000. You should see the Django welcome page.