Random Bright, Funny, Info, Deep Thoughts, AI Chats, and More

[
[
[

]
]
]

Since you develop article and informational pages in HTML and PHP, learning Docker is a fantastic move. It allows you to package your code with its exact environment (PHP version, extensions, web server) so it runs perfectly on any machine.
Here is a step-by-step guide to containerizing a simple PHP “Hello World” app.
1. Create Your Project Files
First, create a new folder on your computer and add these two files inside it:
index.php
This is your “Hello World” application.
<?php
echo “<h1>Hello from Docker!</h1>”;
echo “<p>PHP version: ” . phpversion() . “</p>”;
?>

Dockerfile
The Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
# Use an official PHP image with Apache pre-installed
FROM php:8.2-apache

# Copy your index.php into the web server’s root directory
COPY index.php /var/www/html/

# Expose port 80 to the outside world
EXPOSE 80

2. Understand the Architecture
Before we run the commands, it helps to visualize what is happening. You are taking your code and a “base image” (PHP + Apache) and baking them into a single Image. You then run that image as a Container.
3. Build and Run
Open your terminal or command prompt in that folder and run these two commands:
Step A: Build the Image
This creates a reusable blueprint called my-php-app.
docker build -t my-php-app .

Step B: Run the Container
This starts a “living” instance of that image.
docker run -d -p 8080:80 –name hello-docker-container my-php-app

* -d: Runs the container in the background (detached).
* -p 8080:80: Maps your computer’s port 8080 to the container’s port 80.
* –name: Gives your container a friendly name.
4. See it in Action
Open your web browser and go to:
http://localhost:8080
You should see your PHP “Hello World” page! Even if you didn’t have PHP or Apache installed on your actual computer, it works because it’s running inside the Docker container.
Useful Housekeeping Commands
| Command | What it does |
|—|—|
| docker ps | Lists all running containers. |
| docker stop hello-docker-container | Stops the app. |
| docker rm hello-docker-container | Deletes the container (not the image). |
| docker images | Lists all images you have built. |
Would you like me to show you how to use Docker Compose next, so you can easily link this PHP container to a MySQL database?
Adding phpMyAdmin is a great choice. It gives you a familiar web interface to manage your MySQL databases without having to write SQL queries in the terminal.
Since we are using Docker Compose, adding it is as simple as adding a few lines to your existing file.
1. Update your docker-compose.yml
Open your file and add the phpmyadmin service at the bottom. Notice how it links to the db service.
services:
  web:
    build: .
    ports:
      – “8080:80”
    volumes:
      – .:/var/www/html
    depends_on:
      – db

  db:
    image: mysql:8.0
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
      MYSQL_DATABASE: my_site

  # Add this section:
  phpmyadmin:
    image: phpmyadmin
    restart: always
    ports:
      – “8081:80”
    environment:
      – PMA_ARBITRARY=1
      – PMA_HOST=db
    depends_on:
      – db

2. Apply the Changes
You don’t need to delete anything. Just run the “up” command again. Docker is smart enough to see that only the phpmyadmin service is new and will only start that one.
docker-compose up -d

3. Access your Tools
Now you have two different web tools running at the same time on your machine:
* Your PHP App: http://localhost:8080
* phpMyAdmin: http://localhost:8081
How to log in to phpMyAdmin:
* Server: db (This is the name of our service in the YAML file).
* Username: root
* Password: rootpassword
Why this is powerful for you
As someone who creates informational web pages, you can now:
* Test different PHP versions: Just change php:8.2-apache to php:7.4-apache in your Dockerfile and restart.
* Clean Slate: If your database gets messy, you can run docker-compose down -v to wipe everything and start fresh in seconds.
* Portability: You can send this folder to a client or colleague, and if they have Docker, they can run your entire site (database and all) with one command.
Would you like to see how to enable PDO or other PHP extensions in your Dockerfile so you can write more secure database code?
To write secure, modern PHP code for your informational pages, you’ll likely want to use PDO (PHP Data Objects).
By default, the official PHP Docker images are kept very slim, so they don’t include every extension. You have to “install” them inside your Dockerfile.
1. Update the Dockerfile
We will use a special helper command provided by the official PHP image called docker-php-ext-install.
Update your Dockerfile to look like this:
FROM php:8.2-apache

# Install the PDO MySQL extension
RUN docker-php-ext-install mysqli pdo pdo_mysql

# Enable Apache mod_rewrite (useful for clean URLs/SEO)
RUN a2enmod rewrite

COPY index.php /var/www/html/

EXPOSE 80

2. Update index.php to use PDO
PDO is the industry standard because it supports prepared statements, which prevent SQL injection. Here is how your new connection code looks:
<?php
$host = ‘db’;
$db   = ‘my_site’;
$user = ‘root’;
$pass = ‘rootpassword’;
$charset = ‘utf8mb4’;

$dsn = “mysql:host=$host;dbname=$db;charset=$charset”;
$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];

