1、创建
mkdir -p /docker/www
mkdir -p /docker/nginx/conf.d/
2、配置
vim nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
location ~ \.php(.*)$ {
root /var/www/html;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
3、创建 index.html,info.php,conn.php 三个文件
# html 静态文件
vim www/index.html
<h1>welcome to nginx</h1>
# php 文件
vim www/info.php
<?php
phpinfo();
?>
# php 连接 mysql
vim www/conn.php
<?php
$serve = 'mysql';
$username = 'root';
$password = '123456';
$dbname = 'mysql';
$mysqli = new Mysqli($serve,$username,$password,$dbname);
if ($mysqli->connect_error) {
die('connect error:'.$mysqli->connect_errno);
}
$mysqli->set_charset('UTF-8');
$result = $mysqli->query('select * from user');
$data = $result->fetch_all();
print_r($data);
?>
4、docker-compose.yml
version: "3"
services:
nginx:
image: nginx
container_name: nginx
ports:
- 80:80
volumes:
- /docker/www:/usr/share/nginx/html
- /docker/nginx/conf.d:/etc/nginx/conf.d
php:
image: bitnami/php-fpm:7.1
container_name: php-fpm
volumes:
- /docker/www:/var/www/html
mysql:
image: mysql:5.6
container_name: mysql
environment:
- MYSQL_ROOT_PASSWORD=123456
5、运行
cd /docker
[root@ ~docker]# ll
total 12
-rw-r--r-- 1 root root 419 Oct 20 22:17 docker-compose.yml
drwxr-xr-x 3 root root 4096 Oct 18 16:59 nginx
drwxr-xr-x 2 root root 4096 Oct 20 22:33 www
# 启动
docker-compose up -d
# 成功后,会自动创建一个 docker-compose.yml 所在目录名 + networks 名的 bridge
# 默认为 networks 名为 defalut
[root@ ~docker]# docker network ls
NETWORK ID NAME DRIVER SCOPE
f494d9e871fc bridge bridge local
2efd0aa32907 docker_default bridge local
0049c2e52fe2 host host local
d1e9a42f8a20 none null local
6、查看
[root@ ~docker]# docker ps
CONTAINER ID IMAGE COMMAND STATUS AMES
f4530f0204d7 bitnami/php-fpm:7.1 "php-fpm -F --pid ..." 9000/tcp php-fpm
f8defe11c1eb nginx "/docker-entrypoin..." 0.0.0.0:80->80/tcp nginx
8e36b1e0280e mysql:5.6 "docker-entrypoint..." 3306/tcp mysql