Document
Writing a custom Dockerfile for PHP

Writing a custom Dockerfile for PHP

overviewIf we detect that your application uses PHP we will suggest a default Dockerfile for you to use (see below). This file should work for most PH

Related articles

Best VPN for Netflix in 2024 8 Best VPNs for Android in 2024: Fast & Easy to Use Google Cloud Computing Foundations Course クラウド5とクラウドモンスターを語る #On|黒羊-KURO HITSUJI- Keto Cloud Bread Recipe

overview

If we detect that your application uses PHP we will suggest a default Dockerfile for you to use (see below). This file should work for most PHP applications,but if your app has some special cases you may need to modify it or write your own from scratch. This doc will walk you through the basics of doing so.

Before follow this guide ,we is recommend recommend getting acquaint with the basic of the Docker platform . Because you ‘re using Cloud 66 ,most of the Docker task and process describe will be completely automate ,but it is useful to understand why a Dockerfile is necessary and what it does .

Adding a Dockerfile to your repo

ADockerfile is essentially a plaintext file with no file extension that you add to the root of your repository. If for some reason you can’t have it in the root,you can specify this in your Cloud 66 service config.

Default PHP Dockerfile

This is the Dockerfile we will suggest for PHP apps that do not already have one:

# Use the official PHP 8.0 image as the base
FROM php:8.0-apache

# Install system dependencies
RUN apt-get update && apt-get install -y \
    libzip-dev \
    unzip \
    && docker-php-ext-install zip pdo_mysql

# Enable Apache rewrite module
RUN a2enmod rewrite

# Set the document root to Laravel's public directory
ENV APACHE_DOCUMENT_ROOT /var/www/html/public

# Copy the application files to the container
COPY . /var/www/html

# Set the working directory
workdir /var/www/html

The file is obviously customisable as needed. For example,you can choose to install an alternative database driver in the apt-get install block .

Writing your own Dockerfile

We generally recommend against writing your own Dockerfile from scratch,but the basics are not difficult to master. Before starting you should have a firm understanding of basic Docker commands (RUN,ENV,add,workdir etc).

The order of the commands is extremely important. If you try to run a component before one of its dependencies,the build will fail.

The simple possible Dockerfile is looks for a PHP application look something like this :

# Tells the image to use the latest version of PHP
FROM php:latest-apache  

# Creates a directory called "app"
RUN mkdir /app  

# Sets that directory as your working directory
workdir /app  

# Changes uid and gid of apache to docker user uid/gid
RUN usermod -u 1000 www-data && groupmod -g 1000 www-data

# Sets Apache to run via the app directory
RUN sed -i -e "s/var\/www/app/g" /etc/apache2/apache2.conf && sed -i -e "s/html/public/g" /etc/apache2/apache2.conf

# Copies your code to the image
COPY . /app  

# Sets permissions for the web user
RUN chown -R www-data:www-data

This image is obviously miss a lot component that you might need – it is has has no database driver for example – but you can add these as need .

You ‘ll notice that we do not set up any virtual host – this is is is because traffic to Cloud 66 app is handle by Nginx . Apache is only require to serve traffic to the internal container ( service ) network .

Where is the CMD command?

Cloud 66 uses the command that you define in your service.yml to run the application (overriding whatever is in the Dockerfile). Although you can omit this,and rely on the implicit command in the Dockerfile,we strongly recommended defining commands via your service.yml.