Results 1 to 2 of 2

Thread: Docker Container: PHP - MySQL - Apache

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,037

    Docker Container: PHP - MySQL - Apache

    I do quite a bit of PHP development where I am deploying to cloud based hosting provides (e.g. BlueHost, HostGator, etc.) where they are running Linux that uses PHP, MySQL, and Apache. This is the necessary docker files to run the application locally using Docker.

    Some background, the file structure looks like this:
    1. /
      1. application
        1. {php/html files go in this directory}
      2. docker-compose.yml
      3. Dockerfile
      4. php.ini


    Dockerfile:
    Code:
    FROM php:8.2-apache
    
    RUN apt-get update && apt-get install -y \
        libzip-dev libmariadb-dev libmariadb-dev-compat libpq-dev \
        vim \
        && docker-php-ext-install \
        pdo pdo_mysql zip
    
    COPY ./php.ini /usr/local/etc/php/php.ini
    
    WORKDIR /var/www/html
    
    COPY . /var/www/html
    
    RUN chown -R www-data:www-data /var/www/html \
        && chmod -R 755 /var/www/html
    
    # this assumes you're using composer to install 3rd party libraries
    # if not, then comment the next line or delete it
    COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
    
    EXPOSE 80
    
    CMD ["apache2-foreground"]
    
    RUN echo "ServerName localhost" > /etc/apache2/conf-available/servername.conf \
        && ln -s /etc/apache2/conf-available/servername.conf /etc/apache2/conf-enabled/servername.conf
    
    RUN a2enmod rewrite
    docker-compose.yml:
    Code:
    services:
      app:
        build: .
        container_name: php_app
        ports:
          - "8080:80"
        volumes:
          - ./application:/var/www/html
          - ./php.ini:/usr/local/etc/php/php.ini
        environment:
          - APACHE_DOCUMENT_ROOT=/var/www/html
        depends_on:
          - db
    
      db:
        image: mysql:5.7
        container_name: mysql_db
        ports:
          - "3306:3306"
        environment:
          MYSQL_ROOT_PASSWORD: root_password
          MYSQL_DATABASE: change_me_database_name
          MYSQL_USER: change_me_database_username
          MYSQL_PASSWORD: change_me_database_password
        volumes:
          - mysql_data:/var/lib/mysql
    
      phpmyadmin:
        image: phpmyadmin:5.2.1
        container_name: phpmyadmin
        depends_on:
          - db
        ports:
          - "8081:80"
        environment:
          PMA_HOST: db
          MYSQL_ROOT_PASSWORD: change_me_database_password
    
    volumes:
      mysql_data:
    What this will do is copy the source code, located in the application directory, to the /var/www/html and install all the necessary infrastructure to run PHP locally.

    To run the application, start Docker then run the following commands:
    Code:
    docker-compose build --no-cache && docker-compose up -d
    To access the PHP application, go to localhost:8080 in your browser. To access phpMyAdmin, go to localhost:8081.

    To stop the application, run this command:
    Code:
    docker-compose down --remove-orphans
    Because of the db column in docker-compose.yml, it will save the state of the database when you shut down the docker container.

    This setup is useful because:
    1. You do not need PHP/MySQL/Apache installed on your local machine.
    2. You can manage your code base via a code repository (i.e. Git)
    3. Your developing environment will replicate the exact environment you are deploying to.
    Last edited by dday9; Jan 19th, 2025 at 03:37 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,037

    Re: Docker Container: PHP - MySQL - Apache

    This tid bit of information is useful in development.

    You can set up the Docker container to recognize changes in your application directory by using volume mounts in your docker-compose.yml.

    1. Start the Docker container by running build/compose (see post #1)
    2. Edit the apache2.conf by running:
      Code:
      docker exec -it php_app bash
      vim /etc/apache2/apache2.conf
    3. Scroll to the bottom of the file using the keyboard
    4. Hit the insert key
    5. Paste the following at the end of the file:
      Code:
      EnableSendfile Off
      FileETag None
    6. Save the file by hitting escape followed by shift + Z + Z
    7. Restart the Docker container by running:
      Code:
      docker-compose down --remove-orphans && docker-compose build --no-cache && docker-compose up -d
    Last edited by dday9; Jan 19th, 2025 at 03:33 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width