You are currently viewing A Practical Tutorial for Dockerizing Software Applications

A Practical Tutorial for Dockerizing Software Applications

In the ever-evolving landscape of software development, the adoption of containerization has become a game-changer, empowering developers to streamline deployment processes and enhance scalability. One of the leading technologies in this realm is Docker, a platform that allows you to package applications and their dependencies into portable containers. In this blog, we’ll journey to demystify the art of Dockerizing Software Applications. The principles remain the same whether you’re dealing with web servers, databases, or custom services.

common Docker commands

docker run image >> downloads the image from online if not present on the local repository
docker ps >> lists all running containers
docker ps -a >> lists all containers both running or stopped
docker stop container-name >> stop running container
docker rm container-name >> remove container permanently
docker images >> lists all images
docker rmi image-name >> removes image ; first stop image to remove it
docker pull image-name >> pulls the image to local repository
docker inspect container-name >> gives more infor:: among them env definitions

Containerizing a Static Website(HTML, CSS, and javascript)

On the root folder of your project, create a file named Dockerfile and add the code below, then run the command that follows after the code:

# Use a lightweight base image
FROM nginx:alpine

# Set the working directory to the web root
WORKDIR /usr/share/nginx/html

# Copy all files and folders from the code directory to the container
COPY . .

# Expose the default Nginx port

# Command to run when the container starts
CMD ["nginx", "-g", "daemon off;"]
docker build -t dockerusername/webapp-name .
#if you intend to push your image to dockerhub, you have to add your dockerusername, otherwise ignore it. 

This Dockerfile utilizes the official Nginx image from the Alpine Linux distribution, a lightweight base for serving static content. It sets the working directory to the default web root of Nginx, copies the HTML file (index.html) and the CSS file (styles.css) into the container, exposes the default Nginx port 80, and starts Nginx in the foreground. This simple configuration allows you to dockerize a basic HTML and CSS website for quick and efficient deployment.

Containerizing a Static PHP Website

Create a Dockerfile in the same folder as the website files and add the following code.

# Use the official PHP image as the base image
FROM php:7.4-apache

# Set the working directory in the container
WORKDIR /var/www/html

# Copy the PHP files to the container
COPY . .

# Expose port 80 to the outside world

This Dockerfile uses the official PHP image with Apache and sets the working directory to /var/www/html. It copies the contents of the current directory (your PHP files) to the container’s working directory. Now run the build command to create the docker image.

Containerizing a Django project

FROM python:3


COPY requirements.txt ./

RUN pip install --upgrade pip 
RUN pip install -r requirements.txt

COPY . ./


CMD ["python", "", "runserver", ""]

Containerize a Django application using MySQL Database

Dockerizing a Django application and connecting it to a MySQL database involves creating a Dockerfile, a Docker Compose file, and configuring Django settings to use MySQL. Here’s a step-by-step guide:

1. Dockerfile

Create a Dockerfile in the root of your Django project:

# Use an official Python runtime as a parent image
FROM python:3.9

# Set environment variables

# Set the working directory in the container

# Copy the dependencies file to the working directory
COPY requirements.txt /app/

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Copy the current directory contents into the container at /app
COPY . /app/

# Expose port 8000 to allow communication to/from server

# Run the application
CMD ["python", "", "runserver", ""]

2. Docker Compose

Create a docker-compose.yml file in the root of your project:

version: '3'

    image: mysql:8.0
    restart: always
      MYSQL_DATABASE: 'djangodb'
      MYSQL_USER: 'djangouser'
      MYSQL_PASSWORD: 'userpass'
      MYSQL_ROOT_PASSWORD: 'rootpass' 
      - '3306:3306'
      - db_data:/var/lib/mysql

    build: .
    command: python runserver
      - .:/app
      - '8000:8000'
      - db
      - DATABASE_HOST=db


Replace the environmental variables with your Django database settings for the database, user, and password.


#If you have them in your requirements.txt file, you may leave the versions as they are. What we have here pulls the latest versions.

3. Django Settings

Update your Django to use the MySQL database: Also see an article: How to Connect a Django Project to MySQL Database

    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'djangodb',
        'USER': 'djangouser',
        'PASSWORD': 'userpass',
        'HOST': 'db',
        'PORT': '3306',

5. Build and Run

Open a terminal in the directory where your docker-compose.yml is located and run:

docker-compose up -d
#You might need to restart the web container. 

Important commands to troubleshoot should errors occur.

docker restart container_id   #To restart a container
docker exec -it container_id python migrate   #do db migration
docker exec -it container_id python createsuperuser #create a superuser
docker logs container_id #inspect container logs incase something goes wrong

How to Containerize a Flask Project

# Use the official Python image as the base image
FROM python:3.8

# Set the working directory in the container

# Copy the requirements file into the container
COPY requirements.txt .

# Install the dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy the entire project into the container
COPY . .

# Expose port 5000 to the outside world

# Define the command to run your application
CMD ["python", ""]

Containerizing a NodeJs project

# Use the official Node.js image as the base image
FROM node:14

# Set the working directory in the container

# Copy package.json and package-lock.json to the container
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code to the container
COPY . .

# Expose port 3000 (or the port your app is using)

# Define the command to run your application
CMD ["node", "app.js"]

Containerizing a WordPress application

Dockerizing WordPress involves creating a Docker environment with WordPress and a database server. Docker Compose is commonly used for this purpose, allowing you to define and run multi-container Docker applications.

1. Create a docker-compose.yml File:

Create a file named docker-compose.yml in a new directory. This file defines the services (WordPress and MySQL) and their configurations:

version: '3'

    image: wordpress:latest
      - "8000:80"
      WORDPRESS_DB_HOST: mysql
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
      - ./wp-content:/var/www/html/wp-content

    image: mysql:latest
      MYSQL_ROOT_PASSWORD: wordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
      - ./mysql-data:/var/lib/mysql

2. Create a wp-content Directory:

Create a directory named wp-content in the same location as your docker-compose.yml file. This directory will be used to persist WordPress uploads and themes.

3. Build and Run the Docker Containers:

Open a terminal, navigate to the directory containing your docker-compose.yml file, and run:

docker-compose up -d #This command builds the images and starts the containers in detached mode.

5. Stop and Remove Containers:

To stop and remove the containers, run:

docker-compose down

This command stops and removes the containers, but it retains the data volumes for wp-content and the MySQL database

Make a donation to support us

Web Hosting and email hosting Packages

For web development services, SEO services, Digital marketing strategies, website set up services, web hosting and domain registration; contact Dynamic Technologies.

Related content: