PHP-Entwicklungsumgebung basierend auf Docker

Eine Lösung, mit der Sie  in  30 bis 40 Minuten eine universelle PHP- Entwicklungsumgebung auf Ihrem lokalen Computer erstellen können  .

Warum Docker?

  • Docker ist kein VM-System, es modelliert nicht die Hardware eines Computers. Mit Docker erhalten Sie einen minimalen Ressourcenverbrauch. Docker-Container interagieren direkt mit dem Kernel (oder Host-Kernel) Ihres Computers, isolieren das Programm jedoch auch auf Prozessebene.

  • Schnelle Bereitstellungsgeschwindigkeit. Sie können vorgefertigte Docker-Images verwenden, die in Sekunden installiert und ausgeführt werden.

  • Anwendungen in Docker-Containern können auf jedem Computer mit installiertem Docker in derselben Umgebung ausgeführt werden.

  • Möglichkeit der einfachen Trennung von Benutzerdaten und Containerdiensten. Wenn Sie den Docker-Container beschädigen oder löschen, gehen die Daten nicht verloren, da sie nicht zum Container gehören. Der Container dient nur als Dienst und speichert keine Daten, die zwischen den Starts nicht verloren gehen können.

  • Sie können sehr schnell neue Container hinzufügen, ihre Konfiguration ändern und verschiedene Versionen von Datenbanken auf demselben Computer ausführen.

Bedarf

  • Git.

  • Docker Engine 19.x und höher.

Funktionen und Funktionen der Entwicklungsumgebung 

  • Mehrere PHP- Versionen  - 7.3 und 7.1  mit einer Reihe der beliebtesten Erweiterungen.

  • Die Möglichkeit, verschiedene PHP- Versionen für Webprojekte zu verwenden  .

  • Supervisor Process Monitor einsatzbereit  .

  • Vorkonfigurierter Nginx -Webserver .

  • Datenbanken:  MySQL 5.7MySQL 8PostgreSQL  (aktuell),  MongoDB 4.2Redis (aktuell).

  •  .env.

  •  docker-compose.yml.

  •  docker-compose.yml.

  • docker- .

  •  Dockerfile   PHP.

  • docker-, .

, —  Docker .

https://github.com/drandin/docker-php-workspace

 

.

├── .env-example
├── .gitignore
├── .ssh
├── README.md
├── docker-compose.yml
├── mongo
├── mysql-5.7
├── mysql-8
├── nginx
├── php-ini
├── php-workers
├── php-workspace
├── postgres
├── projects
└── redis

.gitkeep. , Git.

.gitkeep — , , .

.env-example 

.

#  
WORKSPACE_TIMEZONE='Europe/Moscow'

# XDEBUG
DOCKER_PHP_ENABLE_XDEBUG='on'

#  Nginx
# ,   
#     
NGINX_PORT=80

#  Redis
# ,   
#     
REDIS_PORT=6379
#  Postgres
POSTGRES_DB=test
POSTGRES_USER=pg_user
POSTGRES_PASSWORD=secret
POSTGRES_PORT=54322

#    MySQL 8.x  MySQL 5.7.x
MYSQL_ROOT_PASSWORD=secret
MYSQL_DATABASE=test

#  MySQL 8.x
# ,   
#     
MYSQL_8_PORT=4308

#  MySQL 5.7.x
# ,   
#     
MYSQL_5_7_PORT=4307

#  MongoDB
# ,   
#     
MONGO_PORT=27017

#  PHP 7.3
#  ,    
PHP_7_3_PORT=9003

#  PHP 7.1
#  ,    
PHP_7_1_PORT=9001

.gitignore

, , ssh- . gitignore.

.ssh 

ssh-.

readme.md 

.

docker-compose.yml 

 YML,  Docker. web-.

mongo 

MongoDB.

├── configdb
│   └── mongo.conf
├── db
└── dump

mongo.conf —  MongoDB. , MongoDB .

db — MongoDB.

dump — .

mysql-5.7 

MySQL 5.7.

├── conf.d
│   └── config-file.cnf
├── data
├── dump
└── logs

config-file.cnf — . , MySQL 5.7 .

data — MySQL 5.7.

dump — .

logs — .

mysql-8 

MySQL 8.

├── conf.d
│   └── config-file.cnf
├── data
├── dump
└── logs

