Creating a PHP container: Difference between revisions
Jump to navigation
Jump to search
(→Intro) |
|||
(One intermediate revision 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 | ||
* 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
- 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.
- Find an official image e.g.
docker search php -f is-official=true
- For example:
docker image pull php:7.4-apache
- Create a Dockerimage file in the same directory as the PHP files you're using
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
- 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
- https://www.howtogeek.com/devops/how-to-use-docker-to-containerise-php-and-apache/