by Devin Yang
(This article was automatically translated.)

Published - 1 year ago ( Updated - 1 year ago )

I don't have time to shoot an introduction video, so I'll just grab some pictures and introduce the container environment deviny/phpenv I use.

https://github.com/DevinY/phpenv

phpenv can be regarded as an evolutionary version of my previous D-Laravel open source project. Conceptually, a lot of D-Laravel's operation methods and directory structure have been extended.
The update of the container is biased towards the user to control by himself and build his own image, so I am not very good at changing the version number.
In fact, the php version number of D-Laravel, I seem to have not touched it for a long time: p

The previous D-Laravel architecture was sub-domain-oriented, so there must be a domain name to link different projects.
If it is open to external use, it is not easy to adjust the nginx settings.
The new version of phpenv is port-oriented, and a random NAS or Router can be used as a reverse proxy to make the service public.
I use HAProxy myself, the certificate update is super fast and convenient, there are as many SSL certificates as you need, it is much more convenient to set up, and the host can be installed in seconds.

In phpenv, different container environments are created through unique .env. The phpenv folder can be placed in a Laravel project and executed separately.
It can also be placed outside the project to execute one or more Laravel projects at a time.

phpenv supports the Linux environment better, and can be adjusted for different users. Don't tell me that your permissions are fully open with 777. phpenv can precisely specify the execution permissions of php and nginx.
In the Linux environment, it is probably that the database will fail when it is started for the first time, and you need to manually set the correct directory owner 999.
Of course, you also need a more in-depth understanding of docker using phpenv.

Having said so much, let's take a look at the functions of phpenv, but it is definitely more than the ones mentioned below.
I just thought about it first😛

1. Multiple environment choices

 devin@CCC:/volume1/docker/phpenv$ ./link
Type number to select your project environment:
1) ccc 3) demo 5) phpenv 7) tn_env
2) chat 4) material 6) test
 #?

2. Display the USER_ID and GROUP_ID that can be defined and executed in the current environment

 devin@CCC:/volume1/docker/phpenv$ ./info
DotEnv Settings
SERVICES="ssh_db mariadb_ssh redis"
PROJECT=ccc
APP_URL=http://127.0.0.1:1056
FOLDER=/volume1/docker/ccc
NETWORK_MOD=host
HTTP_PORT=1056
SSH_PORT=2256
DB_PORT=3356
USER_ID=1026
 GROUP_ID=100

3. You can specify different "main" container configuration files through the DEFAULT parameter according to the project requirements. The default is docker-compose.yml in the directory.
Sometimes we need additional services ourselves, we can put yaml in the services directory, specified by SERVICES=.
In addition, I also provide some commonly used settings and combinations in the services directory.
The concept of using the services directory is completely the same as the setting method of D-Laravel. If you are a veteran of D-Laravel, you should have a feeling.

DEFAULT=custom will use custom.yml in the phpenv directory. This example file uses the version of php7.x intentionally.

 DotEnv Settings
DEFAULT=custom
SERVICES="mariadb"
PROJECT=hello
APP_URL=http://127.0.0.1:1050
FOLDER=/Users/devin/code/test
HTTP_PORT=1050
DB_PORT=1250
USER_ID=501
 GROUP_ID=20

4. Different services can be installed according to the needs. For example, in the above example, SERVICES= parameter, I only choose mariadb. This means to use the mariadb.yml file in the services directory.
5. Enter the container through a simple console command, and Laravel is friendly to the environment. When we use link to make a selection, we execute a mandatory soft link (ln -sf) on the .env file on phpenv to connect to the selected file.
After seeing the execution, there is also a line of instructions pointing out ".env link to envs/material".

 #顯示envs目錄內的所有專案設定檔
