Php mysql project running with Docker

In this tutorial, I am going to teach how to run a PHP MySQL project with Docker.

Create Dockerfile

When building a PHP Docker container for a project that connects to MySQL, you must install the required PHP extensions inside the container.  inside the docker file add this.

FROM php:8.2-apache

RUN docker-php-ext-install mysqli pdo pdo_mysql
RUN a2enmod rewrite

COPY . /var/www/html/

Create docker-compose.yml

When working with Docker, you often need more than one container to run an application. For example, a PHP project may require :

  • A PHP container

  • A MySQL database container

  • Sometimes phpMyAdmin

Managing multiple containers manually can become complicated. This is where docker-compose.yml becomes extremely powerful.

version: '3.8'

services:
  php:
    build: .
    ports:
      - "8080:80"
    volumes:
      - .:/var/www/html
    depends_on:
      - mysql

  mysql:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: mydatabase

Build Project

docker compose up --build

Run Project

http://localhost:8080

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

15 − four =