完成Docker-compose的安装
dockerfile的编写
编写deault.conf配置文件
server {
listen 1106; #修改映射端口
server_name localhost;
location / {
root /web/html; #修改工作目录
index index.html index.html;
}
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;
}
}
nginx
FROM nginx
MAINTAINER lx
EXPOSE 1106
php
FROM php:7.4-fpm
MAINTAINER lx
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
mysql
FROM mysql
MAINTAINER lx
ENV MYSQL_ROOT_PASSWORD 123456
ENV MYSQL_ALLOW_EMPTY_PASSWORD no
phpmyadmin
FROM phpmyadmin/phpmyadmin
MAINTAINER lx
EXPOSE 8080
docker-compose的编写
version: "3"
services:
nginx:
image: mynginx_image #指定镜像名
container_name: mynginx #指定容器名
build:
context: .
dockerfile: dockerfile_nginx #指定dockerfile文件
ports:
- "80:1106" #修改端口映射
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: 123456 #设置好环境变量,在php代码中使用变量名直接引用
volumes:
- ./web:/web/php #挂载工作目录到本机web目录
mysql:
image: mysql_image
container_name: mysql
build:
context: .
dockerfile: dockerfile_mysql
ports:
- "3306:3306"
volumes:
- ./mysql_data:/var/lib/mysql #挂载容器卷,实现数据同步,防止数据丢失
phpmyadmin:
image: myphpmyadmin_image
container_name: myphpmyadmin
build:
context: .
dockerfile: dockerfile_phpmyadmin
ports:
- "8080:80" # phpmyadmin默认监听80
environment:
PMA_HOST: mysql #指定mysql服务所在的host
执行docker-compose文件
sudo docker-compose up -d --build
服务测试
测试php能不能连接mysql数据库
<?php
$servername = "mysql"; #注意这里要用自己自定义的Mysql的容器名
$username = "root";
$password = "123456"; #密码也可以直接使用yml中的变量名
try {
$conn = new PDO("mysql:host=$servername", $username, $password);
echo "连接成功";
}
catch(PDOException $e)
{
echo $e->getMessage();
}
在index.php文件中创建数据库
<?php
$servername = "mysql";
$username = "root";
$password = "123456";
try {
$conn = new PDO("mysql:host=$servername", $username, $password);
echo "连接成功";
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "CREATE DATABASE myDBPDO";
// 使用 exec() ,因为没有结果返回
$conn->exec($sql);
echo "数据库创建成功<br>";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
在myDBPDO数据库中创建Student表
<?php
$servername = "mysql";
$username = "root";
$password = "123456";
$dbname="dockerComposeDB";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); #选择数据库
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// 使用 sql 创建数据表
$sql = "CREATE TABLE student(
id int(4) not null primary key,
name varchar(16) not null
)";
// 使用 exec() ,没有结果返回
$conn->exec($sql);
echo "数据表 student 创建成功";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
向student表中插入数据
<?php
$servername = "mysql";
$username = "root";
$password = "123456";
$dbname="myDBPDO";
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 Student (id,name)
VALUES (123456789, 'lx')");
$conn->exec("INSERT INTO Student (id,name)
VALUES (123456780, 'll')");
$conn->exec("INSERT INTO Student (id,name)
VALUES (123456879, 'xx')");
// 提交事务
$conn->commit();
echo "新记录插入成功";
}
catch(PDOException $e)
{
// 如果执行失败回滚
$conn->rollback();
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
修改数据
<?php
$servername = "mysql";
$username = "root";
$password = "123456";
$dbname="myDBPDO";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// 开始事务
$conn->beginTransaction();
// SQL UPDATE 语句
$conn->exec("UPDATE Student SET name='llx' WHERE id=123456789");
// 提交事务
$conn->commit();
echo "修改成功";
}
catch(PDOException $e)
{
// 如果执行失败回滚
$conn->rollback();
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
删除数据
<?php
$servername = "mysql";
$username = "root";
$password = "123456";
$dbname="myDBPDO";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// 开始事务
$conn->beginTransaction();
//SQL DELETE语句
$conn->exec("DELETE FROM Student where id=123456789");
// 提交事务
$conn->commit();
echo "删除成功";
}
catch(PDOException $e)
{
// 如果执行失败回滚
$conn->rollback();
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
遇到的问题
1、
将docker-compose重新编写后解决
2、
这个实在没搞懂,干脆把nginx那块给重新搞了一下