Docker's flexible implementation of building a PHP environment

Docker's flexible implementation of building a PHP environment

Use Docker to build a flexible online PHP environment. Sometimes you may not need some packages or images that others have already integrated.

We can use the following methods to build the environment structure we need one by one and finally achieve one-click automated deployment

Light up the Docker skill tree step by step

          ## .
       ## ## ## ==
      ## ## ## ## ## ===
    /""""""""""""""".__/ ===
 ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
    ._____ o __/
     . . __/
     .___.______/

* First, clone the [server] project and put it in the server root directory (you can also build your own environment structure later)

(First level) Use Docker to build one by one

1. Download the image

docker pull php:7.2-fpm select the version after the colon

docker pull nginx

docker pull mysql:5.7 does not require a local database and can be ignored

docker pull redis:3.2 does not require local redis, can be ignored

docker images View all downloaded images

2. Run the container after downloading the image [The following uses the --link method to create the container, pay attention to the creation order]

Note:
-i allows us to operate the container
-t specifies a terminal in the new container
-d means the container is executed in the background
/bin/bash This will start a bash shell inside the container
-p creates port mappings for the container and the host
--name specifies a name for the container
-v mounts the path in the container to the host path
--privileged=true gives the container privileges. After mounting the directory, the container can access the files or directories under the directory.
--link can be used to link two containers, so that the source container (the linked container) and the receiving container (the container that actively unlinks) can communicate with each other, eliminating the dependence of communication between containers on the container IP.

<Run mysql container>

docker run --name mydb -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

Note: -MYSQL_ROOT_PASSWORD=123456 sets the initial password for mysql

If you do not need to build a local database, go to the next step

<Run redis container>

docker run --name myredis -p 6379:6379 -d redis:3.2

Note: If you do not need to build a local redis, go to the next step

<Run php container>

docker run -d -p 9000:9000 --name myphp -v /server/www:/var/www/html -v /server/php:/usr/local/etc/php --link mydb:mydb --link myredis:myredis --privileged=true php:7.2-fpm

Note: If you do not need to build a local database or redis, you can omit --link mydb:mydb --link myredis:myredis

Note that -v mounting an empty folder will overwrite the contents of the container, so the configuration file must be prepared in advance

<Run nginx container>

docker run --name mynginx -d -p 80:80 -v /server/www:/usr/share/nginx/html -v /server/nginx:/etc/nginx -v /server/logs/nginx.logs:/var/log/nginx --link myphp:myphp --privileged=true nginx

Note:

The colon after the -v statement is the path inside the container. I mounted the nginx web project directory, configuration directory, and log directory to the /server directory I prepared in advance.
--link myphp:myphp connects the nginx container and the php container through the alias myphp, so there is no need to specify the ip of the myphp container

docker ps -a to see that all containers are running successfully. The environment is basically set up here.

After mounting the directory, you can modify the nginx configuration directly in the corresponding mounted directory without entering the container to modify the configuration

to /server/nginx/conf.d/Default.conf

server {
 listen 80:
 server_name localhost:
 location / {
 root /usr/share/nginx/html/blog/public: ##/user/share/nginx/html is the working directory index index.html index.php index.htm
 }

 error-page 500 502 503 504 /50x.html
 localtion = /50x.html {
 root /usr/share/nginx/html
 }

 location ~\.php$ {
 fastcgi_pass myphp 9000:
   ## To establish a link between containers, you must specify the other party's IP address. Use the command sudo docker inspect myphp to see that the IPAddress parameter at the bottom is the IP address of the container.
   ##We have created the container by --link when creating the container. We can use the pseudonym of the Link container to access it instead of the IP, eliminating the dependence on the IP fastcgi_index index.php
   fastcgi-param SCRIPI_FILENAME /var/www/html/blog/public$fastcgi_script_name:
   ## The working directories of myphp and mynginx are different. Mynginx is /usr/share/nginx/html
   ## php is /var/www/html So when creating the container, we have mounted both directories to the same directory on the host /server/www, but the host's public mount directory cannot be used here include fastcgi_params:
 }
}

3. PHP extension library installation

docker exec -ti myphp /bin/bash first enter the container

docker-php-ext-install pdo pdo_mysql Install the pdo_mysql extension

docker-php-ext-install redis

Note: At this time, an error message is displayed for redis.so because some extensions are not included in the PHP source code file

Method 1:

tar zxvf /server/php_lib/redis-4.1.0.tgz Unzip the downloaded redis extension package

docker cp /server/php_lib/redis-4.1.0 myphp:/usr/src/php/ext/redis Put the extension into the container and then install it

Note:

Placing the extension package directly into the container ext directory may result in an error message Error: No such container:path: myphp:/usr/src/php/ext
You can open another server window to enter the PHP container and execute docker-php-ext-install redis. At this time, an error message is reported: /usr/src/php/ext/redis does not exist
Keep this state and execute the previous command in your first server window to succeed.
(The specific reason is unknown, but the /usr/src/php/ext directory will be opened in the container only after the docker-php-ext-install command is executed once)

Method 2:

Note:

The official recommendation is to use PECL (PHP's extension library repository, packaged through PEAR). Use pecl install to install the extension, and then use the official docker-php-ext-enable
Quick script to enable extension

pecl install redis && docker-php-ext-enable redis

docker restart myphp After installing the extension, exit the container and restart the container

* Other commands

docker stop $(docker ps -q) stops all containers

docker rm $(docker ps -aq) delete all containers

docker rmi $(docker images -q) delete all images

docker inspect myphp to view container configuration information

* Build your own directory structure

You can also build your own server directory structure. First of all, you should know that mounting an empty folder will clear all the contents of the folder in the container, so you should copy it first and then mount it. For example: Create a temporary container sudo docker run --name mynginx -p 80:80 -it -d nginx
Enter the container and check the directory address of the configuration file you want, for example: /etc/nginx Exit the container and copy the directory structure you want in the container to the host, for example: docker cp mydb:/etc/nginx /server/nginx
When you delete the container and create a new container, you can mount the directory. After that, you can modify the nginx configuration file directly on the host machine.
docker run --name mynginx -d -p 80:80 -v /server/nginx:/etc/nginx --link myphp:myphp --privileged=true nginx

(Second order) docker-compose automated build

After completing the above steps, you will have a preliminary understanding of the basic container operations of Docker.
Docker-compose is used to orchestrate containers. For example, you have a php image, a mysql image, and an nginx image. If there is no docker-compose,
Then every time you start the container, you need to enter the startup parameters of each container, environment variables, container names, specify the link parameters of different containers, and so on.
Quite cumbersome. After using docker-composer, you can write these commands in the docker-composer.yml file at once. Every time you start the entire environment (including 3 containers), you only need to type a docker-composer up command.

1. Install docker-compose

curl -L https://github.com/docker/compose/releases/download/1.8.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose

docker-compose --version

2. One-click deployment environment

/server/compose/docker-compose.yml has been configured, just enter the command

cd /server/compose

docker-compose up -d
version:"2"
Sevices:
  mydb:
    container_name:"mydb"
    ports:
      - "3306:3306"
    volumes: #Write the mounted directory here - /server/mysql:/var/lib/mysql
      - /etc/localtime:/etc/localtime:re #Set the time synchronization between the container and the host environment: # Custom environment variable MYSQL_ROOT_PASSWORD:123456
    images:mysql: 8.0 # The container references the image myredis:
    container_name:"myredis"
    restart:always
    ports:
      - "6379:6379"
    volumes:
      - /server/redis:/data
      -/etc/localtime:etc/localtime:re
    image: redis:3.2
  myphp:
    container_name:"myphp"
    restart: always
    ports:
      - "9000:9000"
    volumes:
      - /server/www:/var/www/html
      -/sever/php:/usr/local/etc/php
      -/etc/localtime:/etc/localtime:re
     links:
      - "mydb"
      - "myredis"
     image:php:7.2-fpm
   mynginx:
     container_name: "mynginx"
     restart: always
     ports:
       - "80:80"
     links:
       - "myphp"
     Volumes:
       - /server/www:/usr/share/nginx/html
       - /server/nginx:/etc/nginx
       - /server/logs/nginx.logsL/var/log/nginx
       - /etc/localtime:/etc/localtime:re
     image: nginx:latest

Compared with the above command to run the container, the configuration structure and semantics of docker_yml are clear at a glance.

(Level 3) docker-compose and dockerfile complete build

Docker-compose is used to achieve one-click operation, but the problem is that the PHP extension library still needs to be installed separately, so Dockerfile is needed here to build a custom container image.

Achieve true one-click completion

Table of contents:

  server -|           
     -| compose.dockerfiles -| docker-compose.yml
                 -| mysql -| Dockerfile Here we set our custom dockerfile to build the mysql image|      
                 -| nginx -| Dockerfile Here we set our custom dockerfile to build the nginx image|     
                 -| php -| Dockerfile Here we set our custom dockerfile to build the php image|    
                 -| redis -| Dockerfile Here we set our own Dockerfile to build the redis image
FROM php:7.2-fpm #Build a custom image and import the official image MAINTAINER goozp "user name"
#Set the container time zone to the same as the host ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtome && echo $TZ > /etc/timezone
#Update and install dependent packages and PHP core extensions RUN apt-get update && apt-get install -y \
      libfreetype6-dev \ 
      libhpeg62-turbo-dev \
      libpng-dev \ 
    && 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 zip \ 
      && docker-php-ext-install pdo_mysql \
      && docker-php-ext-install opcache
      && docker-php-ext-install mysqli
      rm -r /var/lib/apt/lists/*
# If the source package does not exist, use pecl to install the extension. RUN pecl install redis \
  && pecl install xdebug \
  && docker-php-ext-enable redis xdebug

WORKDIR /data
#Permission setting RUN usermod -u 1000 www-data

Customize PHP's Dockerfile to build a custom image and install extensions at the same time. After completing all Dockerfile configurations, the Docker-compose.yml file no longer needs to use the official image image:php-fpm:7.2, but directly build: ./php to directly reference the Dockerfile configured in the directory
Final tip: Once the image is created, the next time docker-compose will directly take the existing image instead of building it. If you modify the Dockerfile configuration, please remember to delete the previous image.

cd /server/compose.dockerfiles

docker-compose up -d

The above is all the environment configuration methods of Docker

* When you need to use shell to schedule PHP to execute scripts on the host machine

docker exec -it myphp /bin/bash -c '/usr/local/bin/php /var/www/html/blog/public/index.php'

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 PHP development environment through docker on Mac
  • Docker installation of PHP and deployment example with Nginx
  • Explanation of the steps to install PHP extension in Docker
  • How to use Docker to build OpenLDAP+phpLDAPadmin unified user authentication
  • Docker build PHP environment tutorial detailed explanation
  • Detailed tutorial on installing php-fpm service/extension/configuration in docker
  • How to deploy LNMP & phpMyAdmin in docker
  • PHP uses docker to run workerman case explanation

<<:  Vuex implements simple shopping cart function

>>:  Solve the problem of changing the password when logging in for the first time after installing MySQL 8.0

Recommend

Table of CSS Bugs Caused by hasLayout

IE has had problems for a long time. When everyone...

MySQL 5.7 zip archive version installation tutorial

This article shares the installation tutorial of ...

What is ZFS? Reasons to use ZFS and its features

History of ZFS The Z File System (ZFS) was develo...

Use semantic tags to write your HTML compatible with IE6,7,8

HTML5 adds more semantic tags, such as header, fo...

How to use crontab to backup MySQL database regularly in Linux system

Use the system crontab to execute backup files re...

How to solve the problem of MySQL query character set mismatch

Find the problem I recently encountered a problem...

7 Best VSCode Extensions for Vue Developers

Adding the right VS Code extension to Visual Stud...

How to operate MySQL database with ORM model framework

What is ORM? ORM stands for Object Relational Map...

About the pitfalls of implementing specified encoding in MySQL

Written in front Environment: MySQL 5.7+, MySQL d...

Vue custom v-has instruction to implement button permission judgment

Application Scenario Taking the background manage...

VMware ESXi installation and use record (with download)

Table of contents 1. Install ESXi 2. Set up ESXi ...

Example code for implementing stacked carousel effect with HTML+CSS+JS

Effect: When the slideshow moves in one direction...