On what is Docker, it is recommended that you check the relevant usage on the Internet first. If you do not understand, in this article, you can simply understand that he is a lightweight virtual machine.
First, docker install MySQL
First, we pull a MySQL image from the warehouse.
Docker Pull mysql:5.6
Then we can view the image of MySQL we just pulled down by command Docker images
Next, we'll start running and start a container with the following command
Docker run-d-P 3307:3306-e mysql_root_password=xy123456--name xy_mysql mysql:5.6
parameter Description
-D Let the container run in the background
-P adding host-to-container port mappings
-e Set the environment variable, here is the initial password to set the root user of MySQL, this must be set
–name the name of the container, whatever it takes, but must be unique
PS: Actually we can just use the Docker Run command. The Docker run will first pull and then create. Personal habits pull the mirror down first, and it will be fast in run.
Next we'll be able to view the container we just created by ordering Docker Ps-a
Here we can see the up state of my container state, indicating that the container is running, and that you can see the port mappings between the host and the container.
Next, we can go into the container we just created, enter the command
Docker exec-it Xy_mysql/bin/bash
parameter Description
-T produces a pseudo terminal in a container
-I interacts with standard input (STDIN) within the container
There is no vim in the container by default, so we first need to install VIM, it is important to remember to install the APT Update command before installation, otherwise there will be problems.
After entering the MySQL container, we create a remote accessible user so that we can access our database from another host.
Second, Docker installation PHP-FPM
Also first we pull the php-fpm image
Docker Pull PHP:7.0-FPM
Create a PHPFPM container again
parameter Description
-D Let the container run in the background
-P adding host-to-container port mappings
-V Adds a directory map, that is, the/var/nginx/www/html on the host and the/var/www/html directory in the container is synchronous
The name of the –name container
–link is linked to another container so that we can use the service in the other container in the current container.
Here if you do not specify the –link parameter is actually available, because the easy itself is also IP and unique, so we can also directly use IP to access the container.
Then go into our container and then we create a new index.php file in the/var/www/html directory
Touch index.php
We can see a new PHP file in this directory
Next we go back to our host and visit our host on the/var/nginx/www/html
We found that the newly created files in the/var/www/html directory in the container are also in the/var/nginx/www/html directory of the host, because we have attached the directories in the host to the container when we create the container.
Since I'm going to test with the PDO module later, I need to install the Pdo_mysql module myself, which can be installed in a Docker container.
Docker-php-ext-install Pdo_mysql
Then we can view all of our PHP extensions via command php-m, and we can go to see the pdo_mysql extension we just installed.
third, Docker installation Nginx
First, we pull a nginx image from the warehouse.
Docker Pull nginx:1.10.3
Next, run the Nginx container
Docker run-d-P 80:80-v/var/nginx/www/html:/var/www/html--link xy_phpfpm:phpfpm--name Xy_nginx nginx:1.10.3
Parameter description:
-D Let the container run in the background
-P adding host-to-container port mappings
-V Add directory mapping, it is best that the root directory of Nginx container is best written as the root directory in PHP container. But do not have to be the same, if not the same when configuring Nginx need to pay attention to
The name of the –name container
–link in connection with another container
Then go into the Nginx container and modify the Nginx configuration file so that it supports PHP
Docker Exec-ti Xy_nginx/bin/bash
parameter Description
-T produces a pseudo terminal in a container
-I interacts with standard input (STDIN) within the container
Find nginx config file in container, default is in/etc/nginx directory
Location ~ \.php$ {root /var/www/html;fastcgi_index index.php;fastcgi_pass phpfpm:9000;// This is changed to the container we--link in before, or directly with the PHP container's ipfastcgi_param script_filename $document _root$fastcdi_script_name;// If your root directory is not the same as the root directory of the PHP container, here the $document_root need to be replaced by your PHP root directory, or PHP will not find the file include fastcgi_params; }
Finally, let's test if our installation was successful.
<?phptry { $con = new PDO (' Mysql:host=mysql;dbname=test ', ' root ', ' xy123456 '); $con->query (' SET NAMES UTF8 '); $res = $con->query (' SELECT * from test '); while ($row = $res->fetch (PDO::FETCH_ASSOC)) { echo "id:{$row [' ID ']} name:{$row [' name ']}\n '; }} catch ( Pdoexception $e) { echo ' Error reason: ' . $e->getmessage ();}
When when, see the right output, it proves that our configuration was successful. One of the most basic environments is built. Isn't it simple?
I don't know if anyone has noticed me in the test code.
$con = new PDO (' Mysql:host=mysql;dbname=test ', ' root ', ' xy123456 ');
In this line, I did not put the MySQL container link in the new container, the host I directly with MySQL can also succeed, why? Because the actual implementation of this code is the PHP container, (if you do not know the relationship between Nginx and PHP, it is best to check the data on the Internet to find out) and before we put the PHP container link in the PHP container, so here is feasible, the current switch to the MySQL container IP is the same, Information about the container can be viewed through the dokcer inspect Contanier_name|id, but only the container's IP is used inside the intranet. If you want to access MySQL in the container outside the network, or through the host's public network ip:port this form to access.
View IP:
Docker Inspect--format ' {{. Networksettings.ipaddress}} ' <container-ID>
Build database:
CREATE DATABASE IF not EXISTS test default charset UTF8 COLLATE utf8_general_ci; CREATE TABLE test (ID int NOT NULL auto_increment,name varchar (a) NOT null default ' ', Constraint Pk_test primary key (ID)) ;
Build LNMP Environment (RPM) based on Docker