Docker for Web Development: Apache, PHP, MySQL, and PhpMyAdmin

Advertising

Last Updated : |  

Hey there, fellow developers! If you're like me, you've probably struggled with setting up a local development environment that mimics the live server. Enter Docker - a lifesaver that allows us to create, deploy, and run applications in containers. In this blog post, I'll walk you through creating a Docker environment for web development, using Apache, PHP, MySQL, and PhpMyAdmin. Let's dive in!



Why Use Docker?


Docker is not just a buzzword; it's a powerful platform that offers some serious advantages. It gives you portability, efficiency, and consistency, all wrapped in one. If you haven't jumped on the Docker train yet, it's high time you did!



Setting up Apache, PHP, MySQL with Docker Compose


Setting up Apache, PHP, and MySQL with Docker is a breeze. Here's how you can do it:



1. Dockerfile


Let's start by creating a Dockerfile to define the Apache and PHP environment. Here's an example you can use:


##########################################
#
# Dockerfile
#
# See: https://docker.com/
# See: https://docs.docker.com/engine/reference/builder/
#
# Author: prod3v3loper
# Copyright: prod3v3loper
# License: MIT
# Since: v1.0.0
# Link: https://github.com/prod3v3loper/docker
#
##########################################

FROM php:8.1-apache

# Install system dependencies
RUN apt-get update && apt-get install -y \
nano \
vim \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd


# Install PHP extensions
RUN docker-php-ext-install pdo pdo_mysql mysqli && docker-php-ext-enable mysqli

# Copy files to container htdocs
COPY /var/www /var/www/html/

# Set working directory
WORKDIR /var/www/html/

# NPM actions
# RUN npm install --production

# COMPOSER actions
# COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

RUN a2enmod rewrite

CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]


2. docker-compose.yml


Next, you'll need to define and run your services using a docker-compose.yml file. Here's an example:


##########################################
#
# Docker-Compose
#
# See: https://docs.docker.com/compose/
# See: https://hub.docker.com/
#
# Author: prod3v3loper
# Copyright: prod3v3loper
# License: MIT
# Since: v1.0.0
# Link: https://github.com/prod3v3loper/docker
#
##########################################

version: "3.8" # Version

services:

# --- PHP #
#
#
php:
container_name: "docker-php"
build:
context: .
dockerfile: Dockerfile
volumes:
- ./php/php.ini:/usr/local/etc/php/php.ini
networks:
- github-sample-app

# --- APACHE #
#
#
apache:
container_name: "docker-apache"
build:
context: .
dockerfile: Dockerfile
ports:
- 8002:80
volumes:
- ./var/www:/var/www/html/
- ./apache2/log:/var/log/apache2/
- ./apache/apache2.conf:/etc/apache2/apache2.conf
- ./apache/httpd.conf:/etc/apache2/sites-available/httpd.conf
- ./apache/vhost.conf:/etc/apache2/sites-available/000-default.conf
links:
- db
- php
networks:
- github-sample-app

# --- MYSQL #
#
#
db:
container_name: "docker-mysql"
image: mysql:8.0.32
ports:
- 6606:3306
build:
context: .
environment:
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_PASSWORD: ${DB_PASSWORD}
env_file:
- ./.env
volumes:
- ./dump:/docker-entrypoint-initdb.d
- ./mysql:/var/lib/mysql/
networks:
- github-sample-app


# --- PHPMYADMIN #
#
# See: https://docs.phpmyadmin.net/en/latest/setup.html#installing-using-docker
#
phpmyadmin:
container_name: "docker-phpmyadmin"
image: phpmyadmin:5.2.1
build:
context: .
ports:
- 8003:80
environment:
PMA_HOST: db
links:
- db
networks:
- github-sample-app

networks:
github-sample-app:
driver: bridge

volumes:
db:


3. Build and Run


Now, all you need to do is run docker-compose up, and you'll have your environment ready to go!



Working with PhpMyAdmin


PhpMyAdmin is a handy tool that lets you manage your databases through a web interface. It's a must-have in any developer's toolkit.



Managing Containers


Docker provides some nifty commands that make managing your containers a piece of cake. Here are a few examples:



  • Start Containers: docker-compose up
  • Stop Containers: docker-compose down
  • Access Container Shell: docker exec -it <container_name> /bin/bash


Conclusion


And there you have it! With Docker, setting up a development environment has never been easier. I hope you found this guide helpful. Feel free to check out the complete code on  GitHub - prod3v3loper docker.

Happy coding!

Advertising

Your Comment

* This fields are required, email don't publish
?

This field is optional
Fill this field link your name to your website.

Data entered in this contact form will be stored in our system to ensure the output of the comment. Your e-mail address will be saved to determine the number of comments and for registration in the future

I have read the Privacy policy and Terms. I Confirm the submission of the form and the submission of my data.
tnado © 2024 | All Rights Reserved
Made with by prod3v3loper