config-file.cnf — . , MySQL 8 .

data — MySQL 8.

dump — .

logs — .

nginx 

Nginx .

├── conf.d
│   ├── default.conf
│   └── vhost.conf
└── logs

default.conf — , .

vhost.conf — web-.

 vhost.conf :

server {
    listen 80;
    index index.php index.html;
    server_name project-1.localhost;
    error_log /var/log/nginx/project-1.error.log;
    access_log /var/log/nginx/project-1.access.log combined if=$loggable;
    root /var/www/project-1.ru;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php-7.3:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_script_name;
    }
}

server {
    listen 80;
    index index.php index.html;
    server_name project-2.localhost;
    error_log /var/log/nginx/project-2.error.log;
    access_log /var/log/nginx/project-2.access.log combined if=$loggable;
    root /var/www/project-2.ru;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php-7.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_script_name;
    }
}

  web- — project-1.localhost  project-2.localhost.

, docker-.

,  project-1.localhost :

fastcgi_pass php-7.3:9000;

php-7.3 — docker-,  9000 — . , docker-compose.yml.

php-ini 

PHP.

├── 7.1
│   └── php.ini
└── 7.3
    └── php.ini

 PHP — .

php-workers 

 Supervisor.

├── 7.1
│   └── supervisor.d
└── 7.3
    └── supervisor.d

PHP — .

php-workspace 

, , docker- PHP.

└── Dockerfile

Dockerfile — , , PHP.

postgres

 PostgreSQL.

├── .gitkeep
├── data
└── dump

data — PostgreSQL.

dump — .

projects 

web-.

, web-.

:

project-1.ru
project-2.ru 
...

projects php-7.1 php-7.3.

php-7.1 php-7.3, /var/www , projects .

redis 

key-value Redis.

├── conf
└── data

conf — .

data — , Redis .

docker- PHP 

,  php-7.x  php-workspace/Dockerfile.

, :

  • bash

  • htop

  • curl

  • Git

  • omposer

  • make

  • wget

  • NodeJS

  • Supervisor

  • npm

 

1.  .

git clone https://github.com/drandin/docker-php-workspa

, . .

2.  .env-example  .env

cp .env-example .env

,  .env. .

3. web- projects.

, ,  2 :

project-1.ru
project-2.ru

project-1.ru —  PHP 7.3,  project-2.ru —  PHP 7.1.

4.  Nginx.

 ./nginx/conf.d/.

5. ( ) web- .

web-  hosts  .

 hosts  web-  IP docker- Nginx.

 Mac  Linux   /etc/hosts.  Windows   C:\Windows\System32\drivers\etc\hosts.

, , :

127.0.0.1   project-1.localhost
127.0.0.1   project-2.localhost

, ,  Nginx, docker-,  127.0.0.1  web-  80.

.dev  . — .localhost  .test.

6[, ]  web-.

Web- http- .

docker- php-7.1 web- № X  web- № Y, docker- php-7.3. ,  /etc/hosts  .

 /etc/hosts.

—  extra_hosts  php-7.1  php-7.3  docker-compose.yml.

:

...  
  php-7.1:  
  ...
    extra_hosts:
      - 'project-1.localhost:IP_HOST_MACHINE'
      - 'project-2.localhost:IP_HOST_MACHINE'
  ...

IP_HOST_machine — IP , docker- .

 Mac, docker-  docker.for.mac.localhost.

 IP   Mac  , :

docker run -it alpine ping docker.for.mac.localhost

, - :

PING docker.for.mac.localhost (192.168.65.2): 56 data bytes
64 bytes from 192.168.65.2: seq=0 ttl=37 time=0.286 ms
64 bytes from 192.168.65.2: seq=1 ttl=37 time=0.504 ms
64 bytes from 192.168.65.2: seq=2 ttl=37 time=0.801 ms

, IP-,  extra_hosts  php-7.1  php-7.3  docker-compose.yml.

...  
  php-7.1:  
  ...
    extra_hosts:
      - 'project-1.localhost:192.168.65.2'
      - 'project-2.localhost:192.168.65.2'
  ...

8. .

, docker- php-7.1  php-7.3  :

MySQL 5.7

mysql-5.7

3308

MySQL 8

mysql-8

3308

PostgreSQL

postgres

