The third operation of system comprehensive practice

The third comprehensive system practice

1, Installation of docker compose

  • Download docker compose
    sudo curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  • Apply executable permissions to binaries
    sudo chmod +x /usr/local/bin/docker-compose
  • Create soft chain
    sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
  • Test whether the installation is successful
    sudo docker-compose --version


2, Dockerfile writing

1. Pull nginx image, php image and mysql image

2. Dockfile file writing

Create a docker? Compose folder, including the web folder and MySQL? Data folder
  • Create index.html in the web folder
    #index.html
    NGINX SUCCESS

  • Create index.php in the web folder
    #index.php
    <?php phpinfo();?>

Create and write dockerfile ﹣ mysql, dockerfile ﹣ PHP, default.conf, docker-compose.yml, dockerfile ﹣ nginx files
  • dockerfile_mysql
#base image 
FROM hub.c.163.com/library/mysql
#Set root password
ENV MYSQL_ROOT_PASSWORD 123
#Set up password free login
ENV MYSQL_ALLOW_EMPTY_PASSWORD no
  • dockerfile_php
FROM daocloud.io/library/php 
FROM daocloud.io/library/php 
RUN apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
    && docker-php-ext-install pdo_mysql \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd
  • default.conf
server {
    listen       2020;        #Modify mapping port
    server_name  localhost;

    location / {
        root   /web/html;     #Modify working directory
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
        root           /web/php;          #Modify working directory
        fastcgi_pass   myphp:9000;        #Change to container name
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; 
        include        fastcgi_params;
    }
}
  • docker-compose.yml
version: "3"
services:
 nginx:
    image: mynginx_image                #Specify image name
    container_name: mynginx             #Specify container name
    build:
      context: .
      dockerfile: dockerfile_nginx      #Specify the dockerfile file
    ports:
      - "80:2020"                       #Modify port mapping
    volumes:
      - ./web:/web/html                 #Mount the container volume. After modifying the file locally / in the container, the other party will update it synchronously;
      - ./default.conf:/etc/nginx/conf.d/default.conf     #Mount profile

 php:
    image: myphp_image 
    container_name: myphp 
    build:
      context: .
      dockerfile: dockerfile_php 
    environment:
      MYSQL_PASSWORD: 123456            #Set the environment variable, and use the variable name to reference directly in php code
    volumes:
      - ./web:/web/php                  #Mount working directory to local web directory

 mysql:
   image: mysql_image 
   container_name: mysql
   build:
      context: .
      dockerfile: dockerfile_mysql
   ports:
     - "3306:3306"
   volumes:
     - ./mysql_data:/var/lib/mysql       #Mount container volume to realize data synchronization and prevent data loss
  • dockerfile_nginx
#base image 
FROM daocloud.io/library/nginx 
#Declare exposed ports
EXPOSE 2020

3, Using Compose to realize multi container running mechanism

1. View the tree structure of the docker & compose folder

sudo apt install tree
cd docker-compose
tree



2. Execute docker compose file

sudo docker-compose up -d --build

3. View containers and images

4. Visit localhost/index.html and localhost/index.php

  • Access html

  • Visit php

4, Service Testing

1. Database connection

  • Modify the index.php file in the / docker compose / Web Directory
<?php
$servername = "mysql";  
$username = "root";
$password = "123";   
 
try {
    $conn = new PDO("mysql:host=$servername", $username, $password);
    echo "Connection successful"; 
}
catch(PDOException $e)
{
    echo $e->getMessage();
}
  • Visit localhost/index.php

2. Database NEW

  • Modify the index.php file
<?php
$servername = "mysql";
$username = "root";
$password = "123";
 
