Skip to content

Latest commit

 

History

History
156 lines (121 loc) · 5.8 KB

README.md

File metadata and controls

156 lines (121 loc) · 5.8 KB

Docker Fullstack App

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.

Table of Contents

Requirements

  • Docker must be installed on your system.
  • Basic knowledge of the command line is helpful.

Installation

  1. Clone this repository:
    git clone https://github.com/1manfactory/Docker-Fullstack-App new-project
  2. Navigate to the project directory:
    cd new-project
  3. Run the build-container.bat script to build the Docker container:
    .\build-container.bat

Usage

  1. Start the container with:

    .\start-container.bat
  2. Access the local server in your browser:

  3. Run bash

    docker ps
    docker exec -it <container_id> /bin/bash

Directory Structure

  • 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.

Configuration Explanation

Dockerfile

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.

init.sh

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.

supervisord.conf

The supervisord.conf file configures Supervisor to manage multiple services (Apache, MariaDB, MailHog) within the container, ensuring they all start correctly.

Advantages over XAMPP

  • 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.

Using Volumes

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:

  1. 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.

  2. 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.

Symfony

  1. Run bash
    docker ps
    docker exec -it <container_id> /bin/bash
  2. 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
  3. Edit Apache configuration to work smoothly with Symfony
    nano /etc/apache2/sites-available/000-default.conf
  4. 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>
  5. Restart Apache
    service apache2 restart
  6. Edit/Create .htaccess
    nano /var/www/html/my_project_name/public/.htaccess
  7. Enter this:
    <IfModule mod_rewrite.c>
    	RewriteEngine On
    	RewriteCond %{REQUEST_FILENAME} !-f
    	RewriteRule ^(.*)$ index.php [QSA,L]
    </IfModule>
  8. Open in browser
    http://127.0.0.1:8080/

Why Use Batch Files?

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.