Docker is a very popular technology in recent years. As a software developer, I should timely contact and master it. The company I just entered needs to use Docker as a development environment, so I have to learn how to use Docker. Today I write about the process of setting up the PHP development environment, which is also a record.

I use Docker Desktop for Mac, Docker installation needs to pull image, using the original image pull will be very slow. However, we can use domestic mirror resources, and most of them are free. I use ali Cloud’s image acceleration link, which is very fast. We can get it for free on Ali Cloud and then configure it to use it. Ali Cloud Docker accelerator, no Ali cloud account registration can be a.



Check whether Docker is running properly

docker -v
Copy the code

Install Mysql pull mirror

Mysql docker pull Mysql :5.7Copy the code

Running Mysql

Mysql docker run --name Mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=12345678 -d Mysql # run docker run --name Mysql_57 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=12345678 -d mysql:5.7Copy the code

Parameters:

  • Run: Creates a new container
  • – name: indicates the container name
  • -p: port mapping (local_port:container_port)
  • -e: passes in environment variables
  • -d: background running

Inside the container

docker exec -it mysql bash
Copy the code
  • -I allows us to operate on the container
  • -t Specifies a terminal in a new container
  • /etc/mysql: this directory contains the my.cnf configuration and a conf.d folder. The my.cnf file contains some basic configuration. The custom configuration should be written to the configuration file in the conf.d folder to override the default my.cnf.
  • /var/lib/mysql: persistent data storage directory of mysql in the mirror

MySQL is a stateful application. If MySQL is containerized, its state needs to be removed from the host, and its configuration files, log files, and persistent data should be mounted to the host. Create a directory on the PC to store Mysql data, configuration, and logs. In this way, the data is stored on the host. We can delete the container and install it again.

mkdir -p docker/mysql &&  cd docker/mysql
mkdir data log conf
Copy the code

Remove the Mysql container we created earlier

docker stop mysql && docker rm mysql
Copy the code

Create the my. CNF file in the conf directory as the Mysql configuration file

Vim my.cnf [mysqld] datadir=/var/lib/mysql # Default-time_zone = '+8:00' log-error= /var/log/mysql.errorCopy the code

Then re-create the Mysql container and mount the created directory to the corresponding directory within the container.

Docker run --name mysql --restart=always --privileged=true docker run --name mysql --restart=always --privileged=true Insufficient or it will tip the log directory permissions) - p, 3306:3306 - v docker/mysql/conf: / etc/mysql/conf. D (the configuration directory mounted to the container configuration directory) - v Docker/mysql/data: / var/lib/mysql (storing the data directory is mounted to the container out of storage directory) - v/docker/mysql/log: / var/log/mysql (the log directory mounted to the container log directory) - e MYSQL_ROOT_PASSWORD=12345678 -d mysqlCopy the code
  • -v Mounts the container path to the host path

Inside the container, configure the connection to Mysql using database management tools

$ docker exec -it mysql bash

$ mysql -u root -p

$ mysql-> ALTER user 'root'@'%' IDENTIFIED WITH mysql_native_password BY '12345678';

$ mysql-> FLUSH PRIVILEGES;
Copy the code

You are now ready to connect using database management tools.

Install Nginx pull image

docker pull nginx
Copy the code

Create and start the container

docker run --name nginx -p 80:80 -d nginx
Copy the code

Then visit localhost to see Nginx’s default welcome page create directory to store Nginx configuration and logs

mkdir -p docker/nginx && cd docker/nginx
mkdir conf log
Copy the code

Copy the configuration file from the container to the host and map the configuration file to the corresponding directory in the container

docker cp nginx:/etc/nginx/conf.d/default.conf docker/nginx/conf/default.conf
Copy the code

To recreate the

docker stop nginx && docker rm nginx
docker run --name nginx -p 80:80 -v docker/nginx/conf:/etc/nginx/conf.d 
-v docker/www:/usr/share/nginx/html -v docker/nginx/log:/var/log/nginx -d nginx
Copy the code

Install PHP to pull PHP images

docker pull php:fpm
Copy the code

Create a container

docker run --name php-fpm -p 9000:9000 -d php:fpm
Copy the code

Create a directory to store PHP configuration files and logs

mkdir -p docker/php && cd docker/php
mkdir conf log
Copy the code

Copy the configuration file from the container

docker cp php-fpm:/usr/local/etc/php-fpm.d/www.conf docker/php/conf/www.conf
docker cp php-fpm:/usr/local/etc/php/php.ini-production docker/php/conf/php.ini
Copy the code

Restart the container

docker run --name php-fpm --link mysql:mysql 
                -v ~/docker/www:/var/www/html 
                -v docker/php/conf/www.conf:/usr/local/etc/php-fpm.d/www.conf 
                -v docker/php/conf/php.ini:/usr/local/etc/php/php.ini 
                -d php:fpm
Copy the code
  • – Link: connects two containers so that the source and receiver containers can communicate with each other

After connecting to the PHP and Mysql containers, the PHP script connects to the database with the host value of ‘Mysql’.

The Nginx container supports the php-fPM container to modify the Nginx configuration file default.conf

server { listen 80; server_name localhost; root /usr/share/nginx/html; charset utf-8; access_log /var/log/nginx/access.log main; error_log /var/log/nginx/error.log; location / { index index.html index.htm index.php; try_files $uri $uri/ /index.php? $query_string; } #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 html; } location ~ \.php$ { fastcgi_index index.php; #php-fpm :9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; }}Copy the code

Recreate the Nginx container

docker stop nginx && docker rm nginx
docker run --name nginx -p 80:80 --link php-fpm 
                  -v docker/nginx/conf:/etc/nginx/conf.d 
                  -v docker/www:/usr/share/nginx/html 
                  -v docker/nginx/log:/var/log/nginx 
                  -d nginx
Copy the code

OK! Create index. PHP under docker/ WWW

phpinfo();
Copy the code

Open localhost to see the output of the phpInfo () function.

Install PHP extensions

docker-php-ext-install mysqli
Copy the code

If the message cannot be installed

pecl install redis && docker-php-ext-install redis
Copy the code

Install the GD extension

apt-get update
apt-get install libpng-dev libjpeg-dev libfreetype6-dev
docker-php-ext-install gd
Copy the code

After installing the extension, you don’t need to modify the configuration file, just restart the PHP container.

  • Mysql > PHP > nginx