In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to Docker the PHP project". In the daily operation, I believe many people have doubts about how to Docker the PHP project. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to Docker the PHP project". Next, please follow the editor to study!
The highlight of the article
Containerization steps for PHP applications and their dependent services
How to automate the construction of application container images
How to quickly deploy application containers to test and production environments
Get started quickly
PHP officially maintains the official PHP Docker image on http://hub.docker.com, which contains a variety of different versions of PHP from 5.5to 7.0.
We will introduce how to Dockerize a simple PHP application based on PHP's official Docker image.
Create a new directory php-quickstart as our project directory
Create the file app.php in the project directory
Create the file Dockerfile in the project directory
FROM php:5.6-cliCOPY. / projectWORKDIR / projectCMD ["php", ". / app.php"]
In the above Dockerfile, we use the official php-5.6-cli as our basic image through the FROM instruction.
Through the COPY instruction, we copy the files in the current directory to the mirrored / project directory
The CMD directive sets the command that the mirror executes by default, while WORKDIR sets the directory where the mirror executes the command.
Build an image
Docker build-t php-app.
This will generate a mirror named php-app
Run the container
Docker run php-app
At this point, the container will execute the app.php we created earlier and output:
Docker example of Hello Dockerboard PHP + MySQL
Next, through an example of PHP + MySQL, we introduce how to connect to the database after Docker is applied to PHP.
Create a new directory php-mysql as our project directory
In index.php, our PHP application will connect to the mysql database through the host name db, using the username root and MYSQL_ROOT_PASSWORD in the environment variables. Here, simply use the echo connection information to confirm that the MySQL connection is working.
Create a Dockerfile under the project directory
FROM php:5.6-apacheRUN docker-php-ext-install mysqliCOPY. / var/www/html
Here we are using the official php:5.6-apache image, because this time we want to be able to access the PHP application directly from the browser.
In addition, we have installed the mysqli extension of PHP by running docker-php-ext-install mysqli through the RUN instruction
Build an image
Docker build-t php-mysql-app.
Create a MySQL container
Docker run-- name db-e MYSQL_ROOT_PASSWORD=secret-d mysql:5.6
Here we create a MySQL container using the official mysql:5.6 image
-- the name parameter names the container db
-e MYSQL_ROOT_PASSWORD=secret through the environment variable, we set the root user password of MySQL to secret
The-d parameter sets this container to run in the background
Start the PHP container and connect it to the MySQL container
Docker run-- link db-e MYSQL_ROOT_PASSWORD=secret-p 8080 MYSQL_ROOT_PASSWORD=secret 80 php-mysql-app
We run the php-mysql-app image we built earlier, and connect the MySQL container mysql-instance created in the previous step to it. At the same time, we pass the root password of MySQL to the inside of the container through the environment variable MYSQL_ROOT_PASSWORD-p 8080 MySQL 80 and map port 80 of the container to port 8080 of the host.
Access http://127.0.0.1:8080 from a browser
Connected to mysql: db via TCP/IP
We will get the execution result of index.php from the browser.
Automatic Image Construction based on cSphere Private Docker Registry
In a Docker project, the Docker image of the project becomes the final component of the project delivery. Therefore, in the continuous integration and continuous delivery of the project, the automatic construction of the image is an indispensable part.
This article describes how to use cSphere's private image repository to configure automatic image construction, so as to automatically build Docker images after the code Push is sent to the repository.
Create a private Docker Registry
On the Image Warehouse page via cSphere, click the New Image Warehouse button and follow the prompts to successfully create a private image repository.
Configure the project
Go to the image repository page created in the previous step, click the add automatic build button, and enter the Git repository address and Dockerfile path of the project:
Then, according to the prompt, set the location where the image will be stored in the image repository and the branches that need to be built automatically:
Set up the Web Hook and Deploy key of the project
According to the prompt, set up Webhook and Deploy Key. In this way, when the project has a new code push to the branch set in the previous step, the private Docker Registry will automatically build the image. After the construction is successful, the image will be automatically Push to the specified location of the image repository.
Deploy and manage PHP applications using cSphere
After you have implemented the automatic build project mirror, let's take a look at how to quickly deploy the project to various environments through cSphere.
Create an application template
Go to the cSphere application template page, click the create New template button, and follow the prompts to create a new application template.
Add MySQL service
In the previous Docker example of the PHP + MySQL project, we started the MySQL container with the following command:
Docker run-- name db-e MYSQL_ROOT_PASSWORD=secret-d mysql:5.6
At this point, we configure the above command as a service in the application template: db
At the same time, set the environment variables.
Add PHP service
In the Docker example of the PHP + MySQL project, we started the PHP container with the following command:
Docker run-- link db-e MYSQL_ROOT_PASSWORD=secret-p 8080 MYSQL_ROOT_PASSWORD=secret 80 php-mysql-app
At the same time, in the automatic build image, we set the automatic build image to 192.168.1.130/tsing/php-mysql-app:latest
Here we configure the above information as another service of the application template: php
Set the value of the environment variable used in PHP code
-- the parameter link db does not need to be set in the application template, because cSphere application management will automatically handle the connection of different containers according to the name of the service.
The-p port mapping does not need to be set, because all containers created by cSphere application management have independent IP, so it is no longer necessary to map the port of the container to the host.
Save template
Deploy the application
Click the deployment button on the rightmost right of the template version just created in the previous step to start deployment.
In this interface, you can choose which host group to deploy the application to, and you can deploy the application to hosts in different development, testing and production environments as needed. Of course, it is also possible to deploy multiple instances in an environment that are isolated from each other.
Application template Management
On the application template page, you can modify the application template, and each modification of the template will result in a new version, which is convenient for upgrade and rollback.
Application management
On the page of the application instance, you can manage the application instance, expand and restart the application service.
Click the upgrade rollback button to quickly update the application to the specified version of the template
Application deployment automation
When the image is rebuilt, you can click the service redeploy button on the cSphere panel to upgrade the service, or you can use cSphere's API to upgrade automatically.
Before calling the API of cSphere, generate an API Key on the settings page of cSphere:
You can automatically upgrade the application by calling the following API
Curl-H 'cSphere-Api-Key: 6bbdc50dd0561b47ca8186f8ac29acde70bc65b3'-X PUT http://192.168.1.130/api/instances/php-mysql-example/redeploy, the study on "how to Docker the PHP project" is over. I hope to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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: 216
*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.