devin@CCC:/volume1/docker/phpenv$ ./link
Type number to select your project environment:
1) ccc 3) demo 5) phpenv 7) tn_env
2) chat 4) material 6) test
#?4
.env link to envs/material
==== current env file =====
SERVICES="ssh redis"
PROJECT=material
APP_URL=http://127.0.0.1:1051
FOLDER=/volume1/docker/material
NETWORK_MOD=host
HTTP_PORT=1051
DB_PORT=1250
SSH_PORT=2051
USER_ID=1026
GROUP_ID=100
devin@CCC:/volume1/docker/phpenv$ ./console
The "material" project of the php container.
dlaravel@a46c5e5cbec0:/var/www/html$ tinker
Psy Shell v0.11.9 (PHP 8.1.11 — cli) by Justin Hileman
 >

6. The Chinese environment is optimized, for example, Chinese can be seen in Tinekr

 > Content::find(1)->content1
= """
In Laravel, how to use ModelFactory to create Chinese fake data?
\n I did this for reference. \n
\n
<?php\n
 use FakerFactory as TwFacker;\n

7. Under the multi-environment configuration file, it supports the functions of "full open" and "full close".Use ./all start to start or ./all stop to stop all

 ./all start
Project: stisla
Project FILE: envs/stisla_1054
http://127.0.0.1:1054
[+] Running 3/3
⠿ Network stisla_dlaravel_net Created 0.0s
⠿ Container stisla-php-1 Started 0.3s
⠿ Container stisla-web-1 Started 0.5s


Project: * imap (current)
Project FILE: envs/imap
http://127.0.0.1:1057
[+] Running 3/3
⠿ Network imap_dlaravel_net Created 0.0s
⠿ Container imap-php-1 Started 0.3s
⠿ Container imap-web-1 Started 0.5s


Project: auth2
Project FILE: envs/auth2
 http://127.0.0.1:1055

8. Laravel-friendly settings, so in the Dockerfile, I also added nodejs support by default.

 #一個./console命令秒進容器
devin@CCC:/volume1/docker/phpenv$ ./console
The "material" project of the php container.
dlaravel@a46c5e5cbec0:/var/www/html$ npm install

up to date, audited 755 packages in 2s

76 packages are looking for funding
run `npm fund` for details

found 0 vulnerabilities
dlaravel@a46c5e5cbec0:/var/www/html$

9. Basically, you can execute all docker-compose commands through ./consoe, how to execute the sub-commands of docker-compose, and carry the sub-commands of ./console.
docker-compsoe , you need to substitute the project name, multiple parameters of the container configuration file

 docker-compose -p material -f docker-compose.yml -f services/redis.yml -f services/ssh.yml ps

vs.
./console , the ./console command of phpenv, does not need to take parameters at all, and will automatically refer to the settings in .env to help you get everything done.

 ./console ps

The following is the screen during operation

 #./info其實就是cat .env,用來查看phpenv目錄上的.env設定
devin@CCC:/volume1/docker/phpenv$ ./info
DotEnv Settings
SERVICES="ssh redis"
PROJECT=material
APP_URL=http://127.0.0.1:1051
FOLDER=/volume1/docker/material
NETWORK_MOD=host
HTTP_PORT=1051
DB_PORT=1250
SSH_PORT=2051
USER_ID=1026
GROUP_ID=100
devin@CCC:/volume1/docker/phpenv$ docker-compose -p material -f docker-compose.yml -f services/redis.yml -f services/ssh.yml ps
Name Command State Ports
-------------------------------------------------- ----------------------------------------
material_php_1 docker-php-entrypoint sudo ... Up 9000/tcp
material_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp
material_ssh_1 docker-php-entrypoint sudo ... Up 0.0.0.0:2051->22/tcp, 9000/tcp
material_web_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:1051->80/tcp
devin@CCC:/volume1/docker/phpenv$ ./console ps
x86_64
http://127.0.0.1:1051
Name Command State Ports
-------------------------------------------------- ----------------------------------------
material_php_1 docker-php-entrypoint sudo ... Up 9000/tcp
material_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp
material_ssh_1 docker-php-entrypoint sudo ... Up 0.0.0.0:2051->22/tcp, 9000/tcp
material_web_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:1051->80/tcp
devin@CCC:/volume1/docker/phpenv$

Because of the service that supports SSH, basically what I am developing now is developed by connecting to the remote host through the Remote-SSH suite developed by VS Code Microsoft.
It is not a problem to change the Code no matter the ends of the earth.

10. Use the ./console exec db command to enter the database (this is exactly the same as the operation on Dlaravel)
Use it when you have a new Laravel project and want to connect to db to build a database
docker-compsoe exec db mysql can be changed to ./console exec db mysql

 devin@CCC:/volume1/docker/phpenv$ ./console ps
x86_64
http://127.0.0.1:1056
Name Command State Ports
-------------------------------------------------- -------------------------------------------------- ---------
ccc_db_1 docker-entrypoint.sh mysqld Up
ccc_php_1 docker-php-entrypoint sudo ... Up 9000/tcp
ccc_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp
ccc_ssh_1 docker-php-entrypoint sudo ... Up 0.0.0.0:2256->22/tcp, 0.0.0.0:3356->3306/tcp, 9000/tcp
ccc_web_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:1056->80/tcp
devin@CCC:/volume1/docker/phpenv$ ./console exec db mysql
x86_64
http://127.0.0.1:1056
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 7672
Server version: 10.5.5-MariaDB-1:10.5.5+maria~focal mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 MariaDB [(none)]>

Eleven, the built-in mysql client program, you can also issue mysql commands in the container.
So I made a backup of Laravel's php artisan console, which is quite convenient for small projects and small databases, and can quickly back up and restore the database.

 devin@CCC:/volume1/docker/phpenv$ ./console
The "ccc" project of the php container.
dlaravel@b7a4dfbe775a:/var/www/html$ php artisan | grep backup
db:backup backup MySQL database
db:restore backup data restoration
dlaravel@b7a4dfbe775a:/var/www/html$ php artisan db:backup
The backup has been completed and stored in: storage/app/backup/ccctc.sql
 dlaravel@b7a4dfbe775a:/var/www/html$

Now that I mention it, I will share my console program here

 

use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\Artisan;
#Database Backup
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
use Illuminate\Support\Facades\Storage;
/*
|------------------------------------------------- -------------------------
| Console Routes
|------------------------------------------------- -------------------------
|
| This file is where you may define all of your Closure based console
| commands. Each Closure is bound to a command instance allowing a
| simple approach to interacting with each command's IO methods.
|
*/

Artisan::command("db:backup", function(){
$database = env("DB_DATABASE");
$command = sprintf("mysqldump -uroot --default-character-set=utf8mb4 -h%s %s",env("DB_HOST"), env("DB_DATABASE"));
$process = new Process(explode(" ", $command));
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
$data = $process->getOutput();
Storage::disk('local')->put(sprintf("%s/%s.sql","backup",$database), $data);
$this->comment(sprintf("The backup has been completed and stored in: storage/app/backup/%s.sql",$database));

})->purpose('backup MySQL database');

Artisan::command("db:restore", function(){
if ($this->confirm('Do you want to import the backup data into the Mysql database?')) {
$database = env("DB_DATABASE");
$command = sprintf("mysql -uroot -h %s --default-character-set=utf8mb4 %s < %s/%s.sql",
env("DB_HOST"),
$database,
Storage::path("backup"),
$database);
$this->comment($command);
system($command);
}

})->purpose('backup data restoration');



Of course, D-Laravel is not dead yet, the base image used by phpenv is also from D-Laravel.
This is why phpenv builds a project image very quickly when using ./console build.
If you check under docker images, in the phpenv environment, each image and service is created along with the project name in .env.
The ccc below is the project name, and the bottom line is the service name

 devin@CCC:/volume1/docker/phpenv$ docker images|grep ccc
ccc_ssh latest 948e6ba97f2b 2 weeks ago 1.31GB
ccc_php latest df7295071ef1 2 weeks ago 1.16GB
ccc_nginx latest 9633827261ce 2 months ago 141MB
 devin@CCC:/volume1/docker/phpenv$

If you want to adjust and build your own D-Larvel image, the Dockerfile is in the D-Laravel repository.

https://github.com/DevinY/dlaravel/tree/master/dockerfiles/fpm

In phpenv, the image of dlaravel is used to extend and install more functions, such as npm instructions of nodejs, Chinese environment, etc.

https://github.com/DevinY/phpenv/tree/main/dockerfiles

12. Different from D-Laravel, phpenv uses supervisor to start the container. In the etc/supervisor directory of phpenv, rename it to .conf and start it. Below is the directory of phpenv and some sample files

 devin@CCC:/volume1/docker/phpenv$ cd etc/supervisor/
devin@CCC:/volume1/docker/phpenv/etc/supervisor$ pwd
/volume1/docker/phpenv/etc/supervisor
devin@CCC:/volume1/docker/phpenv/etc/supervisor$ ls
cron.conf.sample laravel.conf mailer.conf.sample queue.conf websockets.conf.sample
devin@CCC:/volume1/docker/phpenv/etc/supervisor$ cat queue.conf
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan queue:work --sleep=3 --tries=3
autostart=true
autorestart=true
user=dlaravel
numprocs=8

redirect_stderr=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
 devin@CCC:/volume1/docker/phpenv/etc/supervisor$

Is queue:work still available in the development environment? Just start the container if you want to start it, it is completely up to you.

 devin@CCC:/volume1/docker/phpenv$ ./console
The "ccc" project of the php container.
dlaravel@b7a4dfbe775a:/var/www/html$ sudo su -
root@b7a4dfbe775a:~# supervisorctl status
laravel-worker: laravel-worker_00 RUNNING pid 11, uptime 8:43:17
laravel-worker: laravel-worker_01 RUNNING pid 12, uptime 8:43:17
laravel-worker: laravel-worker_02 RUNNING pid 13, uptime 8:43:17
laravel-worker: laravel-worker_03 RUNNING pid 14, uptime 8:43:17
laravel-worker: laravel-worker_04 RUNNING pid 15, uptime 8:43:17
laravel-worker: laravel-worker_05 RUNNING pid 16, uptime 8:43:17
laravel-worker: laravel-worker_06 RUNNING pid 17, uptime 8:43:17
laravel-worker: laravel-worker_07 RUNNING pid 18, uptime 8:43:17
php-fpm RUNNING pid 10, uptime 8:43:17
 root@b7a4dfbe775a:~#

Thirteen, the redis-cli command has been installed directly, and it is not a problem to play redis in the command line.
14. Through the simple ./ports command, you can view all the main export ports, and sort them in a forward order, which is very convenient in adding or viewing environments.

 devin@CCC:/volume1/docker/phpenv$ ./ports
envs/tn_env:HTTP_PORT=1050
envs/material:HTTP_PORT=1051
envs/chat:HTTP_PORT=1052
envs/test:HTTP_PORT=1053
envs/demo:HTTP_PORT=1054
envs/phpenv:HTTP_PORT=1055
 envs/ccc:HTTP_PORT=1056


Finally, phpenv is an out-of-the-box Project. If you don’t believe me, try it out. :p

Tags: php docker dlaravel

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


docker

Install docker-compose on ubuntu

Briefly introduce how I installed docker-compose on ubuntu. Quite easy.

openai,flax,docker

Use the container to run OpenAI's official teaching Python (Flax)

If you log in to the openai website, you can see the official teaching instructions herehttps://beta.openai.com/docs/quickstart/build-your-applicationAPI的KEY的申請, there is a button at the bottom of this page + Create new secret key This article teaches you to use Docker to run the official OpenAI execution environment.

dlaravel

Safe Update D-Laravel

How to upgrade the current D-Laravel. If your current use environment is very smooth and you have not encountered any problems, you don't need to update it. If you want to use the latest version, and bug fixes, etc., please read this article carefully, In order to ensure a smooth upgrade, the following is the recommended way to upgrade D-Laravel.