docker installs php/nginx to build a local development environment

Here we mainly talk about the problems encountered in learning docker. Some basic installation processes will be skipped. Because there are already local projects under development and run in laravel homestead mode, I want to try docker mode.

Computer system: windows

Download the windows version client from the official website of docker.hub, then go to the Microsoft Store to search for ubuntu (depending on personal preferences) for download, and then open ubuntu to download docker.

1. Pull php image. Because the original project requires php version > = 7.2.3, the docker needs to specify the version when pulling php. The command is as follows:

docker pull php:7.2.3-fpm

2. Pull nginx image and directly obtain the latest version. The command is as follows:

docker pull nginx

3. Create a php container and use the command

docker run --name php-fpm  -v /mnt/e/code:/usr/share/nginx/html -it --privileged=true -d php:7.2.3-fpm

among

--Name is the container name, which can be set according to your preferences

-v directory mount. The directory of the local existing project directory in the ubuntu system is / mnt/e/code,: / usr/share/nginx/html, which is the running directory of the system project. Note that this directory should be consistent with the running directory in the nginx container when the nginx container is created later.

--privileged=true allows the root user in the container to really have root privileges

-d background operation mode

4. Create nginx container, command

docker run --name php-nginx -p 80:80 -it --privileged=true -d \
    -v /mnt/e/code:/usr/share/nginx/html:ro \
    -v ~/nginx/conf/conf.d:/etc/nginx/conf.d:ro \
    --link php-fpm:php \
    nginx

--Name is the container name, which can be set according to your preferences

-p port mapping can also be 8080:80. For convenience, I directly host port 80 to port 80 in the ubuntu system

--privileged=true allows the root user in the container to really have root privileges

-d background operation mode

-V / mnt/e/code: / usr/share/nginx/html mount the / mnt/e/code in the Ubuntu system to the / usr/share/nginx/html directory in the nginx container

-v ~/nginx/conf/conf.d:/etc/nginx/conf.d   Mount the ~ / nginx / conf / conf.d directory in the ubuntu system to the / etc / nginx / conf.d directory in the nginx container

--Link php FPM uses the php container created in 3 steps. php FPM is my own container name.

Now you can see two running containers in Containers/Apps in the left menu bar of docker client, and enter ~ / nginx/conf/conf.d directory

vim test.conf

Paste the following

server {
    listen       80;
    server_name  baidu.test;

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

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    if (!-e $request_filename) {
        rewrite ^/(.*)$ /index.php?s=$1 last;
    }

    location ~ \.php$ {
        root /usr/share/nginx/html/public;;
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html/public$fastcgi_script_name;
        include        fastcgi_params;
    }
}

Restart the nginx container, enter baidu.test in the browser, and it is found that you can successfully enter the project, but an error is reported, prompting could not find driver.

Because the mysql connected to the project is not local, there is no need to install the mysql container, but php needs to install the php mysql extension, and the official image is pulled down with many fewer extensions, so you need to install it yourself. My solution here is as follows

Enter the php container,

cd /usr/local/bin

Install extensions

./docker-php-ext-install pdo_mysql
./docker-php-ext-install mysql
./docker-php-ext-install mysqli

Similarly, the lack of other extensions can also be installed in this way, such as

./docker-php-ext-install gd 
./docker-php-ext-install -j$(nproc) bcmath

For details on how to install other extensions, please click this article to learn about the summary of PHP installation and expansion in Docker, which is connected to: https://www.cnblogs.com/hcpzhe/p/12109081.html .

After installation, normally we need to modify the configuration in php.ini, but this file is not in the PHP container of docker. Its php.ini configuration is scattered in many places. Then we can create an INI file ourselves and write the configuration we want to add.

cd /usr/local/etc/php/conf.d

Here I create a new test.ini file

vim test.ini

Add the following content to it

extension=mysqli
extension=pdo_mysql
extension=pdo_odbc
extension=openssl
extension=curl
extension=fileinfo
extension=gd
extension=pdo
extension=pdo_sqlite

Just install and add according to your project needs. After that, restart the php container, open the project again and run normally.

 

Tags: Docker

Posted on Wed, 24 Nov 2021 11:54:04 -0500 by puREp3s+