Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Docker compose to orchestrate Laravel applications

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use Docker compose to orchestrate Laravel applications. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Environmental requirements

First, Docker and Docker compose should be installed, and the Docker repository image should be replaced with a domestic one. Normally, I will run a Vagrant on my development computer, and then run applications such as Docker in it.

Main ideas

Docker officially recommends that a container runs a service, so there will be Compose choreography, and the services communicate with each other through container interconnection technology. For example, the Php service connection Mysql only needs to write the Host name as the container name, and the internal will be directly converted into a specific ip. The code directory uses data volumes to map from the container to the host, and configuration files (Nginx, etc.) are also mapped to the container through data volumes.

Practice

I have packaged this set of services, and if I usually use them, I will only use clone to use them directly. Here I mainly talk about the implementation ideas.

Project address: https://github.com/rootrl/php-environment-with-docker

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 is full of encapsulated command-line tools, which are also Docker container services, but they all leave after using them.

Conf this directory is the configuration directory of the application, and will be mapped into the container using Volumn.

The orchestration file of docker-composer.yaml compose, which will be discussed in detail below

Image construction of Dockerfile.php php (there will be some customizations, such as changing dns to install special extensions)

Start runs. / start to start all services, and restart can also run this command

Docekr-compose.yaml

This file is an orchestration file for 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 oa-network container_name 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" networks:-oa-network container_name: " Oa-mysql "redis: image:" redis "ports: -" 6379 redis "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 common networks is defined so that communication can be easily carried out within the container.

Like 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 500502 503 504 / 50x.htl; 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;}}

How to connect with php-fpm here: 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\ 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 a Php image build, where the dns server has been changed and several php extensions have been installed.

Use

Start

. / start starts all services

Command Lin

. / bin/php-v # Laravel artisan./bin/php artisan this article on "how to use Docker compose to arrange Laravel applications" ends here. I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report