Containerizing and automating the deployment of an application
Used: Ubuntu 24.04, Docker, GitHub Actions, PostgreSQL used for a Django application.
Hi there! ?? Today, I'll walk you through the journey of containerizing a Django application using Docker. We'll set up PostgreSQL and then push everything to DockerHub. If you've ever wanted to level up your DevOps skills, this is for you! Let’s dive in!
Table of Contents
Pre-requisites ??
2. Installing Docker Locally
Install pip ??
sudo apt install pip
Installs the Python package installer pip.
Install PostgreSQL ???
sudo apt install postgresql-common postgresql
Installs PostgreSQL and its common files.
Install Docker ??
sudo apt remove docker docker-engine docker.io containerd runc
sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl gnupg
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io
sudo docker version
Removes old Docker installations (if any), adds Docker’s official GPG key, and installs Docker CE (Community Edition).
Make Docker the Primary Group ??
sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker
Adds a docker group and assigns the current user to it to run Docker commands without sudo.
Installing and Setting Up PostgreSQL Locally
Set Up Local PostgreSQL DB ???
sudo -u postgres psql
create user admin with password 'admin';
create database django_todo owner admin;
\q # Exits the PostgreSQL terminal
Creates a PostgreSQL user admin and a database django_todo.
Edit PostgreSQL Configuration ??
sudo vim /etc/postgresql/16/main/pg_hba.conf
# Change "peer" to "md5" or "password" to allow password authentication.
Then restart postresql
sudo systemctl restart postgresql
Local Demo Project Setup
Clone the App's Repo ???
git clone https://github.com/mmato72/django-todo-app.git
Create .env File ?? (To set up environment variables needed to connect the Django app to PostgreSQL.)
cd django-todo-app
vim .env
# Add the following environment variables:
DATABASE_NAME=django_todo
DATABASE_USER=<postgresql_username>
DATABASE_PASSWORD=<postgresql_password>
DATABASE_HOST=localhost
DATABASE_PORT=5432
Prepare the Environment ???
sudo apt install libpq-dev python3-dev build-essential
rm -rf venv
python3 -m venv venv
source venv/bin/activate
pip3 install -r requirements.txt
Installs necessary packages and sets up a virtual environment for the project.
Migrate the Database and Run the Server ??
python3 manage.py migrate
python3 manage.py runserver
Applies migrations and runs the Django development server. Test your app locally at this point.
Let’s containerize this app! ??
领英推荐
Create the Dockerfile ??
vim Dockerfile
Paste the following into the file:
FROM python:3.11
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
This Dockerfile sets up a Python environment for the Django app.
Build the Docker Image ???
docker build -t django-todo-app .
PostgreSQL Image Build
Create a Docker Network ??
docker network create my-django-postgres-network
Run PostgreSQL in a Docker Container ???
docker run --name my-postgres -p 5432:5432 -e POSTGRES_USER=<your_postgres_user> -e POSTGRES_PASSWORD=<your_postgres_user_password> --network my-django-postgres-network -d postgres
Make sure you use the postresql user name that you created
docker exec -it my-postgres bash
psql -U admin
create database django_todo owner admin;
Runs PostgreSQL in a container and sets up the database.
Running the Containerized Application
Update .env File ??
# Update the DATABASE_HOST in the .env file:
DATABASE_HOST=my-postgres
Points the Django app to the PostgreSQL container.
docker run --env-file .env --name django-todo-app-c1 -p 8000:8000 --network my-django-postgres-network -d django-todo-app
docker exec -it django-todo-app-c1 python manage.py migrate
Runs the Django app in a container and applies database migrations. Visit https://127.0.0.1:8000 to see your app live!
DockerHub Image Push
Clean Up the Containers ??
docker rm my-postgres --force
docker rm django-todo-app-c1 --force
Removes the running containers to prepare for DockerHub push.
Push to DockerHub ??
docker login
docker tag django-todo-app <dockerhub_username>/django-todo-app
docker push <dockerhub_username>/django-todo-app
Tags and pushes your image to DockerHub. ??
Automating the Build and DockerHub Push ??
Create GitHub Actions Workflow ??
mkdir -p .github/workflows # Run this command in your repo's root directory
vim .github/workflows/docker.yml
Add this GitHub Actions workflow to automate building and pushing to DockerHub:
name: Build and share Docker Image to Docker Hub
on:
push:
branches:
- main
jobs:
build-and-share:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPO_NAME }}:latest
Create GitHub Secrets ??
To learn how to create a docker token follow this link: https://docs.docker.com/security/for-developers/access-tokens/#create-an-access-token
To create github secrets:
1. Navigate to your repository in github: Go to the repository where you want to create the secret.
2. Access Settings: Click on the "Settings" tab.
3. Go to Secrets: In the left sidebar, find and click on "Secrets" or "Actions".
4. Create a new secret: Click on "Add secret".
5. Enter details:
Name: Give your secret a descriptive name.
Value: Paste the sensitive information you want to store.
6. Add secret: Click "Add secret"
Push changes to github
git add .
git commit -m "added Dockerfile and configured github actions"
git push origin main
You can watch the progress in the github actions tab.
You can also verify that the push to docker hub was successful by checking when the last push was made in your docker hub repo.
That's the Whole Show, Folks!
?? You've just built, containerized, and deployed a Django app using Docker! With automation in place, your project is ready for the world.