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: <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. 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 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 pecl install redis && docker-php-ext-enable redis
* 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 (Second order) docker-compose automated build After completing the above steps, you will have a preliminary understanding of the basic container operations of Docker. 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 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:
|
<<: Vuex implements simple shopping cart function
1. Scroll Snap is a must-have skill for front-end...
Table of contents 1. IDEA downloads the docker pl...
This article takes Centos7.6 system and Oracle11g...
1. Use docker images to view all the image files ...
This article example shares the specific code of ...
Find the problem I recently encountered a problem...
During development, a good user interface will al...
This article shares the specific code of the jQue...
Table of contents 0. Background 1. Installation 2...
Table of contents Preface How to switch between m...
The previous articles introduced the replacement ...
Table of contents animate() animation method Anim...
First look at the example code: #/bin/bash cal da...
Recently I used MySQL to export table data to an ...
You know that without it, the browser will use qui...