by Devin Yang
(This article was automatically translated.)

Published - 5 years ago ( Updated - 5 years ago )

foreword

D-Laravel is a PHP execution environment using Docker, which puts all services into containers for execution.
Database service ( db ), web server service ( web ), PHP-FPM service ( php )... etc.,
By defining the docker-compose.yml file, we can easily adjust the database version or PHP version.
D-Laravel also creates a simple bash, which can help us adopt docker more quickly to create Laravel projects and complete database settings.
Even if you are just a PHP developer, you can actually create your own development environment through D-Laravel.
Next, let us understand the settings made by D-Laravel through the official Docker documents.

About docker-compose.yml settings

The following is the docker-compose-normal.yml file of D-Laravel v1.6.10 version.
Please click the blue link below to view the instructions: ( Scroll down to provide simple Chinese instructions )
version : '3.6'
services :
#=== The container of the web server ========================
web :
image : nginx
dns : 8.8.8.8
ports :
- "80:80"
- "443:443"
volumes :
- ./sites:/var/www/html
- ./etc:/etc/nginx/conf.d
- ./var/log/web:/var/log/nginx
hostname: web
networks :
- dlaravel_net
  
#=== PHP-FPM container ============================
### Laravel 5.6/5.7 >= 7.1.3
### Laravel 5.5 >= 7.0.0
### Laravel 5.4 >= 5.6.4
### Laravel 5.3 between 5.6.4 & 7.1.*
php :
network_mode : "service: web"
image: deviny/fpm :7.2.10
#image: deviny/fpm:7.1.22
#image: deviny/fpm:7.0.32
#image: deviny/fpm:5.6.38
volumes:
- ./etc/php:/usr/local/etc/php/conf.d
- ./sites:/var/www/html
- ./etc/php-fpm.d/www.conf:/usr/local/etc/php-fpm.d/www.conf
###Build composer cache###
- ./etc/cache:/home/dlaravel/.composer/cache
#- ./var/log/php:/var/log
#- ./etc/supervisor:/etc/supervisor/conf.d
#- ./var/log/supervisor:/var/log/supervisor
environment :
-TZ=Asia/Taipei
#- PHP_IDE_CONFIG=serverName=dlaravel
#- XDEBUG_CONFIG="remote_host=???? profiler_enable=1"
#Create bash_aliases under etc, which can be used to customize the environment variables of dlaravel users
#- ./etc/bash_aliases:/home/dlaravel/.bash_aliases

#=== database container ==============================
db :
image: mysql :5.7.19
hostname: db
ports:
- "127.0.0.1:3306:3306"
volumes:
- ./etc/mysql/my.cnf:/etc/mysql/my.cnf
- ./data:/var/lib/mysql
environment:
#- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD-secret}
- MYSQL_ALLOW_EMPTY_PASSWORD="yes"
-TZ=Asia/Taipei
networks:
- dlaravel_net

#=== top-level netowks key ========================
networks:
dlaravel_net:
 
short description in Chinese
grammar Chinese description
version : '3.6' Obviously, it is the compose-file version. Different docker-compose file versions will use different Docker release versions.Please refer to this file for version compatibility
 
services In a distributed application, the different parts of the application are called "services". For example, if you imagine a video-sharing site, it might include a service for storing application data in a database, and a service for video transcoding in the background. User uploaded content, front-end services, etc.

A service is actually a "built-in container". The service will run an image, but the execution of the image is written in the service - such as which ports should be used, how many copies of the container should be executed, and the service has the required capacity, etc. . Scaling services adjusts the number of container instances running in the application, allocating more computing resources to the services in the process.

Fortunately, defining, running and scaling services is very easy with the Docker platform - just write a docker-compose.yml file.
(Author's note: I think the service here refers to the service in Docker swarm mode. A Service has multiple Tasks, so it can be scaled. In D-Laravel's stack.yml, there is a similar definition like a web service Replicas has 4 tasks, which is an advanced application, we can ignore it.)
=====
I translated the above with Goolge and adjusted it slightly, so I felt dizzy after reading it. Simply put, services literally have s, plural, so there are multiple services under services, starting with D- Taking Laravel as an example, there are three main services, namely web , php , and db .
This is why, in the project built by D-Laravel, the database host (DB_HOST) of Laravel's .env will be pointed to the db service, not 127.0.0.1.
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=test1
DB_USERNAME=test1
DB_PASSWORD=test1
image Define which image file to use. The image file of docker is mainly generated by dockerfile.
I can search for different image files on DockerHub , but please try to choose the image file marked as the official version to be safe.
dns

Custom DNS server. Can be one or more.

dns: 8.8.8.8
dns:
- 8.8.8.8
- 9.9.9.9
ports Define the ports to be exposed (Expose ports).
In the docker file, host refers to the host on which we execute docker. Through the settings of posts, we can connect to the open port in the container.
volumes Mount the directory on the host side into the container. In the docker-compose environment, when we execute docker-compose down, the container will be removed, so we can keep our data by mounting volumes.
Of course, the use of Volume (dossier) is definitely not limited to this, please check the official documentation for yourself.
environment Add environment variables, type env in the container to see the environment variables we defined.
hostname It is very obvious to directly translate the host name. It defines the name used when the container is executed, rather than displaying the Container ID.
networks In the setting of docker-compose, "services" under the same network can ping each other, so we can see that both web and db services are used
  networks:
- dlaravel_net
This means that we can ping the db service in the web container.
We can find that the method of --links is listed as not recommended , so the setting of D-Laravel does not use the method of links to connect between containers.
 
network_mode : "service: web" Under the service of php, have you noticed the setting of this network_mode?
 php:
network_mode: "service: web"
The php service is mainly php-fpm, which will open port 9000. The strategy here binds the port it opened to,
web services, so that there will be no problems when integrating Laravel Dusk applications.
There is no requirement that php-fpm must use this method. Of course, if you adjust to the network method, you must manually change the nginx settings.
For example: etc/default.conf.
Change the original web to php as follows, which means that the php service is no longer bound to the web service using network_mode.
fastcgi_pass web:9000;
changed to
fastcgi_pass php:9000;
Finally, I hope this article can let you understand the setting method of docker-compose.yml,
Then generate or adjust the environment settings that meet your needs.

Tags:

Devin Yang

Feel free to ask me, if you don't get it.:)

No Comment

Post your comment

Login is required to leave comments

Similar Stories


security,config

Raspberry Pi fail2ban installation notes

My mainframe basically has remote logs set up, and all of them are sent to the log center on the Synology NAS. Among them, there is a Raspberry Pi that is used as a router, and there will be a bunch of sshd verification attempts to log in. Basically, I only allow public key verification, so I don’t want to use passwords to brute force it, but a bunch of logs of verification failures are really good. Hate.

vpn,cli,macos

How do I dial a VPN from the command line on MacOS

I myself will need to use MacOS to dial VPN in the command line. Here I provide my alias and setting method

gitlab,reset password

reset gitlab password

The gitlab built with docker has been useless for a long time, and I forgot the password, how to reset it. ​​​​​​We should be able to enter the gitlab container through the following command...