DEV Community

vimuth
vimuth

Posted on

Add Docker for simple PHP application

Docker is an open-source platform that automates the deployment, scaling, and management of applications by using containerization.

Using Docker for your PHP application offers several benefits:

  1. Consistency: Docker ensures that your application runs the same way on any system, eliminating "it works on my machine" issues.
  2. Isolation: Containers keep your PHP application and its dependencies isolated from other applications, preventing conflicts.
  3. Portability: Docker containers can be easily moved across different environments (development, testing, production) without modification.
  4. Scalability: Docker makes it easy to scale your application by running multiple container instances.
  5. Efficiency: Containers are lightweight and start quickly, improving resource utilization and performance.

Now let's got to the code. First let's create this docker-compose.yaml file. Since we need to use multiple services here like PHP, MYSQL, PHPMYADMIN we need to work with multiple containers and images. So we use Docker compose and a yml file. So let's create docker-compose.yaml in our root.

version: '3'
services:
  php:
    build: .
    ports:
      - 80:80
    volumes:
      - ./project:/var/www/project


Enter fullscreen mode Exit fullscreen mode

php Service:

  • build: .
    This tells Docker to build the container for this service using the Dockerfile in the current directory (.).

  • ports:
    80:80: This maps port 80 on your host machine to port 80 in the container. It allows you to access the service using http://localhost.

  • volumes:
    ./project:/var/www/project: This mounts the ./project directory from your host machine to /var/www/project in the container. It lets you share files between your host and the container.

Now let us go to the Dockerfile. Create a file called Dockerfile and add these contents.

FROM php:8.1-apache

RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"

# PHP extensions
RUN docker-php-ext-install pdo pdo_mysql && a2enmod rewrite

# Virtual host configuration
ADD config/apache-pm.conf /etc/apache2/sites-available/pm.conf
RUN ln -sr /etc/apache2/sites-available/pm.conf /etc/apache2/sites-enabled
RUN rm /etc/apache2/sites-enabled/000-default.conf
Enter fullscreen mode Exit fullscreen mode

this uses "php:8.1-apache" as base image and add other configurations.

And then here are other services.

mysql:
    image: mysql:5.7
    ports:
      - "3306:3306"
    environment: 
      MYSQL_DATABASE: laravel
      MYSQL_USER: laravel
      MYSQL_PASSWORD: secret
      MYSQL_ROOT_PASSWORD: secret  
    volumes:
      - ./mysql:/var/lib/mysql
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports:
      - 8001:80
    environment:
      - PMA_HOST=mysql
      - PMA_PORT=3306    
Enter fullscreen mode Exit fullscreen mode

These are the services for adding mysql and phpmyadmin services. For them we don't need to run any commands or change configs. So we are calling containers directly. And you need to create mysql and project folders inside the root for volumes too. This way we can keep both mysql and project data even after containers destroyed.

And then you need to add config here 'config\apache-pm.conf'

<VirtualHost *:80>
    ServerAdmin root@localhost
    DocumentRoot /var/www/project

    <Directory /var/www/project>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
    </Directory>
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

These are the virtual host config for apache. Finally this is the folder structure.

Image description

Now create this file 'project\index.php' and add this simple content

<?php 

echo 'dfdf';

?>
Enter fullscreen mode Exit fullscreen mode

Now you can run

docker-compose up
Enter fullscreen mode Exit fullscreen mode

So you can got to http://localhost/ in the browser and can run this.

Top comments (0)