This project provides a complete development environment with Apache, MariaDB, and MailHog, set up using Docker. It serves as a lightweight alternative to traditional development environments like XAMPP, offering advantages in terms of speed, simplicity, and space-saving.
- Requirements
- Installation
- Usage
- Directory Structure
- Configuration Explanation
- Advantages over XAMPP
- Using Volumes
- Symfony
- Why Use Batch Files?
- Docker must be installed on your system.
- Basic knowledge of the command line is helpful.
- Clone this repository:
git clone https://github.com/1manfactory/Docker-Fullstack-App new-project
- Navigate to the project directory:
cd new-project
- Run the
build-container.bat
script to build the Docker container:.\build-container.bat
-
Start the container with:
.\start-container.bat
-
Access the local server in your browser:
- Apache: http://localhost:8080
- MailHog: http://localhost:8025
-
Run bash
docker ps docker exec -it <container_id> /bin/bash
dockerfile
: Defines the Docker image with all necessary installations and configurations.init.sh
: A script for initializing and configuring the container.supervisord.conf
: Configures Supervisor to manage multiple services within the container.build-container.bat
: Batch script for building the Docker image.start-container.bat
: Batch script for starting the container.
The Dockerfile creates an image based on Debian and installs Apache2, MariaDB, MailHog, and Supervisor. It copies the necessary configuration files and sets the appropriate permissions.
The init.sh
script configures the Apache web server and MariaDB during the container startup. It ensures that the DirectoryIndex
settings are adjusted so that index.php
is used as the default start page.
The supervisord.conf
file configures Supervisor to manage multiple services (Apache, MariaDB, MailHog) within the container, ensuring they all start correctly.
- Space Saving: Using Docker containers reduces disk space usage compared to installing a full XAMPP stack.
- Speed: Faster startup and running speeds due to the lightweight nature of containers.
- Ease of Use: Simplified setup and deployment process, with everything configured via the Dockerfile and initialization scripts.
- Consistent Environment: Provides a consistent development environment, avoiding the "it works on my machine" problem.
- Dynamic Mounting: Changes made in the local
./src
directory are immediately reflected in the container's/var/www/html
directory.
Volumes in Docker are used to persist data, ensuring it isn't lost when a container is restarted or removed. In this project, there are two main benefits of using volumes:
-
Data Persistence: By using a volume for MariaDB (
my_data_volume
), all databases and data are preserved even if the container is stopped or restarted. This prevents data loss. -
Code Synchronization: A volume is used to synchronize the local
src
folder with the/var/www/html
directory inside the container. This means that changes made to the source files on the host system are immediately reflected in the container, speeding up development and testing.
The setup of these volumes is handled through the start-container.bat
script, which runs the following commands:
docker volume create my_data_volume
docker run -d -p 8080:80 -p 3306:3306 -p 8025:8025 -p 1025:1025 -v "%CURRENT_DIR%/src:/var/www/html" -v my_data_volume:/var/lib/mysql my-fullstack-app
This script creates a Docker volume named my_data_volume
and starts the container with the necessary volume mounts. This setup ensures that:
- Your source code in the
src
directory is linked to the container's web root. - The MariaDB data is stored in
my_data_volume
, ensuring data persistence between container restarts.
- Run bash
docker ps docker exec -it <container_id> /bin/bash
- Install Symfony with Profiler
cd /var/www/html composer create-project symfony/skeleton my_project_name cd my_project_name composer require --dev symfony/web-profiler-bundle
- Edit Apache configuration to work smoothly with Symfony
nano /etc/apache2/sites-available/000-default.conf
- Enter this:
<VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/html/my_project_name/public <Directory /var/www/html/my_project_name/public> AllowOverride All </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
- Restart Apache
service apache2 restart
- Edit/Create .htaccess
nano /var/www/html/my_project_name/public/.htaccess
- Enter this:
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php [QSA,L] </IfModule>
- Open in browser
http://127.0.0.1:8080/
The docker run
command is encapsulated in a batch file (start-container.bat
) for simplicity and convenience. This allows you to start the container with a single command, avoiding the need to remember and type out the full docker run
command each time.