Home Backend Development PHP Tutorial The combination of Docker Compose, Nginx and MariaDB: Efficiently operate and maintain PHP applications

The combination of Docker Compose, Nginx and MariaDB: Efficiently operate and maintain PHP applications

Oct 12, 2023 pm 02:14 PM
docker nginx mariadb

Docker Compose、Nginx和MariaDB的搭配之道:高效运维PHP应用程序

How to match Docker Compose, Nginx and MariaDB: Efficiently operate and maintain PHP applications

Introduction:

In today's Internet era, applications The demand and scale of the network continue to grow, so efficient operation and maintenance and deployment solutions are crucial. Docker is a popular containerization platform that solves the complexities of application deployment and management. Docker Compose is a powerful tool of Docker that allows us to define and manage a combination of multiple containers through a simple configuration file. In this article, we will focus on how to use Docker Compose with Nginx and MariaDB to efficiently operate and maintain PHP applications, and provide specific code examples.

1. Introduction to Docker Compose

Docker Compose is a tool officially launched by Docker for defining and managing multiple Docker containers. Through a simple YAML file, you can easily define multiple containers, dependencies between containers, network configuration, etc.

Benefits of using Docker Compose include:

  1. Simplified deployment process: By defining configuration files, the entire application can be deployed quickly and consistently.
  2. Improve scalability: Applications can be easily scaled across multiple hosts.
  3. Easy to manage and monitor: You can start, stop, restart and view logs of containers through Docker Compose commands.

2. Use of Nginx

Nginx is a lightweight, high-performance web server that can be used as a reverse proxy server, load balancing server, static resource server, etc. use. In our PHP application, we can use Nginx as a reverse proxy server to forward requests to the PHP-FPM server on the backend.

The following is a sample configuration file (docker-compose.yml) using Docker Compose and Nginx:

version: '3'

services:
  nginx:
    image: nginx:latest
    ports:
      - 80:80
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./public:/var/www/html

  php:
    image: php:7.4-fpm
    volumes:
      - ./public:/var/www/html

networks:
  default:
    external:
      name: my-network
Copy after login

In the above configuration, we define a service named nginx and a name For php services. Among them, the nginx service uses the official nginx image and maps the container's port 80 to the host's port 80. At the same time, we mount the nginx configuration file (nginx.conf) and the public directory where our PHP application is located into the container.

3. Use of MariaDB

MariaDB is a free, open source relational database management system and a branch of MySQL, providing high-performance and high-reliability database solutions. In our PHP application we can use MariaDB as backend database.

The following is a sample configuration file (docker-compose.yml) using Docker Compose and MariaDB:

version: '3'

services:
  db:
    image: mariadb:latest
    ports:
      - 3306:3306
    environment:
      - MYSQL_ROOT_PASSWORD=secret
      - MYSQL_DATABASE=my_database
      - MYSQL_USER=my_user
      - MYSQL_PASSWORD=my_password
    volumes:
      - ./data:/var/lib/mysql

  php:
    image: php:7.4-fpm
    volumes:
      - ./public:/var/www/html

networks:
  default:
    external:
      name: my-network
Copy after login

In the above configuration, we define a service named db and a service named For php services. Among them, the db service uses the official MariaDB image and maps the container's 3306 port to the host's 3306 port. We also set the relevant configuration of the database through environment variables, such as root password, database name, user and password, etc. At the same time, we mount the database folder into the container to ensure persistent storage of data.

4. Complete sample configuration

The following is a complete sample configuration for efficient operation and maintenance of PHP applications using Docker Compose, Nginx and MariaDB:

version: '3'

services:
  nginx:
    image: nginx:latest
    ports:
      - 80:80
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./public:/var/www/html
    depends_on:
      - php
    networks:
      - my-network

  php:
    image: php:7.4-fpm
    volumes:
      - ./public:/var/www/html
    networks:
      - my-network

  db:
    image: mariadb:latest
    ports:
      - 3306:3306
    environment:
      - MYSQL_ROOT_PASSWORD=secret
      - MYSQL_DATABASE=my_database
      - MYSQL_USER=my_user
      - MYSQL_PASSWORD=my_password
    volumes:
      - ./data:/var/lib/mysql
    networks:
      - my-network

networks:
  my-network:
    external: true
Copy after login

In the above configuration, we have defined a service named nginx, a service named php and a service named db. At the same time, we also defined an external network named my-network to connect these three services. The nginx service depends on the php service, so we use the depends_on keyword to specify this dependency.

5. Summary

The above are methods and specific code examples for using Docker Compose, Nginx and MariaDB to efficiently operate and maintain PHP applications. By using the Docker Compose tool, we can quickly build and manage applications containing multiple containers, improving deployment efficiency and operation and maintenance convenience. As a high-performance web server, Nginx can help us implement functions such as reverse proxy and load balancing. MariaDB, as a reliable database management system, provides high-performance and high-availability data storage solutions for our applications.

I hope this article will help you understand and use Docker Compose, Nginx and MariaDB. I wish you better results and experience in the process of operating and maintaining PHP applications!

The above is the detailed content of The combination of Docker Compose, Nginx and MariaDB: Efficiently operate and maintain PHP applications. 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 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 <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)

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] <Container Path> <Host Path>. 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.

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 <container_id>); start the container (docker start <container_id>); verify that the restart is successful (docker ps). Other methods: Docker Compose (docker-compose restart) or Docker API (see Docker documentation).

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

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 create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

See all articles