How to build php+nginx+swoole+mysql+redis environment with docker

How to build php+nginx+swoole+mysql+redis environment with docker

Operating system: Alibaba Cloud ESC instance centos7.4

Software: docker-ce version 18.09.3, docker-compose version 1.23.2

1. Create a docker image with swoole-redis-pdo_mysql-gd extension

1. Create a Dockerfile

vim dockerfile

2. Write in the dockerfile file

From php:7.1-fpm
RUN apt-get update && apt-get install -y \
  libfreetype6-dev \
  libjpeg62-turbo-dev \
  libpng-dev \
&& docker-php-ext-install -j$(nproc) iconv \
&& 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-configure pdo_mysql \
&& docker-php-ext-install pdo_mysql \
&& pecl install redis-4.3.0 \
&& pecl install swoole \
&& docker-php-ext-enable redis swoole

3. Create a custom PHP image. Don't miss the last '.', which specifies the current directory to build the image.

docker build -t myphp4 .

Run the command. Due to network problems, it takes a long time to run. After success, a code similar to the following will appear.

...
Build process completed successfully
Installing '/usr/local/include/php/ext/swoole/config.h'
Installing '/usr/local/lib/php/extensions/no-debug-non-zts-20160303/swoole.so'
install ok: channel://pecl.php.net/swoole-4.3.1
configuration option "php_ini" is not set to php.ini location
You should add "extension=swoole.so" to php.ini
Removing intermediate container ad1420f7554f
---> 2f2f332d73ce
Successfully built 2f2f332d73ce
Successfully tagged myphp4:latest

At this point, the custom myphp4 image of docker has been created successfully!

2. Create a docker-compose.yml file

mkdir pnsmr
cd pnsmr
vim docker-compose.yml

Write the following code

version: '3.0'
services:
nginx:
  image: "nginx:latest"
  ports:
   - "10000:80"
  volumes:
   - /var/www/html:/usr/share/nginx/html
php-fpm:
  image: "myphp4"
  volumes:
  - /var/www/html:/usr/share/nginx/html
mysql:
  image: "mysql:latest"
redis:
  image: "redis:4.0"

Run Instructions

docker-compose up -d

Success can be seen

WARNING: The Docker Engine you're using is running in swarm mode.

Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.

To deploy your application across the swarm, use `docker stack deploy`.

Creating network "pnsmr_default" with the default driver
Creating pnsmr_php-fpm_1 ... done
Creating pnsmr_redis_1 ... done
Creating pnsmr_mysql_1 ... done
Creating pnsmr_nginx_1 ... done

So far, the nginx mysql redis php service has been started

3. Modify each service configuration file

1. Enter 127.0.0.1:9998 in the browser #You should enter your server IP address here, and you can see the following picture


2. Next, modify the nginx configuration file in the container. First, use the command to view the docker IP address of each container

docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)

This command can view the IP addresses of all containers opened with docker-compose. The result is similar to the figure below. The corresponding IP addresses can be used for internal communication.

3. Copy the configuration file of the nginx container and modify it to enable nginx to parse PHP

docker cp pnsmr_nginx_1:/etc/nginx/conf.d/default.conf nginx.conf
vim nginx.conf

Modify to the following code

server {
  listen 80;
  server_name localhost;

  #charset koi8-r;
  #access_log /var/log/nginx/host.access.log main;

  location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
  }

  #error_page 404 /404.html;

  # redirect server error pages to the static page /50x.html
  #
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root /usr/share/nginx/html;
  }

  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  #
  #location ~ \.php$ {
  # proxy_pass http://127.0.0.1;
  #}

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  location ~ \.php$ {
    root html;
    fastcgi_pass 172.24.0.3:9000;#You need to fill in the docker internal communication ip of your php container here
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/$fastcgi_script_name;
    include fastcgi_params;
  }

  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
  #
  #location ~ /\.ht {
  # deny all;
  #}
}
docker cp nginx.conf pnsmr_nginx_1:/etc/nginx/conf.d/default.conf #Copy the modified configuration file to the container docker container stop pnsmr_nginx_1 
docker container start pnsmr_nginx_1 #Restart the nginx container to make the configuration file take effect vim /var/www/html/index.php #Create a new index.php file in the local directory of the server, enter <?php phpinfo(); and save it vim /var/www/html/index.html #Create a new index.html file in the local directory of the server and output helloworld

Access 127.0.0.1:9998, html file parsing is normal


Access 127.0.0.1:9998/index.php, php file parsing is normal


4. Test whether mysql and redis are effective

vim /var/www/html/redis.php #Used to test whether redis is configured successfully<?php
$redis = new Redis();
$redis->connect("172.24.0.4",6379);
$redis->set('test','this is a test for redis');
echo $redis->get('test');

Visit 127.0.0.1:9998/redis.php, redis has taken effect

Enter the mysql container

docker exec -it pnsmr_mysql_1 bash

Enter mysql and change the root user password

Create a test file

vim /var/www/html/mysql.php
<?php
$pdo = new PDO('mysql:host=172.24.0.2;dbname=mysql;port=3306','root','root123');
var_dump($pdo);

Access 127.0.0.1:9998/mysql.php, mysql is effective

IV. Conclusion

Although the environment is successfully configured and can be generated with one click using the docker-compose up command, the configuration files of each container still need to be modified, which is still not convenient enough and needs to be optimized. In addition, the cluster and stack functions of docker are not used, which will be studied later.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to configure nginx+php+mysql in docker
  • How to build php-nginx-alpine image from scratch in Docker
  • The whole process of using docker to build php7 and nginx operating environment (official image)
  • Docker installation of PHP and deployment example with Nginx

<<:  MySQL Optimization Summary - Total Number of Query Entries

>>:  js+Html to realize table editable operation

Recommend

Example of how to implement local fuzzy search function in front-end JavaScript

Table of contents 1. Project Prospects 2. Knowled...

MySQL table field time setting default value

Application Scenario In the data table, the appli...

Solution to the problem that docker logs cannot be retrieved

When checking the service daily, when I went to l...

Detailed explanation of psql database backup and recovery in docker

1. Postgres database backup in Docker Order: dock...

MySQL query sorting and paging related

Overview It is usually not what we want to presen...

Detailed explanation of Linux one-line command to process batch files

Preface The best method may not be the one you ca...

How to modify the IP restriction conditions of MySQL account

Preface Recently, I encountered a requirement at ...

Problems with installing mysql and mysql.sock under linux

Recently, I encountered many problems when instal...

Detailed explanation of root directory settings in nginx.conf

There are always some problems when configuring n...

Docker-compose creates a bridge, adds a subnet, and deletes a network card

1. Create a docker network card [root@i ~]# brctl...