SoFunction
Updated on 2025-03-02

Docker flexibly builds PHP environment implementation

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

We can use the following methods to build the environment structure we need one by one, and implement one-click automated deployment at the end

Light up the docker skill tree step by step

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

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

(First order) Build one by one using docker

1. Download the mirror

docker pull php:7.2-fpm colon select version

docker pull nginx

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

docker pull redis:3.2 does not require local redis to be ignored

docker images View all images that have been downloaded

2. Run the container after downloading the image [The following is to create the container using --link method. Pay attention to the creation order]

Note:
-i means that we allow us to operate on the container
-t means that one is specified as a terminal in the new container
-d means that the container is executed in the background
/bin/bash This will start the bash shell inside the container
-p Create port maps for containers and hosts
--name Specify a name for the container
-v Mount the path inside the container to the host path
--privileged=true Give the container privileges. After mounting the directory, the container can access files or directories below the directory.
--link can be used to link two containers, so that the source container (linked container) and the receiving container (actively delinked container) can communicate with each other, relieving the dependence on container IP for communication between containers.

<Run mysql container>

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

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

If you don't need to build a local database, just go ahead

<Run redis container>

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

Note: If you don't need to build a local redis, go straight 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 -v Mounting an empty folder will overwrite the contents in 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/:/var/log/nginx --link myphp:myphp --privileged=true nginx

Note:

-The colon of 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 Connect nginx container and php container with alias myphp, you no longer need to specify the ip of the myphp container

docker ps -a Check that all containers run successfully, the environment here is basically completed

After mounting the directory, you can modify the configuration without entering the container and directly modify the configuration file in the corresponding mount directory. Modify the nginx configuration

Go to /server/nginx//

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

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

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

Extension library installation

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

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

docker-php-ext-install redis

Note: An error prompt is reported at this time because some extensions are not included in the PHP source code file

Method 1:

tar zxvf /server/php_lib/redis-4.1. Decompress the downloaded redis extension package

docker cp /server/php_lib/redis-4.1.0 myphp:/usr/src/php/ext/redis Place the extension into the container and then execute the installation

Note:

Putting the extension package directly into the container ext directory may cause an error: No such container:path: myphp:/usr/src/php/ext
You can open an extra server window and enter the php container to execute docker-php-ext-install. Redis error: /usr/src/php/ext/redis does not exist
Keep this state and execute the previous command in your first server window.
(The specific reason is unknown, but it does require the docker-php-ext-install command to be executed once. The /usr/src/php/ext directory will be opened in the container)

Method 2:

Note:

It is officially recommended 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 scripts to enable extensions

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

docker restart myphpInstall the extension Exit the container Restart the container

* Other commands

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

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

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

docker inspect myphp view container configuration information

* Build your own directory structure

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

(Second-order) docker-compose automated construction

Complete the above steps and you have a preliminary understanding of the basic container operations of docker
docker-compose is to orchestrate containers. For example, you have a php image, a mysql image, and a nginx image. If there is no docker-compose,
Then every time you start, you need to knock on the startup parameters of each container, environment variables, container naming, specifying link parameters of different containers, etc., etc.
Quite cumbersome. After using docker-composer, you can write these commands in the file at once and start them every time.
When this entire environment (including 3 containers), you just need to hit the docker-composer up command and it will be OK.

1. Install docker-compose

curl -L /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/ has been configured. Just enter the command directly.

cd /server/compose

docker-compose up -d
version:"2"
sevices:
  mydb:
    container_name:"mydb"
    ports:
      - "3306:3306"
    volumes: #The mounted directory is written here      - /server/mysql:/var/lib/mysql
      - /etc/localtime:/etc/localtime:re #Set time synchronization between container and host    environment: # Customize environment variables      MYSQL_ROOT_PASSWORD:123456
    images:mysql: 8.0 # Container reference 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"
     volnmes:
       - /server/www:/usr/share/nginx/html
       - /server/nginx:/etc/nginx
       - /server/logs//var/log/nginx
       - /etc/localtime:/etc/localtime:re
     image: nginx:latest

Comparing the above running container commands, you can see the configuration structure and semantics of docker_yml at a glance

(Third-order) complete construction of docker-compose and dockerfile

Use docker-compose to implement one-click operation, but the problem is that the PHP extension library still needs to be installed separately, so here we need to use Dockerfile to build a custom container image

Realize one-click completion

Table of contents:

  server -|           
     -|  -| 
                 -| mysql -| Dockerfile Here we set our custom onesdockerfileTo buildmysqlMirror     
                  |      
                 -| nginx -| Dockerfile Here we set our custom onesdockerfileTo buildnginxMirror
                  |     
                 -| php -| Dockerfile Here we set our custom onesdockerfileTo buildphpMirror
                  |    
                 -| redis -| Dockerfile Here we set our custom onesdockerfileTo buildredisMirror
FROM php:7.2-fpm #Build a custom mirror and then mirror the official imageMAINTAINER goozp "username"
#Set the container time zone the same as the hostENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtome &amp;&amp; echo $TZ &gt; /etc/timezone
#Update installation dependency package and PHP core expansionRUN apt-get update &amp;&amp; apt-get install -y \
      libfreetype6-dev \ 
      libhpeg62-turbo-dev \
      libpng-dev \ 
    &amp;&amp; docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    &amp;&amp; docker-php-ext-install - j$(nproc) gd \ 
      &amp;&amp; docker-php-ext-install zip \ 
      &amp;&amp; docker-php-ext-install pdo_mysql \
      &amp;&amp; docker-php-ext-install opcache
      &amp;&amp; docker-php-ext-install mysqli
      rm -r /var/lib/apt/lists/*
# Extensions that do not exist in the source code package use pecl installationRUN pecl install redis \
  &amp;&amp; pecl install xdebug \
  &amp;&amp; docker-php-ext-enable redis xdebug

WORKDIR /data
#Permission SettingsRUN usermod -u 1000 www-data

Customize the dockerfile of custom php to build a custom image and install the extension at the same time. After completing all dockerfile configurations, the file is not needed.
Then use the official image image:php-fpm:7.2 to directly build: ./php to directly reference the directory configured Dockerfile
Last tip: Once the image is created, the next time docker-compose, it will directly take the existing image without building it. If you modify the Dockerfile configuration, please remember to delete the previous image.

cd /server/

docker-compose up -d

The above is all the configuration methods of docker

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

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

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.