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

Practice of el-cascader cascade selector in elementui

Table of contents 1. Effect 2. Main code 1. Effec...

Vue Basic Tutorial: Conditional Rendering and List Rendering

Table of contents Preface 1.1 Function 1.2 How to...

Detailed process of installing and configuring MySQL and Navicat prenium

Prerequisite: Mac, zsh installed, mysql downloade...

Windows cannot start MySQL service and reports error 1067 solution

Suddenly when I logged into MySQL, it said that a...

How to completely delete the MySQL 8.0 service under Linux

Before reading this article, it is best to have a...

Detailed explanation of MYSQL stored procedure comments

Table of contents 1. Instructions for use 2. Prep...

Will CSS3 really replace SCSS?

When it comes to styling our web pages, we have t...

Analysis of different MySQL table sorting rules error

The following error is reported when MySQL joins ...

Detailed explanation of MySQL 8's new feature ROLE

What problems does MySQL ROLE solve? If you are a...

The difference between MySQL user management and PostgreSQL user management

1. MySQL User Management [Example 1.1] Log in to ...

Detailed example of using case statement in MySQL stored procedure

This article uses an example to illustrate the us...

How to convert JavaScript array into tree structure

1. Demand The backend provides such data for the ...

A brief analysis of the best way to deal with forgotten MySQL 8 passwords

Preface Readers who are familiar with MySQL may f...

How to implement on-demand import and global import in element-plus

Table of contents Import on demand: Global Import...