Creating a PHP container

From DWAMconsult Wiki
Jump to navigation Jump to search

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