Creating a PHP container: Difference between revisions

From DWAMconsult Wiki
Jump to navigation Jump to search
 
(4 intermediate revisions by the same user not shown)
Line 2: Line 2:
== Intro ==
== Intro ==


* We need to build a custom image for PHP with extensions installed, as it does not seem to play nicely with the build option in a docker-composer.yml file
* We need to build a custom image for PHP with extensions installed and a separate image to configure the Apache/httpd container. We don't need to customize the phpMyAdmin image (only use this for dev/testing) or the MariaDB/MySQL image
* Once we have built the custom PHP image we can use docker-compose.yml to string the PHP container along with separate ones for Apache, MariaDB and phpMyAdmin
* Once we have built the images we can use docker-compose.yml to bring up the four containers and connect them


=== Create custom PHP container ===
=== Create custom PHP container ===
Line 10: Line 10:
** https://hub.docker.com/_/php
** https://hub.docker.com/_/php
** https://hub.docker.com/_/php/tags
** https://hub.docker.com/_/php/tags
** https://www.cloudsavvyit.com/10528/how-to-use-docker-to-containerise-php-and-apache/
* Find an official image e.g. <code>docker search php -f is-official=true</code>
* Find an official image e.g. <code>docker search php -f is-official=true</code>
* For example: <code>docker image pull php:7.4-apache</code>
* For example: <code>docker image pull php:7.4-apache</code>
* Create a Dockerimage file in the same directory as the PHP files you're using
* Create a Dockerimage file in the same directory as the PHP files you're using


  <nowiki>FROM php:7.4-apache
  <nowiki>FROM php:8.3-fpm-apache


WORKDIR /var/www/html
WORKDIR /var/www/html
Line 29: Line 28:
* Have to modify the base image to install PHP extensions, see the link to official PHP Docker image https://hub.docker.com/_/php
* Have to modify the base image to install PHP extensions, see the link to official PHP Docker image https://hub.docker.com/_/php


== Adding MySQL and phpMyAdmin ==
=== Adding the custom Apache/httpd container ===
 
* Create a directory e.g. httpd in local environment (where PHP files are)
* Create a Dockerfile:
 
<nowiki>FROM httpd:latest
 
RUN mkdir -p /var/www/html
 
WORKDIR /usr/local/apache2/conf
 
COPY httpd.conf /usr/local/apache2/conf/httpd.conf</nowiki>
 
* Create the httpd.conf file.  You need to get an existing httpd.conf file (e.g. from the base image, spin one up and run it interactively and copy the httpd.conf file) and add customizations to it.  For example to add FPM support add this to the bottom:
 
<nowiki><FilesMatch \.php$>
SetHandler "proxy:fcgi://php:9000"
</FilesMatch>
 
<IfModule mod_fcgid.c>
  FcgidConnectTimeout 20
  AddType  application/x-httpd-php .php
  AddHandler application/x-httpd-php .php
 
  <IfModule mod_mime.c>
    AddHandler fcgid-script .fcgi
  </IfModule>
</IfModule></nowiki>
 
=== Adding MySQL and phpMyAdmin ===


* MySQL:
* MySQL:
Line 42: Line 70:
** Can then administer DB using phpmyadmin on localhost:8081
** Can then administer DB using phpmyadmin on localhost:8081


== Docker Compose file ==
=== Docker Compose file ===


* Create docker-compose.yml in same directory as PHP files
* Create docker-compose.yml in same directory as PHP files
Line 84: Line 112:


== Links ==
== Links ==
* MySQL and phpMyAdmin https://migueldoctor.medium.com/run-mysql-phpmyadmin-locally-in-3-steps-using-docker-74eb735fa1fc
* MySQL and phpMyAdmin: https://migueldoctor.medium.com/run-mysql-phpmyadmin-locally-in-3-steps-using-docker-74eb735fa1fc
* Part 2: https://medium.com/geekculture/data-persistence-on-mysql-phpmyadmin-using-docker-in-3-steps-c3760d534e41
* Part 2: https://medium.com/geekculture/data-persistence-on-mysql-phpmyadmin-using-docker-in-3-steps-c3760d534e41
* https://www.howtogeek.com/devops/how-to-use-docker-to-containerise-php-and-apache/
* https://www.howtogeek.com/devops/how-to-use-docker-to-containerise-php-and-apache/
* https://www.cloudsavvyit.com/10528/how-to-use-docker-to-containerise-php-and-apache/

