Running a PHP Application inside a Container

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 Iwanted the ability to be able to run and install then locally.

I’ve come up with this docker-compose file

YAML:
services:
  nginx:
    build:
      context: ./
      dockerfile: nginx.dockerfile
    ports:
      - "8080:80"
    volumes:
      - ./config/nginx/conf.d:/etc/nginx/conf.d:z
      - ./data/nginx:/var/log/nginx:z
      - application:/var/www/html:z
    depends_on:
      - php
  php:
    build:
      context: ./
      dockerfile: php83.dockerfile
    volumes:
      - ./config/php.ini:/usr/local/etc/php/php.ini:z
      - application:/var/www/html:z
  database:
    image: "postgres:latest"
#    ports: # Uncomment to expose ports on localhost
#      - "127.0.0.1:15432:5432"
    environment:
      POSTGRES_PASSWORD: user
      POSTGRES_USER: example
    volumes:
      - ./data/postgres/:/var/lib/postgresql/data/:z
  maria:
    image: mariadb
    restart: always
#    ports: # Uncomment to expose ports on localhost
#      - "127.0.0.1:13306:3306"
    environment:
      MARIADB_ROOT_PASSWORD: example
    volumes:
      - ./data/maria/:/var/lib/mysql:z
volumes:
    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:

Bash:
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. SomePHP Applications work with PostgresSQL and some work only with MariaDB/MySQL.

The full code is available on my Forge: containerized-php-application

Thank you for reading, if you have any questions please do reach out here ;)
 
Last edited:
Back
Top