try {
    $conn = new PDO("mysql:host=$servername", $username, $password);
    echo "Connection successful"; 

    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "CREATE DATABASE myDB";

    // Use exec() because no results are returned
    $conn->exec($sql);

    echo "Database created successfully<br>";
}
catch(PDOException $e)
{
    echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>

  • Visit localhost/index.php

-Enter the mysql container, enter the database and query whether the database myDB exists
sudo docker exec -it mysql /bin/bash
mysql -u root -p
show databases;




3. Create a new table

  • Modify the index.php file
<?php
$servername = "mysql";
$username = "root";
$password = "123";
$dbname="myDB";        #Variable settings
 
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);    #Select database
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // Using sql to create data table
    $sql = "CREATE TABLE user(
    id INT(8) UNSIGNED PRIMARY KEY, 
    name VARCHAR(30) NOT NULL,
    age INT DEFAULT NULL
)";
    // Using exec(), no result returned 
    $conn->exec($sql);
    echo "data sheet user Created successfully";
}
catch(PDOException $e)
{
    echo $sql . "<br>" . $e->getMessage();
}
 
$conn = null;
?>
  • Visit localhost/index.php

  • Use myDB database and query data table
    use myDB
    show tables;



Insertion of table records

  • Modify the index.php file
<?php
$servername = "mysql";
$username = "root";
$password = "123";
$dbname="myDB";
 
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // Start transaction
    $conn->beginTransaction();
    // SQL statement
    $conn->exec("INSERT INTO user (id,name,age) 
    VALUES (071703428, 'ymq',21)");
    $conn->exec("INSERT INTO user (id,name,age) 
    VALUES (071703427, 'xjh',22)");
    $conn->exec("INSERT INTO user (id,name,age) 
    VALUES (071703429, 'aaa',23)");
 
    // Commit transaction
    $conn->commit();
    echo "New record inserted successfully!";
}
catch(PDOException $e)
{
    // Rollback if execution fails
    $conn->rollback();
    echo $sql . "<br>" . $e->getMessage();
}
 
$conn = null;
?>

  • query data base
    select * from user;


Modification of table records

  • Modify the index.php file
<?php
$servername = "mysql";
$username = "root";
$password = "123";
$dbname="myDB";
 
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // Start transaction
    $conn->beginTransaction();
    // SQL UPDATE statement
   $conn->exec("UPDATE user SET name='zyh' WHERE age='23'");
 
    // Commit transaction
    $conn->commit();
    echo "Record modified successfully!";
}
catch(PDOException $e)
{
    // Rollback if execution fails
    $conn->rollback();
    echo $sql . "<br>" . $e->getMessage();
}
 
$conn = null;
?>

Deletion of table records

  • Modify php file
<?php
$servername = "mysql";
$username = "root";
$password = "123";
$dbname="myDB";
 
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // Start transaction
    $conn->beginTransaction();
    // SQL DELETE statement
   $conn->exec("DELETE FROM user where id=071703429");
 
    // Commit transaction
    $conn->commit();
    echo "Record deleted successfully!";
}
catch(PDOException $e)
{
    // Rollback if execution fails
    $conn->rollback();
    echo $sql . "<br>" . $e->getMessage();
}
 
$conn = null;
?>

  • query data base

Add a phpmyadmin container to realize database management on the web side

  • Create a dockerfile phpMyAdmin file in the lnmp folder
#base image 
FROM phpmyadmin/phpmyadmin            
#Declare exposed ports
EXPOSE 80
  • Modify docker-compose.yml and insert the following code
phpmyadmin:
    image: myphpadmin_image
    container_name: myphpmyadmin
    build: 
        context: .
        dockerfile: dockerfile_phpmyadmin
    ports: 
        - "8080:80" # phpmyadmin default listening 80
    environment:
        PMA_HOST: mysql                     #Specify the host of mysql service
  • Execute the docker compose up - D -- build command again

  • Open localhost:8080

5, Summary

1. Because the image is bad, it takes a lot of unnecessary time to pull the image
2. When adding a phpmyadmin container to realize the database management on the web side, there are some problems in the php format


3. At the beginning, I was confused by the online tutorials, but I learned about the concept of docker compose using containers for microservice architecture

Tags: PHP Docker MySQL PDO

Posted on Fri, 08 May 2020 19:31:51 -0700 by v4g