Latest revision as of 06:07, 11 May 2024

Intro

  • We need to build a custom image for PHP with extensions installed and a separate image to configure the Apache/httpd container. We don't need to customize the phpMyAdmin image (only use this for dev/testing) or the MariaDB/MySQL image
  • Once we have built the images we can use docker-compose.yml to bring up the four containers and connect them

Create custom PHP container

FROM php:8.3-fpm-apache

WORKDIR /var/www/html

RUN apt install php7.4-mysql

COPY . /var/www/html

EXPOSE 80
  • docker run -dp 3001:80 --mount type=bind,source="$(pwd)",target=/var/www/html php:7.4-apache
  • Can update code & will be reflected in the container when using http://localhost:3001 in browser
  • Have to modify the base image to install PHP extensions, see the link to official PHP Docker image https://hub.docker.com/_/php

Adding the custom Apache/httpd container

  • Create a directory e.g. httpd in local environment (where PHP files are)
  • Create a Dockerfile:
FROM httpd:latest

RUN mkdir -p /var/www/html

WORKDIR /usr/local/apache2/conf

COPY httpd.conf /usr/local/apache2/conf/httpd.conf
  • Create the httpd.conf file. You need to get an existing httpd.conf file (e.g. from the base image, spin one up and run it interactively and copy the httpd.conf file) and add customizations to it. For example to add FPM support add this to the bottom:
<FilesMatch \.php$>
SetHandler "proxy:fcgi://php:9000"
</FilesMatch>

<IfModule mod_fcgid.c>
  FcgidConnectTimeout 20
  AddType  application/x-httpd-php .php
  AddHandler application/x-httpd-php .php

  <IfModule mod_mime.c>
    AddHandler fcgid-script .fcgi
  </IfModule>
</IfModule>

Adding MySQL and phpMyAdmin

  • MySQL:
    • docker pull mariadb:latest (or mysql:latest or whatever version you'd like)
    • docker run --name my-own-mysql -e MYSQL_ROOT_PASSWORD=mypass123 -d mysql:8.0.1
    • Persist the database volume: docker run --name my-own-mysql -v mysqldb:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=mypass123 -d mysql:8.0.1
    • docker exec -it my-own-mysql bash
    • mysql --user=root --password=mypass123
  • PHPMyAdmin:
    • docker pull phpmyadmin/phpmyadmin:latest
    • docker run --name my-own-phpmyadmin -d --link my-own-mysql:db -p 8081:80 phpmyadmin/phpmyadmin (links to the DB container)
    • Can then administer DB using phpmyadmin on localhost:8081

Docker Compose file

  • Create docker-compose.yml in same directory as PHP files
version: "3.8"
services:
  app:
    build: .
    ports:
      - 3000:80
    working_dir: /var/www/html
    volumes:
      - ./:/var/www/html
    environment:
     MYSQL_HOST: mariadb
     MYSQL_USER: root
     MYSQL_PASSWORD: secret
     MYSQL_DB: php

  mariadb:
    image: mariadb:latest
    volumes:
      - mariadb-data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: php

  phpmyadmin:
     image: phpmyadmin:latest
     ports:
       - 3001:80
     environment:
        - PMA_ARBITRARY=1

volumes:
  mariadb-data:

  • Run docker-compose up & from the directory containing PHP files & docker-compose.yml file (omit & to see log messages in real time)
  • PHPMyAdmin is accessed via browser localhost:3001, connect to the database server name mariadb with root and secret (PMA_ARBITRARY means you can specify which database to connect to)

Links