Title: Implementing container extensions for PHP applications using Docker Compose, Nginx and MariaDB
With the development of container technology, more and more developers are beginning to use Docker to build and manage application environments. In this article, we will take a PHP application as an example to demonstrate how to use Docker Compose, Nginx, and MariaDB to scale a containerized environment.
Docker Compose is a tool officially provided by Docker for defining and managing a combination of multiple containers. Nginx is a high-performance HTTP server and reverse proxy server, while MariaDB is an open source relational database management system.
First, we need to create a file called docker-compose.yml to define and configure the required containers. In this file, we will use three services: web, db and phpmyadmin.
version: '3' services: web: image: nginx:latest ports: - "80:80" volumes: - ./app:/var/www/html - ./config/nginx:/etc/nginx/conf.d depends_on: - php networks: - app-network db: image: mariadb:latest environment: - MYSQL_ROOT_PASSWORD=password - MYSQL_DATABASE=app_database - MYSQL_USER=app_user - MYSQL_PASSWORD=password volumes: - ./data:/var/lib/mysql networks: - app-network phpmyadmin: image: phpmyadmin/phpmyadmin:latest links: - db:db ports: - "8080:80" environment: - PMA_HOST=db - MYSQL_ROOT_PASSWORD=password networks: - app-network networks: app-network: driver: bridge
In this file, we define three services. The web service uses Nginx mirroring and maps port 80 of the host to port 80 of the container. volumes specifies mapping the ./app directory on the host to the container's /var/www/html directory (the application code on the host will be placed in the ./app directory), and mapping the ./config/nginx directory on the host Map to the /etc/nginx/conf.d directory of the container (used to configure Nginx). depends_on specifies that the web service depends on the startup of the php service. networks defines a network called app-network to connect these three services to the same network.
The db service uses the MariaDB mirror and specifies some environment variables through environment, such as root password, database name, user and password. volumes maps the ./data directory on the host to the container's /var/lib/mysql directory (for data persistence).
The phpmyadmin service uses the phpmyadmin image, and specifies the connection to the db service through links, and specifies the mapping of the host's 8080 port to the container's port 80 through ports. environment specifies some environment variables, such as the database host and root password.
Next, we need to create the corresponding directories and files on the host. In the terminal, execute the following command:
mkdir app mkdir config/nginx mkdir data
Then, place the application code in the app directory, and create an Nginx configuration file named default.conf and place it in the config/nginx directory. This configuration file will be mapped to the /etc/nginx/conf.d directory of the container.
Now, we can start the container. In the terminal, execute the following command:
docker-compose up -d
This command will create and start all containers and run them in the background. You can use the docker ps command to view all running containers.
Finally, access http://localhost through the browser, and you can see the running results of the PHP application in the container. You can also access phpMyAdmin at http://localhost:8080 to manage the MariaDB database.
At this point, we have successfully implemented container extensions for PHP applications using Docker Compose, Nginx and MariaDB. With Docker Compose, we can easily define and manage multiple containers, while Nginx and MariaDB provide us with efficient and reliable services.
I hope this article can help you and allow you to better understand and apply Docker technology. Happy programming!
The above is the detailed content of Container scaling for PHP applications using Docker Compose, Nginx and MariaDB. For more information, please follow other related articles on the PHP Chinese website!