explain
Write a docker of DNMP environment from scratch- compose.yml (since redis is more commonly used, it is also included). Schematic diagram of overall design:
In terms of network allocation, the containers that need direct communication belong to the same network, and containers that do not communicate directly belong to different networks. As shown in the above figure, nginx and php-fpm belong to network a, and php-fpm, redis and MySQL belong to network B. On data storage, the project code is stored in foldera on the host, and the data volume is created and mapped to nginx and php-fpm.
The directory is planned as follows:
├ - conf # 1. Configuration file directory
The directory of MySQL configuration file is the same below
│ │ my.cnf #The default configuration file prepared in advance, the same below
│ ├─nginx
│ │ │ nginx.conf
│ │ └─conf.d
│ │ default.conf
│ ├─php
│ │ │ php.ini
│ │ └─conf.d
│ ├─redis
│ │ redis.conf
│ └─supervisor
│ supervisord.conf
Database data and project code directory can be divided into mysql, PHP and other directories
The directory where dockerfiles are stored
│ Dockerfile.php73
Log directory can be divided into mysql, PHP, etc
_ - directory of certificates
docker- compose.yml The general structure of the document is as follows:
Next, we write the configuration, debug and solve the problems one by one.
Note: the host system is Ubuntu 18, and all commands are run with a non root account
Redis
to configure
redis:
image: redis:latest # Use latest image
volumes:
-. / data / redis: data directory
- ./conf/redis/ redis.conf :/etc/redis/ redis.conf #Configuration file
networks:
- mysql
ports:
- "6379:6379"
sysctls:
- net.core.somaxconn=1024
Command: > ා note how to write multiple commands
bash -c "echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
&& redis-server /etc/redis/redis.conf --appendonly yes"
Problems encountered
docker- compose.yml In the directory where the file is located, run docker compose up redis with the following error message:
-
Warning: the TCP backlog setting of 511 cannot be enforced because / proc / sys / net / core / somaxconn is set to the lower value of 128
solve: as shown in the above configuration, add:
sysctls: - net.core.somaxconn=1024
-
Warning overcommit_ memory is set to 0! Background save may fail under low memory condition. To fix this issue add ‘ vm.overcommit_ memory = 1’ to /etc/ sysctl.conf and then reboot or run the command ‘sysctl vm.overcommit_ memory=1’ for this to take effect.
solve: as shown in the configuration, add:
echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
This command is also set in the host. -
Warning you have transparent tiger pages (THP) support enabled in your kernel. This will create latency and memory usage issues with redis. To fix this issue run the command ‘echo never > / sys / kernel / mm / transparent_ hugepage/enabled’ as root, and add it to your /etc/ rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
solve: in the host terminal, run in sequence:
echo never > /sys/kernel/mm/transparent_hugepage/enabled echo never > /sys/kernel/mm/transparent_hugepage/defrag
MySQL
to configure
mysql:
image: mysql:latest # Use the latest version
volumes:
-. / data / MySQL / var / lib / MySQL ා data directory
- ./conf/mysql/ my.cnf :/etc/mysql/ my.cnf #Configuration file
networks:
- mysql
ports:
- "3306:3306"
environment:
- MYSQL_ ROOT_ Password = root # password settings, pay attention to change to your own password
#Set password encryption driver. For reasons, see: https://learnku.com/articles/34823
command: --default-authentication-plugin=mysql_native_password
Problems encountered
-
mysqld: Can’t create/write to file ‘/var/lib/mysql/is_writable’ (Errcode: 13 – Permission denied)
solve: the method given on the Internet is: set the user running MySQL as the current user, for example, add configuration:
user: "1000:50"
Oruser:"999:999"
But how does the user 1000 or 999 get it? So, I found a more essential way to get the current user first, and then add it to the configuration, for example, add configuration,user: ${CURRENT_UID}
And then, each time it runs, it must first run on the host:CURRENT_UID=$(id -u):$(id -g)
。
I always feel that this method is not perfect. After thinking about it, I decided to check the host first. The current users running MySQL are running on the terminaltop
Command, found that the user running it is 999, runcat /etc/group
Look at the group to which the user belongs and find that there is a last rowdocker999
999 users belong to the docker group. Therefore, add the user (Ubuntu) used by my current host to the docker group, that is, run:sudo usermod -aG docker ubuntu
。 After that, run it againdocker-compose up mysql
There will be no more mistakes. (remember to add theuser: ${CURRENT_UID}
Configuration deleted)
Nginx
to configure
nginx:
image: nginx:alpine # Using Alpine Linux, its kernel is relatively small, greatly less mirror volume
volumes:
-The directory where the source code of the project is located
- ./conf/nginx/ nginx.conf :/etc/nginx/ nginx.conf : RO ා main configuration, set read only
- ./conf/nginx/conf.d:/etc/nginx/conf.d:ro
- ./log/nginx:/var/log/nignx
- ./ssl:/etc/nginx/ssl
networks:
- nginx
ports:
- "80:80"
- "443:443"
php-fpm
This part is relatively complex. Some extensions need to be installed to compile a new image, so the dockerfile file is used.
to configure
php-fpm73:
build:
Context: the context of constructing the image
dockerfile: dockerfiles/ Dockerfile.php73 #Specify the dockerfile file to find the address according to the context
volumes:
-The directory where the source code of the project is located
- ./conf/php:/usr/local/etc/php
- ./conf/php/conf.d:/usr/local/etc/php/conf.d
- ./conf/supervisor:/etc/supervisor/conf.d
networks:
- mysql
- nginx
ports:
- "9000:9000"
Problems encountered
- When installing the larravel project, we found that the GD extension does not exist
- Solve*: previously, it was directly available when running on other machines. Enter the PHP FPM container and run
php --ini
, output:Configuration File (php.ini) Path: /usr/local/etc/php Loaded Configuration File: /usr/local/etc/php/php.ini Scan for additional .ini files in: /usr/local/etc/php/conf.d Additional .ini files parsed: (none)
/There is no additional configuration in usr / local / etc / PHP / conf.d. I run it on another host with additional configuration:
Loaded Configuration File: /usr/local/etc/php/php.ini Scan for additional .ini files in: /usr/local/etc/php/conf.d Additional .ini files parsed: /usr/local/etc/php/conf.d/docker-php-ext-gd.ini, /usr/local/etc/php/conf.d/docker-php-ext-imagick.ini, /usr/local/etc/php/conf.d/docker-php-ext-mysqli.ini, /usr/local/etc/php/conf.d/docker-php-ext-opcache.ini, /usr/local/etc/php/conf.d/docker-php-ext-pcntl.ini, /usr/local/etc/php/conf.d/docker-php-ext-pdo_mysql.ini, /usr/local/etc/php/conf.d/docker-php-ext-redis.ini, /usr/local/etc/php/conf.d/docker-php-ext-sodium.ini, /usr/local/etc/php/conf.d/docker-php-ext-swoole.ini, /usr/local/etc/php/conf.d/docker-php-ext-zip.ini
Adjust the configuration, recompile, or not. Finally, you can only modify it manually php.ini , add:
extension=gd.so
Finally, the laravel project was successfully installed.
Complete configuration file and common commands
Refer to:https://github.com/HubQin/dnmp
functiongit clone https://github.com/HubQin/dnmp.git
After downloading the configuration, switch to the DNMP directory and run:docker-compose up -d
You can open the DNMP environment. Note that a project directory (if not available) will be generated outside the DNMP folder for the project code.
The common commands of docker compose are as follows:
docker-compose up -d
Running docker in the background- compose.yml All services configured- Enter the container, for example, into the php-fpm73 container, mode a:
docker container exec -it dnmp_php-fpm73_1 /bin/bash
,dnmp_php-fpm73_1
Is the container name, which can be accessed throughdocker ps
View; mode B:docker-compose exec php-fpm73 bash
,php-fpm73
Is the service name (docker- compose.yml Note that the command needs to be configured in docker- compose.yml Run or specify docker under the directory- compose.yml Location. - Recompile the image,
docker-compose build php-fpm73
- Specify the background to start the php-fpm73 service:
docker-compose up -d php-fpm73
- Stop all services:
docker-compose stop
- Stop all services and delete container:
docker-compose down
This work adoptsCC agreementThe author and the link to this article must be indicated in the reprint