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
EXPOSE 80

# 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
EXPOSE 80

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

ENV PYTHONUNBUFFERED 1

WORKDIR /app
COPY requirements.txt ./

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

COPY . ./

EXPOSE 8000

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

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
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set the working directory in the container
WORKDIR /app

# 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
EXPOSE 8000

# Run the application
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

2. Docker Compose

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

version: '3'

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

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/app
    ports:
      - '8000:8000'
    depends_on:
      - db
    environment:
      - DATABASE_HOST=db

volumes:
  db_data:

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

Django
gunicorn
mysqlclient
cryptography

#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 settings.py to use the MySQL database: Also see an article: How to Connect a Django Project to MySQL Database

DATABASES = {
    '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 manage.py migrate   #do db migration
docker exec -it container_id python manage.py 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
WORKDIR /app

# 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
EXPOSE 5000

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

Containerizing a NodeJs project

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

# Set the working directory in the container
WORKDIR /app

# 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)
EXPOSE 3000

# 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'

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

  mysql:
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: wordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    volumes:
      - ./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: