Zum Inhalt springen

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

I’ve come up with this docker-compose file:



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:

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: https://forge.nuculabs.de/dnutiu/containerized-php-application

Thank you for reading, if you have any questions please do reach out here 😉

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert