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











session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

React,Vue,andAngularcanbeintegratedwithLaravelbyfollowingspecificsetupsteps.1)ForReact:InstallReactusingLaravelUI,setupcomponentsinapp.js.2)ForVue:UseLaravel'sbuilt-inVuesupport,configureinapp.js.3)ForAngular:SetupAngularseparately,servethroughLarave

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

How to build portable applications with Docker and Linux? First, use Dockerfile to containerize the application, and then manage and deploy the container in a Linux environment. 1) Write a Dockerfile and package the application and its dependencies into a mirror. 2) Build and run containers on Linux using dockerbuild and dockerrun commands. 3) Manage multi-container applications through DockerCompose and define service dependencies. 4) Optimize the image size and resource configuration, enhance security, and improve application performance and portability.

1. The Origin of .NETCore When talking about .NETCore, we must not mention its predecessor .NET. Java was in the limelight at that time, and Microsoft also favored Java. The Java virtual machine on the Windows platform was developed by Microsoft based on JVM standards. It is said to be the best performance Java virtual machine at that time. However, Microsoft has its own little abacus, trying to bundle Java with the Windows platform and add some Windows-specific features. Sun's dissatisfaction with this led to a breakdown of the relationship between the two parties, and Microsoft then launched .NET. .NET has borrowed many features of Java since its inception and gradually surpassed Java in language features and form development. Java in version 1.6

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

phpMyAdmin simplifies MySQL database management through the web interface. 1) Create databases and tables: Use graphical interface to operate easily. 2) Execute complex queries: such as JOIN query, implemented through SQL editor. 3) Optimization and best practices: including SQL query optimization, index management and data backup.

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin
