Docker flexible construction of PHP environment

Using docker to build a flexible online PHP environment, sometimes you may not need some packages or images that others have integrated

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

(first order) build one by one with docker

1. Download Image

docker pull php:7.2-fpmSelect version after colon

docker pull nginx

docker pull mysql:5.7Local database is not required to be ignored

docker pull redis:3.2Local redis is not required to be ignored

docker imagesView all downloaded images

2. After downloading the image, run the container [create the container in — link mode, pay attention to the creation order]


< 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 initial password for MySQL

< 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 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 don't need to build a local database or redis, you can omit -- link mydb:mydb --link  myredis:myredis

< 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


docker ps -aCheck that all containers run successfully, and the environment is basically set up

After mounting the directory, you can directly change the configuration file in the corresponding mount directory without entering the container to modify the nginx configuration to / server / nginx / conf.d/ Default.conf
server {

3. PHP extension library installation

docker exec -ti myphp /bin/bashEnter the container first

docker-php-ext-install pdo pdo_mysqlInstall PDO_ MySQL extension

docker-php-ext-install redis

Note: an error is reported at this time  Because some extensions are not included in the PHP source file
Method 1

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

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

Method 2

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

docker restart myphpAfter loading the extension, exit the container and restart the container

*Other orders

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

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

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

docker inspect myphpView 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 empty all contents under the folder in the container, so you should copy and then mount

(second order) automated construction of docker compose

After completing the above steps, you have a preliminary understanding of docker's basic container operation

1. Install docker compose

curl -L`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 It has been configured to enter the command directly

cd /server/compose

docker-compose up -d


Compare the container command above to see docker_ The configuration structure and semantics of YML are clear at a glance

(third order) complete construction of dokcer compose and dockerfile

Docker compose is used to implement one click operation, but the problem is that the PHP extension library has to be installed by itself, so we need to use dockerfile to build a custom container image
FROM  php:7.2-fpm # Build official image of custom image
Customize the dockerfile of PHP, build the custom image, install the extension, and complete the docker configuration- compose.yml Documents are not needed

cd /server/compose.dockerfiles

docker-compose up -d

The above is all the environment configuration methods of docker

*When you need to use the 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/index.php'

MySQL 8 setting up remote access

Enter MySQL image

This work adoptsCC agreementThe author and the link to this article must be indicated in the reprint

You’re far from it!