Home Backend Development PHP Tutorial Continuous delivery of PHP applications with Docker Compose, Nginx and MariaDB

Continuous delivery of PHP applications with Docker Compose, Nginx and MariaDB

Oct 12, 2023 pm 12:06 PM
php docker compose

通过Docker Compose、Nginx和MariaDB实现PHP应用程序的持续交付

Continuous delivery of PHP applications through Docker Compose, Nginx and MariaDB

Overview:
With the rapid development of cloud computing and containerization technology, more and more More and more applications are adopting containerization to achieve rapid delivery and deployment. This article will introduce how to use Docker Compose, Nginx and MariaDB to build a simple PHP application and implement the continuous delivery process. At the same time, we will give specific code examples to help readers better understand this process.

1. Preparation

  1. Install Docker and Docker Compose.

2. Create a Docker Compose configuration file
Create a file named docker-compose.yml and fill it in according to the following example content.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

version: '3'

services:

  web:

    build:

      context: .

      dockerfile: Dockerfile

    ports:

      - "80:80"

    depends_on:

      - db

  db:

    image: mariadb:latest

    environment:

      MYSQL_ROOT_PASSWORD: example

Copy after login

3. Create Nginx configuration file
Create a file named nginx.conf and fill it in according to the following example content.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

server {

    listen 80;

    server_name localhost;

    root /var/www/html;

    index index.php index.html;

 

    location / {

        try_files $uri $uri/ /index.php?$args;

    }

 

    location ~ .php$ {

        fastcgi_pass web:9000;

        fastcgi_index index.php;

        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    }

}

Copy after login

4. Create a PHP application file
Create a file named index.php in the project root directory and fill it in according to the following example content.

1

2

3

<?php

echo "Hello, World!";

?>

Copy after login

5. Create Dockerfile
Create a file named Dockerfile in the project root directory and fill in the following example content.

1

2

3

4

5

6

7

8

9

10

11

FROM php:7.4-fpm

 

WORKDIR /var/www/html

 

COPY . /var/www/html

 

RUN docker-php-ext-install mysqli pdo pdo_mysql

 

CMD ["php-fpm"]

 

EXPOSE 9000

Copy after login

6. Build and start the container
Execute the following commands in the project root directory to build and start the container.

1

docker-compose up -d

Copy after login

7. Verify the running results
Access http://localhost in the browser. If you see the words "Hello, World!", it means that the container has run successfully.

8. Continuous delivery process
When we update the application, we need to follow the following steps to achieve continuous delivery.

  1. Modify the code file
    Before modifying the code file, we need to stop the currently running container.

1

docker-compose down

Copy after login

Then, we can modify the index.php file, for example, change "Hello, World!" to "Hello, Docker!".

  1. Rebuild and start the container
    Execute the following command in the project root directory to rebuild and start the container.

1

docker-compose up -d --build

Copy after login
  1. Verify the running results
    Visit http://localhost in the browser. If you see the words "Hello, Docker!", it means that the update has been successfully deployed.

Conclusion:
By using Docker Compose, Nginx and MariaDB, we can quickly build a PHP application and implement the continuous delivery process. The advantage of containerization technology is that it can provide consistent development, testing and production environments, greatly simplifying deployment and maintenance. At the same time, using Nginx as a reverse proxy and load balancer can better manage request traffic. Through the above code examples, readers can further understand how to use these tools to implement the continuous delivery process.

The above is the detailed content of Continuous delivery of PHP applications with Docker Compose, Nginx and MariaDB. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to update the image of docker How to update the image of docker Apr 15, 2025 pm 12:03 PM

The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

How to exit the container by docker How to exit the container by docker Apr 15, 2025 pm 12:15 PM

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop &lt;container_name&gt; Command Use docker kill &lt;container_name&gt; command in the host terminal (force exit)

How to restart docker How to restart docker Apr 15, 2025 pm 12:06 PM

How to restart the Docker container: get the container ID (docker ps); stop the container (docker stop &lt;container_id&gt;); start the container (docker start &lt;container_id&gt;); verify that the restart is successful (docker ps). Other methods: Docker Compose (docker-compose restart) or Docker API (see Docker documentation).

How to copy files in docker to outside How to copy files in docker to outside Apr 15, 2025 pm 12:12 PM

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] &lt;Container Path&gt; &lt;Host Path&gt;. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

PHP: An Introduction to the Server-Side Scripting Language PHP: An Introduction to the Server-Side Scripting Language Apr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

How to use docker desktop How to use docker desktop Apr 15, 2025 am 11:45 AM

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

How to view the docker process How to view the docker process Apr 15, 2025 am 11:48 AM

Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

See all articles