安装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
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
docker-compose --version
编写Dockerfile
(1)、拉取镜像
(2)、建立文件夹docker_compose 里面有web和mysql_data子文件夹
(3)、编写文件
dockerfile_mysql
dockerfile_php
dockerfile_nginx
default.conf
docker-compose.yml
index.html
NGINX SUCCESS
index.php
<?php phpinfo();?>
使用compose实现多容器运行机制
sudo docker-compose up -d --build
访问localhost/index.php
测试
(1)、数据库连接测试,修改index.php
<?php
$servername = "mysql";
$username = "root";
$password = "123456";
try {
$conn = new PDO("mysql:host=$servername", $username, $password);
echo "连接成功";
}
catch(PDOException $e)
{
echo $e->getMessage();
}
(2)、新建数据库,修改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 myDB";
// 使用 exec() ,因为没有结果返回
$conn->exec($sql);
echo "数据库创建成功<br>";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>