1.完成Docker-compose的安装
由于是Mac os,安装docker时已经捆绑安装了docker-compose,用docker --version即可查看版本
2.dockerfile以及配置文件等
2.1基础文件建立
brew install tree
先安装好tree以后新建文件夹以及dockerfile等文件
mkdir docker-compose
touch docekr-compose.yml
touch dockerfile_mysql
touch dockerfile_php
touch dockerfile_nginx
mkdir web
mkdir mysql_data
touch default.conf
2.2编写文件内容
docker-compose.yml
version: "3"
services:
nginx:
image: mynginx_image
container_name: mynginx
build:
context: .
dockerfile: dockerfile_nginx #指定dockerfile文件
ports:
- "80:2020"
volumes:
- ./web:/web/html
- ./default.conf:/etc/nginx/conf.d/default.conf
php:
image: myphp_image
container_name: myphp
build:
context: .
dockerfile: dockerfile_php
environment:
MYSQL_PASSWORD: 123
volumes:
- ./web:/web/php
mysql:
image: mysql_image
container_name: mysql
build:
context: .
dockerfile: dockerfile_mysql
ports:
- "3306:3306"
volumes:
- ./mysql_data:/var/lib/mysql
docekrfile_nginx
FROM nginx
EXPOSE 2020
dockerfile_mysql
FROM mysql
ENV MYSQL_ROOT_PASSWORD 123
ENV MYSQL_ALLOW_EMPTY_PASSWORD no
dockerfile_php
FROM php:7.4-fpm
FROM php:7.4-fpm
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;
server_name localhost;
location / {
root /web/html;
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;
fastcgi_pass myphp:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
3.使用Compose实现多容器运行机制
docker-compose up -d
之前出现error是因为mysql的3306端口被占用,原因是本机打开了sqlserver服务,从本机设置里关闭sqlserver即可
4.修改php
<?php
$servername = "mysql_container";
$username = "root";
$password = "123456";
try {
$conn = new PDO("mysql:host=$servername;", $username, $password);
echo "连接成功";
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
5.数据库相关操作
<?php
$servername = "mysql";
$username = "root";
$password = "123";
try {
$conn = new PDO("mysql:host=$servername", $username, $password);
echo "连接成功";
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "CREATE DATABASE myDB";
$conn->exec($sql);
echo "数据库创建成功<br>";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
建表
<?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);
// 使用 sql 创建数据表
$sql = "CREATE TABLE user(
id INT(8) UNSIGNED PRIMARY KEY,
name VARCHAR(30) NOT NULL
)";
$conn->exec($sql);
echo "数据表 user 创建成功";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
向表中插入数据
<?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);
// 开始事务
$conn->beginTransaction();
// SQL 语句
$conn->exec("INSERT INTO user (id,name)
VALUES (031702145, 'mlz',)");
$conn->exec("INSERT INTO user (id,name)
VALUES (031702140, 'pwz',)");
// 提交事务
$conn->commit();
echo "新记录插入成功!";
}
catch(PDOException $e)
{
// 如果执行失败回滚
$conn->rollback();
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
6.小节
因为用的Mac os,很多东西感觉都和linux不一样,自己也在重新再linux上面复现,这次的实验感觉花时间的地方主要是在找不同,去适应mac下面的东西,网上的资源也比较少,很多错误也没办法及时的找到问题根源