FROM mysql #MAINTAINER IFORMATION MAINTAINER jayer@xiajibaxie.com
FROM nginx #MAINTAINER IFORMATION MAINTAINER jayer@xiajibaxie.com
FROM php:7.4-fpm #MAINTAINER IFORMATION MAINTAINER jayer@xiajibaxie.com
#Configure PHP Core Extensions RUN apt-get update && apt-get install -y \ libfreetype6-dev \ libjpeg62-turbo-dev \ libpng-dev \ && docker-php-ext-configure gd --with-freetype --with-jpeg \ && docker-php-ext-install -j$(nproc) gd
server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /usr/nginx-2/html; index index.html index.htm index.php; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root /usr/nginx-2/php; #php容器下的工做路徑 fastcgi_pass lnmp_php-container:9000; #container name fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
其中重點在於加入location ~ \.php${}的匹配信息(大體瞭解就是nginx在匹配路徑文件時,若是後綴是php文件,即向容器端口去發送請求,從而獲得運行php容器中相應路徑下的index.php文件)php
同時瞭解PHP的也應該知道,該腳本語言與HTML是兼容的。html
前面3個root、fastcgi_pass、fastcgi_index容易理解,後面兩個就不明白爲啥了(直接寫上就完事)mysql
version: "3" services: nginx: image: lnmp_nginx container_name: lnmp_nginx-container build: ./nginx ports: - "80:80" links: - "php" volumes: - ./nginx/html/:/usr/nginx-2/html/ - ./nginx/default.conf:/etc/nginx/conf.d/default.conf php: image: lnmp_php container_name: lnmp_php-container build: ./php ports: - "9000:9000" volumes: - ./php/phpfile/:/usr/nginx-2/php/ links: - "mysql" stdin_open: true tty: true mysql: image: lnmp_mysql container_name: lnmp_mysql-container build: ./mysql ports: - "3306:3306" volumes: - ./mysql/data/:/var/lib/mysql/ environment: MYSQL_ROOT_PASSWORD : 123
這裏用到的幾個基本的就是,image和container_name定義鏡像名與容器名,build相對路徑下尋找Dockerfile定製鏡像,ports端口映射。nginx
volumes掛載數據卷或環境(稍微瞭解一下就是將主機的某個文件或路徑補充到容器下的文件路徑以後,所體現就是造成同一文件環境,主機內修改文件信息可永久性反饋到容器內)web
...sql
構建成功docker
<?php phpinfo();?>
說明端口訪問PHP環境成功shell
搭建完以後PHP沒有mysqli,有pdo但不支持mysql,因而就去安裝一下唄數據庫
寫index.php執行php文件,這裏直接套用菜鳥教程上的代碼框架
<?php $servername = "lnmp_mysql-container"; $username = "root"; $password = "123"; // 建立鏈接 $conn = new mysqli($servername, $username, $password); // 檢測鏈接 if ($conn->connect_error) { die("鏈接失敗: " . $conn->connect_error); } echo "鏈接成功"; // 建立數據庫 $sql = "CREATE DATABASE myDB"; if ($conn->query($sql) === TRUE) { echo "數據庫建立成功"; } else { echo "Error creating database: " . $conn->error; } // 使用 sql 建立數據表 $sql = "CREATE TABLE MyGuests ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, firstname VARCHAR(30) NOT NULL, lastname VARCHAR(30) NOT NULL, email VARCHAR(50), reg_date TIMESTAMP )"; if ($conn->query($sql) === TRUE) { echo "Table MyGuests created successfully"; } else { echo "建立數據表錯誤: " . $conn->error; } //插入數據 $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')"; if ($conn->query($sql) === TRUE) { echo "新記錄插入成功"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } ?>
結果: 忘記還有dbname變量來選中數據庫,略微尷尬。
改:
<?php $servername = "lnmp_mysql-container"; $username = "root"; $password = "123"; $dbname = "myDB"; // 建立連接 $conn = new mysqli($servername, $username, $password, $dbname); // 檢查連接 if ($conn->connect_error) { die("鏈接失敗: " . $conn->connect_error); } // 使用 sql 建立數據表 $sql = "CREATE TABLE MyGuests ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, firstname VARCHAR(30) NOT NULL, lastname VARCHAR(30) NOT NULL, email VARCHAR(50), reg_date TIMESTAMP )"; if ($conn->query($sql) === TRUE) { echo "Table MyGuests created successfully\n"; } else { echo "建立數據表錯誤: " . $conn->error; } //插入數據 $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com');"; $sql .= "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('Mary', 'Moe', 'mary@example.com');"; $sql .= "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('Julie', 'Dooley', 'julie@example.com')"; if ($conn->multi_query($sql) === TRUE) { echo "新記錄插入成功\n"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } ?>
進入mysql容器查看結果
查看數據:
$sql = "SELECT id, firstname, lastname FROM MyGuests"; $result = $conn->query($sql); if ($result->num_rows > 0) { // 輸出數據 while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>"; } } else { echo "0 結果"; }
刪除數據:
mysqli_query($conn,"DELETE FROM MyGuests WHERE LastName='Dooley'");
經過docker-compose鏈接一個phpmyadmin容器,在docker-compose.yml末尾添上以下代碼,執行docker-compose up -d;
phpmyadmin: image: lnmp_pmadmin container_name: lnmp_pmadmin-container build: ./phpmyadmin ports: - "9090:80" links: - "mysql" environment: - PMA_ARBITRARY=1
web端口進入phpmyadmin
有圖形操做界面就感受很能夠。