Orchestrate Laravel applications using Docker compose
This article mainly introduces the use of Docker compose to orchestrate Laravel applications. It has certain reference value. Now I share it with everyone. Friends in need can refer to it
Preface
Laravel official The recommended development environment is Homestead (actually a packaged Vagrant box). I feel that this is relatively heavy, so I used Docker compose to organize a development environment and share it here.
Environmental requirements
Docker and Docker compose must be installed first, and it is best to replace the Docker warehouse image with a domestic one. Generally, I run Vagrant on my development computer, and then run Docker and other applications on it.
Main idea
What Docker officially recommends is that one container runs one service, so there will be Compose orchestration, and each service communicates through container interconnection technology. For example, when the Php service connects to Mysql, you only need to put the Host name Written as a container name, it will be directly converted into a specific IP internally. The code directory is mapped from the container to the host using data volumes, and the configuration files (Nginx, etc.) are also mapped to the container through data volumes.
Practice
I have encapsulated this set of services. For daily use, just clone it and use it directly. I will mainly talk about the implementation ideas here.
Project address: https://github.com/rootrl/php...
My project directory structure:
php-environment-with-docker/
├── bin
│ ├── composer
│ ├── getContainerIp
│ └── php
├── conf
│ ├── nginx
│ │ └── conf.d
│ │ │ └── nginx.conf
│ └── redis
│ └── redis.conf
├── docker-compose.yaml
├─ ─ Dockerfile.php
├── LICENSE
├── README.MD
└── start
bin These are all encapsulated command line tools. In fact, they are also Docker container services, but they are all disposable services.
conf This directory is the configuration directory of the application and will be mapped to the orchestration file of
docker-composer.yaml compose in the container using Volume , the following will talk specifically about the image construction of
Dockerfile.php php (there will be some customization, such as changing dns and installing special extensions)
start Run ./start to start all services. You can also run this command after restarting.
docekr-compose.yaml
This file is the orchestration file of compose
version: '2' services: nginx: depends_on: - "php" image: "nginx" volumes: - "$PWD/conf/nginx/conf.d:/etc/nginx/conf.d" - "$PWD/www:/usr/share/nginx/html" ports: - "8888:80" networks: - oa-network container_name: "oa-nginx" command: /bin/bash -c "mkdir -p /var/www && ln -s /usr/share/nginx/html /var/www && nginx -g 'daemon off;'" php: image: "oa-php-fpm" build: context: . dockerfile: "Dockerfile.php" networks: - oa-network container_name: "oa-php-fpm" volumes: - "$PWD/www:/var/www/html" mysql: image: mysql:5.7 volumes: - "$PWD/db_data:/var/lib/mysql" environment: MYSQL_ROOT_PASSWORD: root123 MYSQL_DATABASE: oa MYSQL_USER: oa MYSQL_PASSWORD: oa123 ports: - "3306:3306" networks: - oa-network container_name: "oa-mysql" redis: image: "redis" ports: - "6379:6379" networks: - oa-network volumes: - "$PWD/conf/redis/redis.conf:/usr/local/etc/redis/redis.conf" container_name: "oa-redis" networks: oa-network: driver: bridge
Four services php-fpm, nignx, mysql, and redis are defined here (if you need other services, add them yourself). Then a public networks are defined so that all containers can communicate easily.
For example, in nginx.conf
server { listen 80; server_name localhost; root /usr/share/nginx/html/public; index index.php index.html; location / { try_files $uri $uri/ /index.php?$query_string; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { fastcgi_pass php:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/html/public/$fastcgi_script_name; include fastcgi_params; } }
The connection method with php-fpm is: php:9000
Dockerfile.php
FROM php:7.2-fpm Run echo "nameserver 223.5.5.5" >> /etc/resolv.conf \ && echo "nameserver 223.6.6.6" >> /etc/resolve.conf \ && apt-get update \ && apt-get install -y \ libfreetype6-dev \ libjpeg62-turbo-dev \ libpng-dev \ && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \ && docker-php-ext-install -j$(nproc) gd \ && docker-php-ext-install mysqli pdo_mysql \ && pecl install swoole \ && pecl install redis \ && docker-php-ext-enable swoole redis
This is the Php image Build, change the dns server here, and install several php extensions.
Use
Start
./start to start all services
Command line
./bin/php -v # Laravel artisan ./bin/php artisan
The above is the entire content of this article, I hope it will be helpful Everyone’s learning is helpful. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
The above is the detailed content of Orchestrate Laravel applications using Docker compose. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Laravel provides a comprehensive Auth framework for implementing user login functions, including: Defining user models (Eloquent model), creating login forms (Blade template engine), writing login controllers (inheriting Auth\LoginController), verifying login requests (Auth::attempt) Redirecting after login is successful (redirect) considering security factors: hash passwords, anti-CSRF protection, rate limiting and security headers. In addition, the Auth framework also provides functions such as resetting passwords, registering and verifying emails. For details, please refer to the Laravel documentation: https://laravel.com/doc

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

In the Laravel framework version selection guide for beginners, this article dives into the version differences of Laravel, designed to assist beginners in making informed choices among many versions. We will focus on the key features of each release, compare their pros and cons, and provide useful advice to help beginners choose the most suitable version of Laravel based on their skill level and project requirements. For beginners, choosing a suitable version of Laravel is crucial because it can significantly impact their learning curve and overall development experience.

Want to learn the Laravel framework, but suffer from no resources or economic pressure? This article provides you with free learning of Laravel, teaching you how to use resources such as online platforms, documents and community forums to lay a solid foundation for your PHP development journey from getting started to master.

The Laravel framework has built-in methods to easily view its version number to meet the different needs of developers. This article will explore these methods, including using the Composer command line tool, accessing .env files, or obtaining version information through PHP code. These methods are essential for maintaining and managing versioning of Laravel applications.

Laravel and ThinkPHP are both popular PHP frameworks and have their own advantages and disadvantages in development. This article will compare the two in depth, highlighting their architecture, features, and performance differences to help developers make informed choices based on their specific project needs.
