Home  >  Article  >  Backend Development  >  Quickly deploy PHP applications with Docker Compose

Quickly deploy PHP applications with Docker Compose

PHPz
PHPzOriginal
2023-10-12 09:25:46897browse

使用Docker Compose快速部署PHP应用程序

Use Docker Compose to quickly deploy PHP applications

Introduction:

Docker is an open source containerization platform that can integrate applications and their Dependencies are packaged into a separate container and run in isolation. Docker Compose is a tool for defining and running multi-container Docker applications. This article will introduce how to use Docker Compose to quickly deploy PHP applications, with specific code examples.

1. Preparation

Before you begin, you need to ensure that Docker and Docker Compose have been installed. Please follow the corresponding steps to install according to your operating system type.

2. Create a Docker Compose file

Create a file named docker-compose.yml in the project root directory, and write the following content in the file:

version: '3'
services:
  web:
    image: php:7.4-apache
    volumes:
      - ./src:/var/www/html
    ports:
      - "80:80"
    networks:
      - mynetwork
  db:
    image: mysql:8.0
    environment:
      - MYSQL_ROOT_PASSWORD=your_password
      - MYSQL_DATABASE=your_database
    volumes:
      - ./data:/var/lib/mysql
    networks:
      - mynetwork
networks:
  mynetwork:

In the above example, we defined two services: web and db.

  • The web service uses the php:7.4-apache image and mounts the local ./src directory to the container's /var/www/html directory to achieve synchronous code updates. At the same time, map port 80 of the container to port 80 of the host so that the application can be accessed through localhost.
  • The db service uses the mysql:8.0 mirror, and the environment variables of MYSQL_ROOT_PASSWORD and MYSQL_DATABASE are set, which are used to specify the MySQL root password and the name of the created database respectively. In addition, we also mount the local ./data directory to the container's /var/lib/mysql directory to persistently store database data.

3. Write PHP application

Create a folder named src in the project root directory and write your PHP application code in this folder.

4. Start the application

Open the terminal, switch to the project root directory, and run the following command to start the application:

docker-compose up -d

Wait a moment, Docker will automatically download all the required image and launch your application.

5. Access the application

Visit http://localhost in the browser, you should be able to see the running results of your PHP application.

Conclusion:

By using Docker Compose, we can quickly deploy PHP applications and ensure the repeatability and portability of the application. I hope this article can help you get started with Docker Compose quickly and achieve good results in practical applications. I wish you a happy use!

Reference link:

  • Docker official documentation: https://docs.docker.com/
  • Docker Compose official documentation: https://docs.docker .com/compose/

The above is the detailed content of Quickly deploy PHP applications with Docker Compose. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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