try {
     $pdo = new PDO($dsn, $user, $pass, $options);
     echo “<h1>Connected via PDO!</h1>”;
} catch (\PDOException $e) {
     throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
?>

3. Rebuild and Launch
Whenever you change the Dockerfile, you need to tell Docker to rebuild the image so it can install the new software.
Run this command:
docker-compose up -d –build

The –build flag ensures Docker re-reads your Dockerfile and installs the pdo_mysql extension.
Summary of your Web Stack
You now have a professional-grade development environment:
* PHP 8.2 with PDO enabled.
* Apache with mod_rewrite (great for .htaccess files).
* MySQL 8.0 for your data.
* phpMyAdmin for visual management.
* Volumes enabled so you can edit code and see changes instantly.
Since you mentioned you create informational web pages, would you like to know how to use Docker Volumes to make sure your database data (like articles) stays safe even if you delete the containers?
In Docker, containers are designed to be “ephemeral”—meaning if you delete the container, any data stored inside it (like the articles you’ve written in your MySQL database) disappears.
To prevent this, we use Named Volumes. This tells Docker to store the database files on your actual hard drive in a protected area that persists even if the containers are stopped or removed.
1. Persistent Storage with Volumes
We need to modify the db service in your docker-compose.yml. We will add a volumes section at the bottom of the file and link it to the MySQL data folder.
Updated docker-compose.yml
services:
  web:
    build: .
    ports:
      – “8080:80”
    volumes:
      – .:/var/www/html
    depends_on:
      – db

  db:
    image: mysql:8.0
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
      MYSQL_DATABASE: my_site
    # This links the container’s data to a persistent volume
    volumes:
      – db_data:/var/var/lib/mysql

  phpmyadmin:
    image: phpmyadmin
    restart: always
    ports:
      – “8081:80”
    environment:
      – PMA_HOST=db
    depends_on:
      – db

# This creates the persistent storage space on your computer
volumes:
  db_data:

2. Why this matters for your work
As a developer of informational pages, your content is your most valuable asset.
* Standard behavior: If you run docker-compose down and then up, your database is usually wiped clean.
* With Volumes: You can stop your project for months, update your computer, or even change your PHP code entirely. When you run docker-compose up again, all your articles, tables, and data will still be there exactly as you left them.
3. Deployment Tip (The “Production” mindset)
When you are ready to move your site from your computer to a real web server:
* You install Docker on the server.
* You copy your folder (containing your Dockerfile, docker-compose.yml, and index.php).
* You run docker-compose up -d.
Your site will run exactly the same way on the server as it did on your laptop. No more “it works on my machine” bugs!
Next Steps
You now have a complete, professional, and persistent development environment.
Would you like me to help you create a bash script or a Makefile to automate your common tasks (like backing up your database or clearing logs) with a single word?
As your project grows, typing docker-compose up -d –build or remembering the MySQL backup commands becomes tedious. To stay productive, you can use a Makefile.
A Makefile allows you to create “shortcuts” for complex terminal commands. Since you’re creating informational pages, this makes it easy to switch between projects or perform maintenance.
1. Create the Makefile
In your project root folder (next to your docker-compose.yml), create a file named exactly Makefile (no extension) and paste this:
# Start the project
up:
docker-compose up -d

# Stop the project
down:
docker-compose down

# Rebuild everything (useful after Dockerfile changes)
build:
docker-compose up -d –build

# View logs in real-time
logs:
docker-compose logs -f

# Backup the database to a file named backup.sql
backup:
docker-compose exec db mysqldump -u root -prootpassword my_site > backup.sql

# Restore the database from backup.sql
restore:
docker-compose exec -T db mysql -u root -prootpassword my_site < backup.sql

> Note: Makefiles must use tabs for indentation, not spaces.
>
2. How to use it
Now, instead of long Docker commands, you just type one word in your terminal:
* make up — Starts your site.
* make build — Rebuilds and starts (perfect for when you add PHP extensions).
* make backup — Instantly creates a SQL dump of your articles/data.
* make logs — Shows you PHP errors in real-time as you refresh your browser.
3. Organizing for Multiple Pages
Since you create various informational pages, you might wonder if you need a Docker setup for every single site. You have two professional options:
| Strategy | When to use it |
|—|—|
| One Project, One Docker | Best for complex sites with unique PHP versions or specific database needs. |
| One Docker, Many Sites | Best for simple HTML/PHP pages. You can map a sites/ folder to /var/www/html/ and visit localhost:8080/site-a/ or localhost:8080/site-b/. |
Final Workflow Summary
You have built a complete development pipeline:
* Dockerfile: Configures your PHP environment (extensions like PDO).
* Docker Compose: Links your Web, Database, and phpMyAdmin.
* Volumes: Keeps your articles and data safe.
* Makefile: Automates your daily tasks.
Would you like to know how to optimize your Dockerfile for speed (using multi-stage builds) so your informational pages load faster when you eventually deploy them to a live server?


Discover more from NathanLegakis.com

Subscribe to get the latest posts sent to your email.

Leave a comment