Skip to main content
Running a PHP Application inside a Container
  1. Posts/

Running a PHP Application inside a Container

Denis Nutiu
Author
Denis Nutiu
Professional Software Engineer with ~10 years of experience.

Hello 👋,

In this month’s blog post I’ll show you how to run a PHP Application inside a container.

I’m quite a fan of online forums and the majority of forum software is written in PHP. To evaluate them quickly I wanted the ability to be able to run and install then locally.

I’ve come up with this docker-compose file

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
services:
  nginx:
    build:
      context: ./
      dockerfile: nginx.dockerfile
    ports:
      - "8080:80" # change port 10080 to any other port
    volumes:
      - ./config/nginx/conf.d:/etc/nginx/conf.d:z
      - ./.data/nginx:/var/log/nginx:z
      - application:/var/www/html:z
      - composer:/root/.composer:z
  php:
    build:
      context: ./
      dockerfile: php83.dockerfile
    volumes:
      - ./config/php.ini:/usr/local/etc/php/php.ini:z
      - application:/var/www/html:z
      - composer:/root/.composer:z
  database:
    image: "postgres:latest"
    ports:
      - 15432:5432
    environment:
      POSTGRES_PASSWORD: denis
      POSTGRES_USER: batman
    volumes:
      - ./.data/postgres/:/var/lib/postgresql/data/:z
  maria:
    image: mariadb
    restart: always
    ports:
      - 13306:3306
    environment:
      MARIADB_ROOT_PASSWORD: example
    volumes:
      - ./.data/maria/:/var/lib/mysql:z
volumes:
    composer:
    application:
      driver: local
      driver_opts:
        type: none
        device: ./application
        o: bind

All you need to do is place the PHP application inside the ./application directory and run:

1
podman compose up # or docker compose up

The file will set up the following components:

  • php - The PHP runtime.
  • nginx - The Nginx web server used to serve requests. You can customize it by editing ./config/nginx/conf.d/default.conf
  • database - Runs a PostgresSQL database which persists data inside the local ./.data directory.
  • maria - Runs a MariaDB database which persists data inside the local ./.data directory.

It’s unlikely that you need both databases running at the same time, feel free to delete the one you don’t need. Some PHP Applications work with PostgresSQL and some work only with MariaDB/MySQL.

Thank you for reading, if you have any questions please do reach out on @mastodon.

By the way, I’m using this method to host my forum.

The full code is available on forge: https://forge.nuculabs.dev/dnutiu/containerized-php-application

Related

Anubis: Protection against LLMs and Scrapers

Hello everyone! 👋 In this blog post we’re exploring the option of self hosting Anubis. Anubis is a software that defends and protects your services from AI Scrapers and LLMs. Since AI started to get popular more and more scrappers started appearing. They grab your data in order to use it to train their AIs while ignoring any rules such as robots.txt, licenses and intellectual property laws. Meta even torrented 82TB of books to train their AI.