Creating a PHP container: Difference between revisions

From DWAMconsult Wiki
Jump to navigation Jump to search
No edit summary
Line 2: Line 2:
== Intro ==
== Intro ==


* Goal: create a container running Apache/PHP for testing out PHP code
=== Overview ==
 
* 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
* 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
 
=== Create custom PHP container ===
 
* Begin: find a container image to use.  
* Begin: find a container image to use.  
** https://hub.docker.com/_/php
** https://hub.docker.com/_/php

Revision as of 12:50, 2 May 2024

Intro

= Overview

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

Create custom PHP container

FROM php:7.4-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 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