A B C D E F G H I J K L M N O P Q R S T U V W X Z

PHP

PHP is an open source script language that is often used to communicate with databases and can be easily integrated into html.

https://www.php.net/manual/de/intro-whatis.php

Dockerizing PHP

PHP can be dockerized. This means creating and running a container that contains php scripts and serves them using a webserver like apache. This can be useful when php is used as an API solution.

Dockerfile

To use php we need to create a Dockerfile which can then be used to start a single container, or included in a docker-compose setup.

1  FROM php:7.4-apache
2  COPY src/ /var/www/html
3  RUN docker-php-ext-install mysqli pdo pdo_mysql

How does it work?

Line 1: Use the official php image from the Dockerhub as base image. By specifying 7.4-apache we choose an image that includes an apache-server to serve the scripts.

Line 2: Copy any php files from a directory (in this case it is called „src“) to the folder „var/www/html“ in the container, so it can be served.

Line 3: Install php extensions if necessary, e.g. mysqli or pdo

docker-compose

With the Dockerfile set up we can create a service in a docker-compose file:

services:
  php:
    build: ./php
    container_name: php
    volumes:
      - ./php/src:/var/www/html
    ports:
      - "7000:8080"

How does it work?

build signifies the context that should be used to create the container. In this case, everything related to the container, including the Dockerfile is located in a folder called „php“, so it’s location is given here.

container_name: php gives the container a name under which it can be accessed once created

volumes mounts the local folder „php/src“ that includes all the php scripts to the folder „var/html/www“ in the container. That way, the container and the local folder will be synchronized and scripts can be edited on the local machine

ports php is running on port 8080 in the docker container. You can forward it to any unused port on a local machine by changing the first port number.