Environment

  • centos 7
    • 2-core 8g
  • docker 1.13.1
  • docker-compose 1.24.1

Versions of each container

  • php:7.3-fpm-buster
  • mysql:8.0
  • nginx:1.21
  • redis:6.2
  • frp
    • using debian:10-slim as the base image build
    • The specific version is 0.38.0
    • frp’s main role is to act as a proxy for xdebug
  • elasticsearch:7.9

The magento version is 2.3.4

The following section assumes that docker and docker-compose are already installed.

This is intended as a development environment, so the code is not packaged into the image.

For ease of presentation, we will assume that the root directory of magento is /var/html/www.

Configuration files

  1. Create a new docker folder in the root directory of magento, and a new vm folder in docker.

    1
    
    mkdir -p docker/vm
    
  2. Create the following file in the vm folder

    • Dockerfile

    • docker-compose.yml

    • nginx folder

    • frps folder

      1
      2
      
      touch Dockerfile docker-compose.yml
      mkdir nginx frps
      
  3. Write the following in the /var/html/www/docker/vm/Dockerfile file.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    
    FROM php:7.3-fpm-buster
    
    RUN curl -sS -o composer.phar https://getcomposer.org/composer-1.phar && \
        chmod 0755 composer.phar && \
        mv composer.phar /usr/local/bin/composer
    
    RUN apt-get update && \
        requirements="git cron rsync vim libpng-dev libmcrypt-dev libmcrypt4 libcurl3-dev libfreetype6 libjpeg62-turbo libjpeg62-turbo-dev libpng-dev libfreetype6-dev libicu-dev libxslt1-dev libzip-dev  unzip libc-client-dev libkrb5-dev" && \
        apt-get install -y $requirements && \
        rm -rf /var/lib/apt/lists/*
    
    RUN docker-php-ext-install -j$(nproc) pdo_mysql && \
        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 -j$(nproc) mbstring && \
        docker-php-ext-install -j$(nproc) zip && \
        docker-php-ext-install -j$(nproc) intl && \
        docker-php-ext-install -j$(nproc) xsl && \
        docker-php-ext-install -j$(nproc) soap && \
        docker-php-ext-install -j$(nproc) bcmath && \
        docker-php-ext-install -j$(nproc) opcache && \
        docker-php-ext-configure imap --with-kerberos --with-imap-ssl && \
        docker-php-ext-install -j$(nproc) imap && \
        docker-php-ext-install -j$(nproc) sockets && \
        requirementsToRemove="libpng-dev libmcrypt-dev libcurl3-dev libpng-dev libfreetype6-dev libjpeg62-turbo-dev" && \
        apt-get purge --auto-remove -y $requirementsToRemove && \
        printf "\n\n\n" | pecl install redis-4.0.0 && \
        docker-php-ext-enable redis
    
    RUN pecl install xdebug-2.9.8 && \
        docker-php-ext-enable xdebug && \
        echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
        echo "xdebug.remote_host=frps" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
        echo "xdebug.remote_port=9003" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
        echo "xdebug.show_local_vars=on" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
        echo "xdebug.default_enable=on" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
        echo "xdebug.idekey=vscode" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
    
    RUN cp /usr/local/etc/php/php.ini-development /usr/local/etc/php/php.ini && \
        sed -i 's/short_open_tag = Off/short_open_tag = On/g' /usr/local/etc/php/php.ini && \
        sed -i 's/memory_limit = 128M/memory_limit = 4G/g' /usr/local/etc/php/php.ini && \
        sed -i 's/;max_input_vars = 1000/max_input_vars = 10000/g' /usr/local/etc/php/php.ini && \
        sed -i 's/;date.timezone=/date.timezone = "Asia\/Hong_Kong"/g' /usr/local/etc/php/php.ini && \
        sed -i 's/;opcache.enable=1/opcache.enable=1/g' /usr/local/etc/php/php.ini && \
        sed -i 's/;opcache.enable_cli=0/opcache.enable_cli=1/g' /usr/local/etc/php/php.ini && \
        sed -i 's/max_execution_time = 30/max_execution_time = 900/g' /usr/local/etc/php/php.ini && \
        sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 64M/g' /usr/local/etc/php/php.ini && \
        sed -i 's/post_max_size = 8M/post_max_size = 128M/g' /usr/local/etc/php/php.ini && \
        sed -i 's/max_input_time = 60/max_input_time = 300/g' /usr/local/etc/php/php.ini && \
        sed -i 's/pm = dynamic/pm = static/g' /usr/local/etc/php-fpm.d/www.conf && \
        sed -i 's/pm.max_children = 5/pm.max_children = 32/g' /usr/local/etc/php-fpm.d/www.conf && \
        sed -i 's/;pm.max_requests = 500/pm.max_requests = 500/g' /usr/local/etc/php-fpm.d/www.conf
    
    WORKDIR /var/www/html
    RUN chown root:root -R /var/www && \
        chmod 777 -R /var/www
    
  4. Execute this command in the /var/html/www/docker/vm/nginx folder, mainly to generate the https certificate.

    1
    
    openssl req -newkey rsa:4096 -nodes -keyout rsa_private_key.pem -x509 -days 365 -out domain.crt -subj "/C=CN/ST=State/L=City/O=Ltd/OU=Section/CN=test.magento2.com"
    
  5. Create a new file called nginx.conf in the /var/html/www/docker/vm/nginx folder and write the following.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    
    user  root;
    worker_processes  auto;
    
    error_log  /var/log/nginx/error.log notice;
    pid        /var/run/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;
    
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';
    
        access_log  /var/log/nginx/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        keepalive_timeout  65;
    
        #gzip  on;
    
        include /etc/nginx/conf.d/default.conf;
    
        upstream fastcgi_backend {
            server  php:9000;
        }
        server {
            listen 80;
    
            listen 443 ssl http2;
            ssl_certificate  ./cert/domain.crt;
            ssl_certificate_key ./cert/rsa_private_key.pem;
    
            access_log  /var/log/nginx/magento.access.log;
            error_log  /var/log/nginx/magento.error.log;
    
            server_name test.magento2.com; # 这里的域名要和证书的域名一致
            set $MAGE_ROOT /var/www/html; # 这里是 magento 的根目录
            set $MAGE_DEBUG_SHOW_ARGS 1;
            include /var/www/html/nginx.conf.sample; # 这里是 magento 的根目录里的 nginx.conf.sample
            fastcgi_buffers 16 16k;
            fastcgi_buffer_size 32k;
            proxy_buffer_size 128k;
            proxy_buffers 4 256k;
            proxy_busy_buffers_size 256k;
        }
    }
    
  6. Create a new file called frps.ini in the /var/html/www/docker/vm/frps folder and write the following

    1
    2
    3
    
    [common]
    bind_port=8999
    token=sdfjsldfwqefwnjnvosadfp
    
  7. Create a new file called Dockerfile in the /var/html/www/docker/vm/frps folder and write the following.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    
    FROM debian:10-slim
    
    ARG FRP_VERSION=0.38.0
    
    RUN apt update \
        && apt install -y wget
    
    WORKDIR /tmp
    RUN set -x \
        && wget https://github.com/fatedier/frp/releases/download/v${FRP_VERSION}/frp_${FRP_VERSION}_linux_amd64.tar.gz \
        && tar -zxf frp_${FRP_VERSION}_linux_amd64.tar.gz \
        && mv frp_${FRP_VERSION}_linux_amd64 /var/frp \
        && mkdir -p /var/frp/conf \
        && apt remove -y wget \
        && apt autoremove -y \
        && rm -rf /var/lib/apt/lists/*
    
    COPY frps.ini /var/frp/conf/frps.ini
    
    WORKDIR /var/frp
    ENTRYPOINT ./frps -c ./conf/frps.ini
    
  8. Write the following in the /var/html/www/docker/vm/docker-compose.yml file

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    
    version: '3.2'
    services:
    php:
        build:
        context: . # 相对于 docker-compose.yml 的路径
        dockerfile: Dockerfile # 相对于 context 的路径
        restart: always
        container_name: php
        depends_on:
            - mysqld
            - redis
            - frps
        volumes:
            - ./../../:/var/www/html # 相对于 docker-compose.yml 的路径
        command: ["php-fpm"]
        environment:
            TZ: Asia/Shanghai
    
    nginx:
        image: nginx:1.21
        restart: always
        container_name: nginx
        ports:
            - 80:80
            - 443:443
        depends_on:
            - php
        volumes:
            - ./../../:/var/www/html:ro
            - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
            - ./nginx/domain.crt:/etc/nginx/cert/domain.crt:ro
            - ./nginx/rsa_private_key.pem:/etc/nginx/cert/rsa_private_key.pem:ro
        user: root
        environment:
            TZ: Asia/Shanghai
    
    mysqld:
        image: mysql:5.7
        restart: always
        container_name: mysqld
        ports:
            - 3306:3306
        volumes:
            - ~/mysql-data:/var/lib/mysql
        environment:
        MYSQL_DATABASE: magento234
        MYSQL_ROOT_PASSWORD: 123456
            TZ: Asia/Shanghai
    
    redis:
        image: redis:6.2
        restart: always
        container_name: redis
        ports:
            - 6379:6379
        environment:
            TZ: Asia/Shanghai
    
    frps:
        build:
        context: ./frps
        dockerfile: Dockerfile
        container_name: frps
        ports:
            - 8999:8999
            - 9003:9003
    
    elasticsearch:
        image: elasticsearch:7.9.3
        restart: always
        ports:
            - 9200:9200
            - 9300:9300
        environment:
            discovery.type: single-node
    

Build and run

  1. To build the image, run this command from the root of magento.

    1
    
    docker-compose -f ./docker/vm/docker-compose.yml build
    
  2. To run the container, run this command from the root of magento.

    1
    2
    3
    
    docker-compose -f ./docker/vm/docker-compose.yml up -d
    or
    docker-compose -f ./docker/vm/docker-compose.yml up
    
  3. Stop the container and run this command from the root of magento, or ctrl-c if it’s not running in the background.

    1
    
    docker-compose -f ./docker/vm/docker-compose.yml stop
    
  4. Uninstall the container and run this command from the root of magento.

    1
    
    docker-compose -f ./docker/vm/docker-compose.yml down
    

Cautions and references

If you change the Dockerfile, you will have to rebuild the image.

Pay attention to the permissions of the directory where the container is mounted. Both fpm and nginx need permission to access the magento directory.

After the container is started, you need to go into the fpm container and run the magento build command, assuming the fpm container name is php.

1
2
docker ps -a
docker exec -it php /bin/bash

If you have an existing magento project, import the database and change the fields in the core_config_data table before running the build command.

Remember to modify the hosts file.

One-click deployment script

Assuming that docker and docker-compose are installed and the corresponding ports are not occupied

  • 2.3
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
#!/bin/bash

# 假设已经安装好 docker 和 docker-compose

# 下载源码

for ((i=0;i<100;i++))
do
    curl -L -o magento2.tar.gz --connect-timeout 120 --max-time 3600 --retry 100 --retry-delay 5 https://github.com/magento/magento2/archive/refs/tags/2.3.4.tar.gz
    if [ $? -eq 0 ]; then
        break
    fi
done
if [ $? != 0 ]; then
    echo "can not download magento source code"
    exit 1;
fi

# 解压源码

tar -zxf magento2.tar.gz
mv magento2-2.3.4 magento234

# 在新建对应的文件

cd magento234
rm -r .github
mkdir -p docker/vm
cd docker/vm
touch Dockerfile docker-compose.yml
mkdir nginx frps

cd nginx
openssl req -newkey rsa:4096 -nodes -keyout rsa_private_key.pem -x509 -days 365 -out domain.crt -subj "/C=CN/ST=State/L=City/O=Ltd/OU=Section/CN=test.magento2.com"

cat > nginx.conf << EOF
user  root;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '\$remote_addr - \$remote_user [\$time_local] "\$request" '
                    '\$status $body_bytes_sent "\$http_referer" '
                    '"\$http_user_agent" "\$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/default.conf;

    upstream fastcgi_backend {
        server  php:9000;
    }
    server {
        listen 80;

        listen 443 ssl http2;
        ssl_certificate  ./cert/domain.crt;
        ssl_certificate_key ./cert/rsa_private_key.pem;

        access_log  /var/log/nginx/magento.access.log;
        error_log  /var/log/nginx/magento.error.log;

        server_name test.magento2.com; # 这里的域名要和证书的域名一致
        set \$MAGE_ROOT /var/www/html; # 这里是 magento 的根目录
        set \$MAGE_DEBUG_SHOW_ARGS 1;
        include /var/www/html/nginx.conf.sample; # 这里是 magento 的根目录里的 nginx.conf.sample
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        proxy_buffer_size 128k;
        proxy_buffers 4 256k;
        proxy_busy_buffers_size 256k;
    }
}
EOF

cd ..
cd frps
cat > frps.ini << EOF
[common]
bind_port=8999
token=sdfjsldfwqefwnjnvosadfp
EOF

cat > Dockerfile << EOF
FROM debian:10-slim

ARG FRP_VERSION=0.38.0

RUN apt update \
    && apt install -y wget

WORKDIR /tmp
RUN set -x \
    && wget https://github.com/fatedier/frp/releases/download/v\${FRP_VERSION}/frp_\${FRP_VERSION}_linux_amd64.tar.gz \
    && tar -zxf frp_\${FRP_VERSION}_linux_amd64.tar.gz \
    && mv frp_\${FRP_VERSION}_linux_amd64 /var/frp \
    && mkdir -p /var/frp/conf \
    && apt remove -y wget \
    && apt autoremove -y \
    && rm -rf /var/lib/apt/lists/*

COPY frps.ini /var/frp/conf/frps.ini

WORKDIR /var/frp
ENTRYPOINT ./frps -c ./conf/frps.ini
EOF

cd ..
cat > Dockerfile << EOF
FROM php:7.3-fpm-buster

RUN curl -sS -o composer.phar https://getcomposer.org/composer-1.phar && \
    chmod 0755 composer.phar && \
    mv composer.phar /usr/local/bin/composer

RUN apt-get update && \
    requirements="git cron rsync vim libpng-dev libmcrypt-dev libmcrypt4 libcurl3-dev libfreetype6 libjpeg62-turbo libjpeg62-turbo-dev libpng-dev libfreetype6-dev libicu-dev libxslt1-dev libzip-dev unzip libc-client-dev libkrb5-dev" && \
    apt-get install -y \$requirements && \
    rm -rf /var/lib/apt/lists/*

RUN docker-php-ext-install -j\$(nproc) pdo_mysql && \
    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 -j\$(nproc) mbstring && \
    docker-php-ext-install -j\$(nproc) zip && \
    docker-php-ext-install -j\$(nproc) intl && \
    docker-php-ext-install -j\$(nproc) xsl && \
    docker-php-ext-install -j\$(nproc) soap && \
    docker-php-ext-install -j\$(nproc) bcmath && \
    docker-php-ext-install -j\$(nproc) opcache && \
    docker-php-ext-configure imap --with-kerberos --with-imap-ssl && \
    docker-php-ext-install -j\$(nproc) imap && \
    docker-php-ext-install -j\$(nproc) sockets && \
    requirementsToRemove="libpng-dev libmcrypt-dev libcurl3-dev libpng-dev libfreetype6-dev libjpeg62-turbo-dev" && \
    apt-get purge --auto-remove -y \$requirementsToRemove && \
    printf "\n\n\n" | pecl install redis-4.0.0 && \
    docker-php-ext-enable redis

RUN pecl install xdebug-2.9.8 && \
    docker-php-ext-enable xdebug && \
    echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
    echo "xdebug.remote_host=frps" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
    echo "xdebug.remote_port=9003" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
    echo "xdebug.show_local_vars=on" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
    echo "xdebug.default_enable=on" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
    echo "xdebug.idekey=vscode" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

RUN cp /usr/local/etc/php/php.ini-development /usr/local/etc/php/php.ini && \
    sed -i 's/short_open_tag = Off/short_open_tag = On/g' /usr/local/etc/php/php.ini && \
    sed -i 's/memory_limit = 128M/memory_limit = 4G/g' /usr/local/etc/php/php.ini && \
    sed -i 's/;max_input_vars = 1000/max_input_vars = 10000/g' /usr/local/etc/php/php.ini && \
    sed -i 's/;date.timezone=/date.timezone = "Asia\/Hong_Kong"/g' /usr/local/etc/php/php.ini && \
    sed -i 's/;opcache.enable=1/opcache.enable=1/g' /usr/local/etc/php/php.ini && \
    sed -i 's/;opcache.enable_cli=0/opcache.enable_cli=1/g' /usr/local/etc/php/php.ini && \
    sed -i 's/max_execution_time = 30/max_execution_time = 900/g' /usr/local/etc/php/php.ini && \
    sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 64M/g' /usr/local/etc/php/php.ini && \
    sed -i 's/post_max_size = 8M/post_max_size = 128M/g' /usr/local/etc/php/php.ini && \
    sed -i 's/max_input_time = 60/max_input_time = 300/g' /usr/local/etc/php/php.ini && \
    sed -i 's/pm = dynamic/pm = static/g' /usr/local/etc/php-fpm.d/www.conf && \
    sed -i 's/pm.max_children = 5/pm.max_children = 32/g' /usr/local/etc/php-fpm.d/www.conf && \
    sed -i 's/;pm.max_requests = 500/pm.max_requests = 500/g' /usr/local/etc/php-fpm.d/www.conf

WORKDIR /var/www/html
RUN chown root:root -R /var/www && \
    chmod 777 -R /var/www
EOF

cat > docker-compose.yml << EOF
version: '3.2'
services:
  php:
    build:
      context: . # 相对于 docker-compose.yml 的路径
      dockerfile: Dockerfile # 相对于 context 的路径
    restart: always
    container_name: php
    depends_on:
      - mysqld
      - redis
      - frps
    volumes:
      - ./../../:/var/www/html # 相对于 docker-compose.yml 的路径
    user: root
    command: ["php-fpm"]
    environment:
      TZ: Asia/Shanghai

  nginx:
    image: nginx:1.21
    restart: always
    container_name: nginx
    ports:
      - 80:80
      - 443:443
    depends_on:
      - php
    volumes:
      - ./../../:/var/www/html:ro
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./nginx/domain.crt:/etc/nginx/cert/domain.crt:ro
      - ./nginx/rsa_private_key.pem:/etc/nginx/cert/rsa_private_key.pem:ro
    user: root
    environment:
      TZ: Asia/Shanghai

  mysqld:
    image: mysql:5.7
    restart: always
    container_name: mysqld
    ports:
      - 3307:3306
    volumes:
      - ~/mysql-data:/var/lib/mysql
    environment:
      MYSQL_DATABASE: magento234
      MYSQL_ROOT_PASSWORD: 123456
      TZ: Asia/Shanghai

  redis:
    image: redis:6.2
    restart: always
    container_name: redis
    ports:
      - 6379:6379
    environment:
      TZ: Asia/Shanghai

  frps:
    build:
      context: ./frps
      dockerfile: Dockerfile
    container_name: frps
    ports:
      - 8999:8999
      - 9003:9003

  # elasticsearch:
  #   image: elasticsearch:7.9.3
  #   restart: always
  #   container_name: elasticsearch
  #   ports:
  #     - 9200:9200
  #     - 9300:9300
  #   environment:
  #     discovery.type: single-node
EOF

cd ..
cd ..
pwd

# 运行构建命令

docker-compose -f ./docker/vm/docker-compose.yml build

docker-compose -f ./docker/vm/docker-compose.yml up -d

sleep 10

cat > init.sh << EOF
#!/bin/bash

chmod 777 -R /var/www

composer install

php bin/magento setup:install \
    --base-url=https://localhost-magento/ \
    --db-host=mysqld \
    --db-name=magento234 \
    --db-user=root \
    --db-password=123456 \
    --cleanup-database \
    --admin-firstname=admin \
    --admin-lastname=admin \
    --admin-email=admin@admin.com \
    --admin-user=admin \
    --admin-password=admin123 \
    --backend-frontname=admin \
    --language=en_US \
    --currency=USD \
    --timezone=Asia/Shanghai \
    --use-rewrites=1

php bin/magento setup:config:set --cache-backend=redis --cache-backend-redis-server=redis --cache-backend-redis-db=0
php bin/magento setup:config:set --page-cache=redis --page-cache-redis-server=redis --page-cache-redis-db=1

php bin/magento deploy:mode:set developer
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento indexer:reindex
php bin/magento cache:flush

chmod 777 -R /var/www

EOF

chmod 777 init.sh
docker cp `pwd`/init.sh php:/var/www/html/init.sh
sleep 1
docker exec php /var/www/html/init.sh

# curl http://doututuan.com/test.sh|bash
# vim install23.sh
# chmod 755 install23.sh
# ./install23.sh
# time ./install23.sh
# docker-compose -f ./magento234/docker/vm/docker-compose.yml stop
# rm magento2.tar.gz && rm -r magento234 && rm -r mysql-data
# docker rm `docker ps -a | grep Exited | awk '{print $1}'`
2.4
#!/bin/bash

# 假设已经安装好 docker 和 docker-compose

# 下载源码

for ((i=0;i<100;i++))
do
    curl -L -o magento2.tar.gz --connect-timeout 120 --max-time 3600 --retry 100 --retry-delay 5 https://github.com/magento/magento2/archive/refs/tags/2.4.2.tar.gz
    if [ $? -eq 0 ]; then
        break
    fi
done
if [ $? != 0 ]; then
    echo "can not download magento source code"
    exit 1;
fi

# 解压源码

tar -zxf magento2.tar.gz
mv magento2-2.4.2 magento242

# 在新建对应的文件

cd magento242
rm -r .github
mkdir -p docker/vm
cd docker/vm
touch Dockerfile docker-compose.yml
mkdir nginx frps

cd nginx
openssl req -newkey rsa:4096 -nodes -keyout rsa_private_key.pem -x509 -days 365 -out domain.crt -subj "/C=CN/ST=State/L=City/O=Ltd/OU=Section/CN=test.magento2.com"

cat > nginx.conf << EOF
user  root;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '\$remote_addr - \$remote_user [\$time_local] "\$request" '
                    '\$status $body_bytes_sent "\$http_referer" '
                    '"\$http_user_agent" "\$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/default.conf;

    upstream fastcgi_backend {
        server  php:9000;
    }
    server {
        listen 80;

        listen 443 ssl http2;
        ssl_certificate  ./cert/domain.crt;
        ssl_certificate_key ./cert/rsa_private_key.pem;

        access_log  /var/log/nginx/magento.access.log;
        error_log  /var/log/nginx/magento.error.log;

        server_name test.magento2.com; # 这里的域名要和证书的域名一致
        set \$MAGE_ROOT /var/www/html; # 这里是 magento 的根目录
        set \$MAGE_DEBUG_SHOW_ARGS 1;
        include /var/www/html/nginx.conf.sample; # 这里是 magento 的根目录里的 nginx.conf.sample
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        proxy_buffer_size 128k;
        proxy_buffers 4 256k;
        proxy_busy_buffers_size 256k;
    }
}
EOF

cd ..
cd frps
cat > frps.ini << EOF
[common]
bind_port=8999
token=sdfjsldfwqefwnjnvosadfp
EOF

cat > Dockerfile << EOF
FROM debian:10-slim

ARG FRP_VERSION=0.38.0

RUN apt update \
    && apt install -y wget

WORKDIR /tmp
RUN set -x \
    && wget https://github.com/fatedier/frp/releases/download/v\${FRP_VERSION}/frp_\${FRP_VERSION}_linux_amd64.tar.gz \
    && tar -zxf frp_\${FRP_VERSION}_linux_amd64.tar.gz \
    && mv frp_\${FRP_VERSION}_linux_amd64 /var/frp \
    && mkdir -p /var/frp/conf \
    && apt remove -y wget \
    && apt autoremove -y \
    && rm -rf /var/lib/apt/lists/*

COPY frps.ini /var/frp/conf/frps.ini

WORKDIR /var/frp
ENTRYPOINT ./frps -c ./conf/frps.ini
EOF

cd ..
cat > Dockerfile << EOF
FROM php:7.4-fpm-buster

RUN curl -sS -o composer.phar https://getcomposer.org/composer.phar && \
    chmod 0755 composer.phar && \
    mv composer.phar /usr/local/bin/composer

RUN apt-get update && \
    requirements="git cron rsync vim libpng-dev libmcrypt-dev libmcrypt4 libcurl3-dev libfreetype6 libjpeg62-turbo libjpeg62-turbo-dev libpng-dev libfreetype6-dev libicu-dev libxslt1-dev libzip-dev unzip libc-client-dev libkrb5-dev libonig-dev" && \
    apt-get install -y \$requirements && \
    rm -rf /var/lib/apt/lists/*

RUN docker-php-ext-install -j\$(nproc) pdo_mysql && \
    docker-php-ext-configure gd --with-freetype --with-jpeg && \
    docker-php-ext-install -j\$(nproc) gd && \
    docker-php-ext-install -j\$(nproc) mbstring && \
    docker-php-ext-install -j\$(nproc) zip && \
    docker-php-ext-install -j\$(nproc) intl && \
    docker-php-ext-install -j\$(nproc) xsl && \
    docker-php-ext-install -j\$(nproc) soap && \
    docker-php-ext-install -j\$(nproc) bcmath && \
    docker-php-ext-install -j\$(nproc) opcache && \
    docker-php-ext-configure imap --with-kerberos --with-imap-ssl && \
    docker-php-ext-install -j\$(nproc) imap && \
    docker-php-ext-install -j\$(nproc) sockets && \
    requirementsToRemove="libpng-dev libmcrypt-dev libcurl3-dev libpng-dev libfreetype6-dev libjpeg62-turbo-dev" && \
    apt-get purge --auto-remove -y \$requirementsToRemove && \
    printf "\n\n\n" | pecl install redis-4.0.0 && \
    docker-php-ext-enable redis

RUN apt-get -y install libonig-dev && \
    pecl install xdebug-2.9.8 && \
    docker-php-ext-enable xdebug && \
    echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
    echo "xdebug.remote_host=frps" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
    echo "xdebug.remote_port=9003" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
    echo "xdebug.show_local_vars=on" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
    echo "xdebug.default_enable=on" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && \
    echo "xdebug.idekey=vscode" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

RUN cp /usr/local/etc/php/php.ini-development /usr/local/etc/php/php.ini && \
    sed -i 's/short_open_tag = Off/short_open_tag = On/g' /usr/local/etc/php/php.ini && \
    sed -i 's/memory_limit = 128M/memory_limit = 4G/g' /usr/local/etc/php/php.ini && \
    sed -i 's/;max_input_vars = 1000/max_input_vars = 10000/g' /usr/local/etc/php/php.ini && \
    sed -i 's/;date.timezone=/date.timezone = "Asia\/Hong_Kong"/g' /usr/local/etc/php/php.ini && \
    sed -i 's/;opcache.enable=1/opcache.enable=1/g' /usr/local/etc/php/php.ini && \
    sed -i 's/;opcache.enable_cli=0/opcache.enable_cli=1/g' /usr/local/etc/php/php.ini && \
    sed -i 's/max_execution_time = 30/max_execution_time = 900/g' /usr/local/etc/php/php.ini && \
    sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 64M/g' /usr/local/etc/php/php.ini && \
    sed -i 's/post_max_size = 8M/post_max_size = 128M/g' /usr/local/etc/php/php.ini && \
    sed -i 's/max_input_time = 60/max_input_time = 300/g' /usr/local/etc/php/php.ini && \
    sed -i 's/pm = dynamic/pm = static/g' /usr/local/etc/php-fpm.d/www.conf && \
    sed -i 's/pm.max_children = 5/pm.max_children = 32/g' /usr/local/etc/php-fpm.d/www.conf && \
    sed -i 's/;pm.max_requests = 500/pm.max_requests = 500/g' /usr/local/etc/php-fpm.d/www.conf

WORKDIR /var/www/html
RUN chown root:root -R /var/www && \
    chmod 777 -R /var/www
EOF

cat > docker-compose.yml << EOF
version: '3.2'
services:
  php:
    build:
      context: . # 相对于 docker-compose.yml 的路径
      dockerfile: Dockerfile # 相对于 context 的路径
    restart: always
    container_name: php
    depends_on:
      - mysqld
      - redis
      - frps
    volumes:
      - ./../../:/var/www/html # 相对于 docker-compose.yml 的路径
    user: root
    command: ["php-fpm"]
    environment:
      TZ: Asia/Shanghai

  nginx:
    image: nginx:1.21
    restart: always
    container_name: nginx
    ports:
      - 80:80
      - 443:443
    depends_on:
      - php
    volumes:
      - ./../../:/var/www/html:ro
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./nginx/domain.crt:/etc/nginx/cert/domain.crt:ro
      - ./nginx/rsa_private_key.pem:/etc/nginx/cert/rsa_private_key.pem:ro
    user: root
    environment:
      TZ: Asia/Shanghai

  mysqld:
    image: mysql:8.0
    restart: always
    container_name: mysqld
    ports:
      - 3307:3306
    volumes:
      - ~/mysql-data:/var/lib/mysql
    environment:
      MYSQL_DATABASE: magento242
      MYSQL_ROOT_PASSWORD: 123456
      TZ: Asia/Shanghai

  redis:
    image: redis:6.2
    restart: always
    container_name: redis
    ports:
      - 6379:6379
    environment:
      TZ: Asia/Shanghai

  frps:
    build:
      context: ./frps
      dockerfile: Dockerfile
    container_name: frps
    ports:
      - 8999:8999
      - 9003:9003

  elasticsearch:
    image: elasticsearch:7.9.3
    restart: always
    container_name: elasticsearch
    ports:
      - 9200:9200
      - 9300:9300
    environment:
      discovery.type: single-node
EOF

cd ..
cd ..
pwd

# 运行构建命令

docker-compose -f ./docker/vm/docker-compose.yml build

docker-compose -f ./docker/vm/docker-compose.yml up -d

sleep 10

cat > init.sh << EOF
#!/bin/bash

chmod 777 -R /var/www

composer install

php bin/magento setup:install \
    --base-url=https://localhost-magento/ \
    --db-host=mysqld \
    --db-name=magento242 \
    --db-user=root \
    --db-password=123456 \
    --cleanup-database \
    --admin-firstname=admin \
    --admin-lastname=admin \
    --admin-email=admin@admin.com \
    --admin-user=admin \
    --admin-password=admin123 \
    --backend-frontname=admin \
    --language=en_US \
    --currency=USD \
    --timezone=Asia/Shanghai \
    --use-rewrites=1 \
    --search-engine=elasticsearch7 \
    --elasticsearch-host=elasticsearch \
    --elasticsearch-port=9200 \
    --elasticsearch-index-prefix=magento2

php bin/magento setup:config:set --cache-backend=redis --cache-backend-redis-server=redis --cache-backend-redis-db=0
php bin/magento setup:config:set --page-cache=redis --page-cache-redis-server=redis --page-cache-redis-db=1

php bin/magento deploy:mode:set developer
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento indexer:reindex
php bin/magento cache:flush

chmod 777 -R /var/www

EOF

chmod 777 init.sh
docker cp `pwd`/init.sh php:/var/www/html/init.sh
sleep 1
docker exec php /var/www/html/init.sh

# curl http://doututuan.com/test.sh|bash
# vim install24.sh
# chmod 755 install24.sh
# ./install24.sh
# time ./install24.sh
# docker-compose -f ./magento242/docker/vm/docker-compose.yml stop
# rm magento2.tar.gz && rm -r magento242 && rm -r mysql-data
# docker rm `docker ps -a | grep Exited | awk '{print $1}'`