PHP development environment based on Docker

A solution that will allow you to create a universal PHP development environment on your local computer   in  30 - 40 minutes .

Why Docker?

  • Docker is not a VM system, it does not model the hardware of a computer. Using Docker you will get minimal resource consumption. Docker containers interact directly with your computer's kernel (or host kernel), but they also isolate the program at the process level.

  • Fast deployment speed. You can use pre-built docker images that install and run in seconds.

  • Applications inside docker containers can be run on any machine with Docker installed, with the same environment.

  • Possibility of simple segregation of user data and container services. If you break or delete the docker container, then the data will not be lost, since it will not belong to the container. The container serves only as a service, and does not store data that cannot be lost between runs.

  • You can add new containers very quickly, change their configuration, run different versions of databases on the same machine.

Requirements

  • Git.

  • Docker engine 19.x and later.

Development environment capabilities and features 

  • Several PHP versions  - 7.3 and 7.1  with a set of the most popular extensions.

  • The ability to use different PHP versions for web projects  .

  • Supervisor Process Monitor ready to  go .

  • Pre-configured Nginx web server .

  • Databases:  MySQL 5.7MySQL 8PostgreSQL  (latest),  MongoDB 4.2Redis (latest).

  •  .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

How to deploy a MongoDB dump? 

  • Copy the dump files to the mongo / dump directory  .

  • Enter the mongo container  :

docker exec -it mongo sh

Run the following command to deploy the database_name dump  :

mongorestore -d database_name /dump/databases/database_nam

Git project repository: https://github.com/drandin/docker-php-workspace




All Articles