In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to build a PHP environment in docker. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Using docker to build a flexible online php environment sometimes you may not need packages or images that have been integrated by others
We can use the following ways to build the environment structure we need one by one and finally achieve one-click automatic deployment.
Light up the docker skill tree step by step
# #. # # = = # # = = / ". _ / = ~ ~ {~ /. _ o _ /. _ /
* first, the clone [server] project is placed 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 image
Select a version after the docker pull php:7.2-fpm colon
Docker pull nginx
Docker pull mysql:5.7 does not require a local database to ignore
Docker pull redis:3.2 does not require local redis to ignore
Docker images to view all downloaded images
two。 Run the container after downloading the image [create the container using-- link method below and pay attention to the creation order]
Note:
-I means we are allowed to operate on the container.
-t indicates that one is designated as the terminal in the new container
-d indicates that the container executes in the background
/ bin/bash this will start bash shell in the container
-p create port mappings for containers and hosts
-- name specifies a name for the container
-v Mount the path in the container to the host path
-- privileged=true gives the container privileges. After mounting the directory, the container can access the files or directories under the directory.
-- link can be used to link two containers, so that the source container (the linked container) and the receiving container (the container that is actively unlinked) can communicate with each other, thus relieving the container IP for communication between containers.
Docker run-- name mydb-p 3306 MYSQL_ROOT_PASSWORD=123456-d mysql:5.7
Note:-MYSQL_ROOT_PASSWORD=123456 sets the initial password for mysql
If you don't need to build a local database, go straight to the next step.
Docker run-- name myredis-p 6379RV 6379-d redis:3.2
Note: if you do not need to build a local redis, go to the next step.
Docker run-d-p 9000 server/www:/var/www/html 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.
Note that-v mounting an empty folder will overwrite the contents of the container, so the configuration file should be prepared in advance
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:
After the colon of the-v statement is the path in the container, I mounted the nginx web page project directory configuration directory log directory to the / server directory that I prepared in advance.
-- link myphp:myphp connects the nginx container to the php container through the alias myphp so that you no longer need to specify the ip of the myphp container
Docker ps-a check that all containers are running successfully, and then the environment is basically built.
After mounting the directory, you can modify the configuration without entering the container, but directly change the configuration file and modify the nginx configuration under the corresponding mount directory.
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 502 503 504 / 50x.html localtion = / 50x.html {root / usr/share/nginx/html} location ~\ .php ${fastcgi_pass myphp 9000: # # the other IP must be specified to establish a link between the container and the container With the command sudo docker inspect myphp, you can see that the lowest IPAddress parameter is the container's ip # # when we created the container, we created the container in the way of-- link We can use the pseudonym of the Link container to wake up access, rather than through IP Relieved of dependence on IP fastcgi_index index.php fastcgi-param SCRIPI_FILENAME / var/www/html/blog/public$fastcgi_script_name: # # myphp and mynginx have different working directories mynginx is / usr/share/nginx/html # # php is / var/www/html, so when creating the container, we have already hung both directories on the same directory to the host / server/www, but the host public cannot be used here Mount directory include fastcgi_params:}}
3.PHP extension library installation
Docker exec-ti myphp / bin/bash first enters the container
Docker-php-ext-install pdo pdo_mysql installs the pdo_mysql extension
Docker-php-ext-install redis
Note: error message redis.so 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.tgz decompresses the downloaded redis expansion pack
Docker cp / server/php_lib/redis-4.1.0 myphp:/usr/src/php/ext/redis puts the extension in the container before performing the installation
Note:
Placing the extension package directly in the container ext directory may result in an error Error: No such container:path: myphp:/usr/src/php/ext
You can open one more server window and enter the php container to execute docker-php-ext-install redis. This time you report an error error: / usr/src/php/ext/redis does not exist.
Keep this state and execute the last command in your first server window.
(the exact reason is unknown, but it does take a docker-php-ext-install command container to open the / usr/src/php/ext directory.)
Method 2:
Note:
PECL (PHP's extended repository, packaged via PEAR) is officially recommended. Install the extension with pecl install, and then use the official docker-php-ext-enable
Quick script to enable extension
Pecl install redis & & docker-php-ext-enable redis
Restart the container after docker restart myphp installs the extension exit container
* other commands
Docker stop $(docker ps-Q) stop all containers
Docker rm $(docker ps-aq) deletes all containers
Docker rmi $(docker images-Q) deletes all mirrors
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 should know that mounting an empty folder will empty all the contents under the folder in the container, so you should copy before mounting.
For example: create a temporary container sudo docker run-- name mynginx-p 80:80-it-d nginx
Go to the container to find 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
When you delete a container and create a new container, you can mount this directory, and then the changes to the configuration file of nginx can be operated quickly on the host directly.
Docker run-name mynginx-d-p 80:80-v / server/nginx:/etc/nginx-- link myphp:myphp-- privileged=true nginx
(second-order) automatic construction of docker-compose
By completing the above steps, you have a preliminary understanding of the basic container operations of docker.
Docker-compose is choreographed for containers. For example, you have a php image, a mysql image, and a nginx image. If there is no docker-compose
So each time you start, you need to tap the startup parameters of each container, environment variables, container names, specify link parameters for different containers, and so on.
It's pretty cumbersome. After using docker-composer, you can write these commands in the docker-composer.yml file at once, and then start them every time.
In this whole environment (including 3 containers), you only need to ok by typing a docker-composer up command.
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-composechmod + x / usr/local/bin/docker-composedocker-compose-- version
two。 One-click deployment environment
/ server/compose/docker-compose.yml has been configured to enter commands directly.
Cd / server/composedocker-compose up-dversion: "2" sevices: mydb: container_name: "mydb" ports:-"3306 dversion 3306" volumes: # the mounted directory is written here-/ server/mysql:/var/lib/mysql-/ etc/localtime:/etc/localtime:re # sets the time synchronization of the container and the host environment: # Custom environment variables MYSQL_ROOT_PASSWORD:123456 images:mysql: 8.0 # Container reference Image myredis: container_name: "myredis" restart:always ports:-"6379myredis 6379" volumes:-/ server/redis:/data-/ etc/localtime:etc/localtime:re image: redis:3.2 myphp: container_name: "myphp" restart:always ports:-"9000 myredis 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/nginx.logsL/var/log/nginx-/ etc/localtime:/etc/localtime:re image: nginx:latest
Compared with running container commands above, the configuration structure and semantics of docker_yml are clear at a glance.
(third-order) complete construction of dokcer-compose and dockerfile
Docker-compose is used for one-click operation, but the problem is that the extension library of PHP still has to be installed separately, so here we need to use Dockerfile to build a custom container image.
Achieve real one-click completion
Table of contents:
Server-|-| compose.dockerfiles-| docker-compose.yml-| mysql-| Dockerfile set our custom dockerfile here to build mysql image |-| nginx-| Dockerfile here set our custom dockerfile to build nginx image |- | | php-| Dockerfile set our custom dockerfile here to build a php image |-| redis-| Dockerfile here set our custom dockerfile to build a redis image FROM php:7.2-fpm # build an official image MAINTAINER goozp overmapped by a custom image "user name" # set the container time zone to be the same as the host ENV TZ=Asia/ShanghaiRUN ln-snf / usr/share/zoneinfo / $TZ / etc/localtome & & echo $TZ > / etc/timezone# update installation dependency package and PHP core extension 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/*# extensions that do not exist use pecl to install RUN pecl install redis\ & & pecl install xdebug\ & & docker-php-ext-enable redis xdebugWORKDIR / data# permissions to set RUN usermod-u 1000 www-data
Custom php dockerfile build Custom Image while installing extensions after all the dockerfile configuration is completed, the docker-compose.yml file is not required
Then use the official image image:php-fpm:7.2 instead of direct build:./php to directly reference the configured Dockerfile of the directory
Final hint: once the image is created, the next docker-compose will directly take the existing image instead of build creation. If you modify the Dockerfile configuration, please remember to delete the image before.
Cd / server/compose.dockerfilesdocker-compose up-d
These are all the ways in which docker's environment is configured.
* when you need to use shell to schedule php to execute scripts on the host
This is the end of the article docker exec-it myphp / bin/bash-c'/ usr/local/bin/php / var/www/html/blog/public/index.php' on how to build a PHP environment in docker. I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.