With the rise of cloud computing and containerization technologies, Docker has become the preferred tool for developers in building, deploying, and managing applications. Docker provides a simple environment setup and convenient containerization solution for PHP applications. In this article, we will explore how to use Docker and Docker Compose to deploy and manage PHP applications, improving development efficiency and optimizing the application delivery process.
First, you need to install Docker on your local environment. Depending on your operating system, you can refer to the official documentation and follow the installation steps: [Docker Installation](https://docs.docker.com/install/).
Once Docker is installed, the next step is to install Docker Compose, which helps manage applications with multiple containers. You can find installation instructions for Docker Compose in the official documentation: [Docker Compose Installation](https://docs.docker.com/compose/install/).
Before deploying a PHP application, we need to create a Docker image that contains the required environment. First, create a file named `Dockerfile` that defines the steps for building the image.
Here is an example Dockerfile:
# Use the PHP 7.4 with Apache base image FROM php:7.4-apache <h1>Install PDO MySQL extension</h1> <p>RUN docker-php-ext-install pdo_mysql</p> <h1>Copy the application to the container's working directory</h1> <p>COPY . /var/www/html</p> <h1>Configure Apache</h1> <p>COPY apache.conf /etc/apache2/sites-available/000-default.conf</p> <h1>Modify Apache's DocumentRoot</h1> <p>RUN sed -ri -e 's!/var/www/html!/var/www/html/public!g' /etc/apache2/sites-available/000-default.conf</p> <h1>Set access permissions</h1> <p>RUN chown -R www-data:www-data /var/www/html</p> <h1>Enable Apache's rewrite module</h1> <p>RUN a2enmod rewrite</p> <h1>Set environment variables</h1> <p>ENV APACHE_DOCUMENT_ROOT=/var/www/html/public</p> <h1>Expose the container's port</h1> <p>EXPOSE 80</p> <h1>Start Apache server</h1> <p>CMD ["apache2-foreground"]<br>
The Dockerfile above uses the `php:7.4-apache` base image, installs the `pdo_mysql` extension, copies the application code to the container's specified directory, configures Apache settings, enables the `rewrite` module, sets the `DocumentRoot`, and grants appropriate file permissions to the `www-data` user.
Next, you need to create a Docker Compose file (`docker-compose.yml`) that defines and manages the configuration of your containers.
Here is an example docker-compose.yml file:
version: '3' <p>services:<br> app:<br> build:<br> context: .<br> dockerfile: Dockerfile<br> ports:<br> - "8080:80"<br> volumes:<br> - .:/var/www/html<br> depends_on:<br> - db</p> <p>db:<br> image: mysql:5.7<br> environment:<br> MYSQL_ROOT_PASSWORD: secret<br> MYSQL_DATABASE: mydatabase<br> MYSQL_USER: myuser<br> MYSQL_PASSWORD: mypassword<br>
This docker-compose.yml file defines two services: `app` and `db`. The `app` service uses the Docker image created previously, maps the container's port 80 to the local port 8080, and mounts the current directory to the `/var/www/html` directory in the container. The `db` service uses the official MySQL image and sets the database password and user credentials.
In the command line, navigate to your project's root directory and execute the following command to start the containers:
$ docker-compose up -d
The `-d` flag means to run the containers in the background.
Once the containers are up and running, you can access your PHP application by navigating to [http://localhost:8080](http://localhost:8080) in your browser. If everything is configured correctly, you should see your application's homepage.
Using Docker Compose, you can easily manage and control multiple containers. Here are some commonly used commands:
By using Docker and Docker Compose, you can efficiently deploy and manage PHP applications. Packaging the application along with its environment ensures consistent operation across different environments and simplifies scaling and delivery. We hope this article helps you better understand how to use Docker to deploy PHP applications and enhance your development workflow.