docker

docker | How to dockerize a simple php application

Hello everybody, welcome to this new article.

This article will help to dockerise a simple “hello world” php application. I was learning docker recently and this was my learning to play with docker. So lets get started.

What is docker ? in simple words…

Docker is an open-source platform designed to automate the deployment, scaling, and management of applications using containerization. It enables developers to package applications and their dependencies into standardized units called containers, ensuring consistency across different computing environments.

Key Concepts

  1. Containers vs. Virtual Machines (VMs):
  • Containers: Lightweight, share the host system’s operating system (OS) kernel, and isolate applications at the process level. They start quickly and use fewer resources.
  • Virtual Machines: Emulate entire hardware systems, including separate OS instances, making them heavier and slower to start compared to containers.

2. Images and Containers:

  • Docker Image: A read-only template that contains the application and all its dependencies, such as libraries and configuration files. Images are built from a set of instructions written in a Dockerfile.
  • Docker Container: A runnable instance of a Docker image. Containers are isolated environments where applications run consistently regardless of where they’re deployed.

Lets do it with an example

1). My simple PHP application

as you can see, i have a very simple php application that has a “hello.php” file and i just prints a message on to the screen.

2). Now lets dockerise this project

So we need two docker related files to be added .

  1. Dockerfile :
# 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 current directory contents into the container at /var/www/html
COPY . /var/www/html

# Expose port 80 in the container (Apache default)
EXPOSE 80

2. docker-compose.yml

version: "3"
services:
php-app:
build: .
ports:
- "8080:80"
volumes:
- .:/var/www/html

Alright, So i created the above two files and this is how my application files looks like.

3). Getting Started with Docker

To start using Docker, follow these general steps:

  1. Install Docker: Download and install Docker Desktop (available for Windows, macOS, and Linux) from the official Docker website.

4). Docker commands

Now open a “terminal” at the source code folder and run the command.

docker build -t hello-world-app .

output

next command. lets run the docker

docker run -d -p 8080:80 hello-world-app

output

wow , its now deployed to the docker. Now lets try to run in on the browser.

Amazing , its working . I am so excited. Thank you docker.

So this was just a simple example. to get started. I hope you will try this and will explore more docker options. Thanks for reading my article. Have a good day.

Happy Dockering 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *