📜  docker compose php (1)

📅  最后修改于: 2023-12-03 14:40:49.034000             🧑  作者: Mango

Docker Compose PHP

Docker Compose is a tool that allows you to define and run multi-container Docker applications. It simplifies the process of managing and deploying complex applications by defining and configuring all the necessary services in a single file.

In the context of PHP development, Docker Compose can be used to set up and manage a development environment that includes a PHP server, a database, and any other services required by the application.

Getting Started

To get started with Docker Compose for PHP, you'll need to have Docker and Docker Compose installed on your system. Once installed, create a new directory for your PHP project and navigate into it.

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

version: '3'
services:
  web:
    image: php:8.0-apache
    ports:
      - 8000:80
    volumes:
      - ./src:/var/www/html
    depends_on:
      - db
  db:
    image: mysql:8.0
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: myapp

This docker-compose.yml file defines two services: web and db. The web service uses the official PHP 8.0 with Apache image, exposes port 8000 on the host, mounts the ./src directory as the document root, and depends on the db service. The db service uses the official MySQL 8.0 image, exposes port 3306 on the host, and sets environment variables for the root password and the database name.

Running the Application

To start the application, open a terminal in the project directory and run the following command:

docker-compose up

Docker Compose will pull the necessary images, create the containers, and start the services defined in the docker-compose.yml file.

You can now access your PHP application in the browser at http://localhost:8000.

Customizing the Environment

You can customize the PHP configuration by creating a php.ini file in the project directory and mounting it as a volume in the web service:

version: '3'
services:
  web:
    image: php:8.0-apache
    ports:
      - 8000:80
    volumes:
      - ./src:/var/www/html
      - ./php.ini:/usr/local/etc/php/php.ini
    depends_on:
      - db

You can also add additional services or modify existing ones according to your project requirements.

Conclusion

Docker Compose PHP provides a convenient way to manage Dockerized PHP applications. It helps in replicating development environments across different machines, simplifies the deployment process, and ensures consistent setups. Docker Compose is widely used by PHP developers to streamline their workflow and make their applications more portable.

For more information, refer to the official Docker Compose documentation.