完成Docker-compose的安装
- 换源
curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.5/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
- 修改目录权限,否则后续docker-compose up命令会报错
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
docker-compose --version
Dockerfile编写
- 使用docker pull 下载各自的镜像
sudo docker pull php:5.6-fpm
创建如下各个文件
html
#index.html
welcome to jasonginx
#index.php
<?php phpinfo();?>
mysql
#Dockerfile
#基础镜像
FROM mysql:5.7
#维护者信息
MAINTAINER jaso
nginx
#Dockerfile
# Base images 基础镜像
FROM nginx:latest
#MAINTAINER 维护者信息
MAINTAINER jaso
#default.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html index.htm index.php; # 增加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 /var/www/html;
}
location ~ .php$ {
root /var/www/html; # 代码目录
fastcgi_pass phpfpm:9000; # 修改为phpfpm容器
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # 修改为$document_root
include fastcgi_params;
}
}
phpfpm
#Dockerfile
FROM php:5.6-fpm
RUN apt-get update && apt-get install -y
libfreetype6-dev
libjpeg62-turbo-dev
libpng12*-dev
vim
&& docker-php-ext-install pdo_mysql
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
&& docker-php-ext-install gd
使用Compose实现多容器运行机制
docker-compose.yml
version: "3"
services:
nginx:
build: ./nginx
ports:
- "80:80"
links:
- "phpfpm"
volumes:
- ./html/:/var/www/html/
- ./nginx/conf/default.conf:/etc/nginx/conf.d/default.conf
phpfpm:
build: ./phpfpm
ports:
- "9000:9000"
volumes:
- ./html/:/var/www/html/
links:
- "mysql"
mysql:
build: ./mysql
ports:
- "3306:3306"
volumes:
- ./mysql/data/:/var/lib/mysql/
environment:
MYSQL_ROOT_PASSWORD : 123456
- 在compose文件夹打开终端
docker-compose up -d
- 构建成功
服务测试
测试访问index.html和index.php,
- PDO安装成功
- 连接mysql
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;", $username, $password);
echo "连接成功";
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
- 创建数据库
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername", $username, $password);
// 设置 PDO 错误模式为异常
$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;
?>
- 用终端进入mysql容器,查看数据库是否创建成功
docker exec -it compose_mysql_1 /bin/bash
mysql -u root -p
show databases;
- 在数据库内建表
<?php
$servername = "192.168.10.139";
$username = "root";
$password = "123456";
$dbname = "myDBPDO";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// 设置 PDO 错误模式,用于抛出异常
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// 使用 sql 创建数据表
$sql = "CREATE TABLE palyer (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
// 使用 exec() ,没有结果返回
$conn->exec($sql);
echo "数据表 player 创建成功";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
use myDBPDO
show tables;
- 插入数据
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// 设置 PDO 错误模式,用于抛出异常
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO player(firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
// 使用 exec() ,没有结果返回
$conn->exec($sql);
echo "新记录插入成功";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
select * from player;
- 修改数据
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// 开始事务
$conn->beginTransaction();
// SQL 语句
$conn->exec("INSERT INTO player (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')");
$conn->exec("INSERT INTO player (firstname, lastname, email)
VALUES ('Mary', 'Moe', 'mary@example.com')");
$conn->exec("INSERT INTO player (firstname, lastname, email)
VALUES ('Julie', 'Dooley', 'julie@example.com')");
// 提交事务
$conn->commit();
echo "修改成功";
}
catch(PDOException $e)
{
// 如果执行失败回滚
$conn->rollback();
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
增加phpmyadmin容器(选做)
- pull phpmyadmin
docker pull phpmyadmin/phpmyadmin
- 在文件夹compose中创建以下文件
Dockerfile
#Dockerfile
FROM phpmyadmin/phpmyadmin:latest
在docker-compose.yml文件末端加上
phpmyadmin:
build: ./phpmyadmin
container_name: "phpmyadmin"
ports:
- "9090:80"
links:
- "mysql"
environment:
- PMA_ARBITRARY=1
- 重构docker-compose
- 在web端使用phpmyadmin,用自己数据库的账号密码进入
- 插入数据
- 使用phpmyadmin查询
小结
- docker-compose官方安装速度太慢,换源轻松起飞
- 配置文件路径未改正,浏览器访问404
- ubuntu悄悄断网(中国农村联不通),数据库断连,还再不停地研究语法错误,浪费了很多时间
- build过程中apt-get update更新很慢,试了几个网上常见的加速方式也不见效,无奈挂机慢慢下
- 跟着大佬的步骤加上自己的一些理解,学起来会比较顺,简单学习了docker-compose的使用、php连接MySQL、docker-compose.yml编写,但是仍有很多东西没摸透,博客写完得再捋捋
- 用时:加起来差不多一天半