Featured image of post Fundamental Docker Concepts and a PHP Example

Fundamental Docker Concepts and a PHP Example

A concise guide to Docker fundamentals, including containerization, images, and orchestration, with a practical PHP example.

Fundamental Docker Concepts and a PHP Example

Docker has revolutionized software development and deployment by providing a consistent and isolated environment for applications. This article provides a foundational understanding of Docker, focusing on key concepts and illustrating them with a practical PHP example.

Core Docker Concepts:

  • Containers: The heart of Docker. A container is an isolated runtime environment that packages an application and its dependencies. This ensures consistent execution across different systems (development, testing, production). Think of it as a lightweight virtual machine, but significantly more efficient.

  • Images: An image is a read-only template that serves as the blueprint for a container. It contains the application code, libraries, runtime environment, and system tools. Images are built from a Dockerfile, a text file containing instructions on how to create the image.

  • Dockerfiles: A Dockerfile is a recipe. It defines the steps to build a Docker image. It specifies the base image, copies application code, installs dependencies, sets environment variables, and defines the command to run when the container starts.

  • Docker Hub: A public registry for Docker images. It’s a central repository where you can find and share pre-built images or push your own. https://hub.docker.com/

  • Orchestration (Docker Compose & Kubernetes): Managing multiple containers becomes complex in production. Orchestration tools like Docker Compose (for simple deployments) and Kubernetes (for larger, more complex deployments) automate container management, scaling, and networking.

PHP Example:

Let’s create a simple PHP application and containerize it using Docker.

1. Project Structure:

1
2
3
my-php-app/
├── index.php
└── Dockerfile

2. index.php:

1
2
3
4
<?php
echo "Hello from Dockerized PHP!\n";
phpinfo();
?>

3. Dockerfile:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Use an official PHP image as the base
FROM php:8.1-apache

# Install additional PHP extensions if needed (e.g., MySQLi)
RUN docker-php-ext-install pdo_mysql

# Copy your PHP application code into the webserver's document root
COPY . /var/www/html

# Expose port 80 for HTTP
EXPOSE 80

# Set the default command to start Apache
CMD ["apache2-foreground"]

4. Build and Run:

Open your terminal, navigate to the my-php-app directory, and execute these commands:

1
2
docker build -t my-php-app .
docker run -p 8080:80 my-php-app

The docker build command creates the image named my-php-app. The docker run command starts a container from this image, mapping port 8080 on your host machine to port 80 inside the container. You can now access your application at http://localhost:8080.

This example demonstrates the fundamental process of creating a Docker image and running a container. More complex applications would necessitate more sophisticated Dockerfiles and potentially the use of orchestration tools. Remember to explore Docker Compose for managing multi-container applications and Kubernetes for scaling and managing complex production environments.