5432

MongoDB

mongo

27017

Redis

redis

6379

web-.

:

  • — 127.0.0.1.

  • —  .env.

7. .

:

docker-compose build && docker-compose up -d

. 10 30 . . .

, . 

 Docker Dashboard  :

8. SSH-.

web- SSH-, ,  Composer  .

SSH- :

ssh-keygen -f ./.ssh/id_rsa -t rsa -b 2048 -C "your-name@example.com"

 your-name@example.com   Email.

 .ssh/   2  — .

9. docker-.

:

docker ps

, :

CONTAINER ID        IMAGE                          COMMAND                  CREATED             STATUS              PORTS                               NAMES
8d348959c475        docker-php-workspace_php-7.1   "docker-php-entrypoi…"   6 minuts ago        Up 54 seconds       0.0.0.0:9001->9000/tcp              php-7.1
a93399727ff6        docker-php-workspace_php-7.3   "docker-php-entrypoi…"   6 minuts ago        Up 53 seconds       0.0.0.0:9003->9000/tcp              php-7.3
5cd80ac95388        nginx:stable-alpine            "/docker-entrypoint.…"   6 minuts ago        Up 51 seconds       0.0.0.0:80->80/tcp                  nginx
70182bc9e44c        mysql:5.7                      "docker-entrypoint.s…"   6 minuts ago        Up 54 seconds       33060/tcp, 0.0.0.0:4307->3306/tcp   mysql-5.7
46f2766ec0b9        mysql:8.0.21                   "docker-entrypoint.s…"   6 minuts ago        Up 53 seconds       33060/tcp, 0.0.0.0:4308->3306/tcp   mysql-8
a59e7f4b3c61        mongo:4.2                      "docker-entrypoint.s…"   6 minuts ago        Up 54 seconds       0.0.0.0:27017->27017/tcp            mongo
eae8d62ac66e        postgres:alpine                "docker-entrypoint.s…"   6 minuts ago        Up 53 seconds       0.0.0.0:54322->5432/tcp             postgres
bba24e86778a        redis:latest                   "docker-entrypoint.s…"   6 minuts ago        Up 54 seconds       0.0.0.0:6379->6379/tcp              redis

10. web-.

web- ,  Composer  NPM, .

 php-7.1  php-7.3   Composer  NPM.

 php-7.1:

docker exec -it php-7.1 bash

php-7.3:

docker exec -it php-7.3 bash

web- .

,  Composer  :

composer install

 

.

docker-? 

:

docker exec -it container_name bash

container_name — .

, ? 

docker-compose down

docker ps -a

docker-? 

docker inspect containername

containername — .

PHP, php-7.3? 

 php-7.3 , :

docker exec -it php-7.3 php -m

:

docker rm -v $(docker ps -aq)

:

docker rm -v $(docker ps -q)

:

docker rm -v $(docker ps -aq -f status=exited)

MySQL, PostgreSQL MongoDB

web- , .

PostgreSQL? 

:

docker exec -i postgres psql --username user_name database_name < /path/to/dump/pgsql-backup.sql

postgres :

psql --username user_name database_name < /path/to/dump/pgsql-backup.sql

user_name — . postgres_USER (. .env).

database_name — . postgres_DB (. .env).

MySQL? 

.

1

, .

 mysql/conf.d/config-file.cnf   slow_query_log=0   long_query_time, 1000.

 gzip, :

gunzip databases-dump.sql.gz

, :

docker exec -i mysql mysql --user=root --password=secret --force < databases-dump.sql

— , .

MySQL  :

mysql: [Warning] Using a password on the command line interface can be insecure

 --force  MySQL, . , .

2

 Percona XtraBackup.

Percona XtraBackup — MySQL.

,  XtraBackup  : https://medium.com/@drandin/---mysql----xtrabackup-26bd3f843075

Wie stelle ich einen MongoDB-Dump bereit? 

  • Kopieren Sie die Dump-Dateien in das Verzeichnis  mongo / dump .

  • Betritt den Mongo- Container  :

docker exec -it mongo sh

Führen Sie den folgenden Befehl aus, um den Datenbanknamen- Dump bereitzustellen  :

mongorestore -d database_name /dump/databases/database_nam

Git-Projekt-Repository: https://github.com/drandin/docker-php-workspace




All Articles