目录结构
[root@pg0 ~]# tree my-images
my-images
├── conf
│ └── default.conf
├── docker-compose.yml
└── html
├── dbtest.php
├── index.html
└── index.php
文件内容
[root@pg0 ~]# cat my-images/conf/default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.php index.html ;
}
#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 /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /var/www/html;
fastcgi_pass my-php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
[root@pg0 ~]# cat my-images/html/dbtest.php
<?php
$conn=mysql_connect("mysql","root","123456");
if ($conn){
echo "it's ok!!!!!";
}else{
echo "it's error!!!!!!";
}
mysql_close();
?>
[root@pg0 ~]# cat my-images/docker-compose.yml
version: "3"
services:
my-nginx:
image: nginx:1.20.1
ports:
- 80:80
volumes:
- /root/my-images/conf.d/:/etc/nginx/conf.d/
- /root/my-images/html/:/usr/share/nginx/html/
depends_on:
- my-php
my-php:
image: php:5.6-fpm
ports:
- 9000:9000
volumes:
- /root/my-images/html/:/var/www/html/
depends_on:
- my-mysql
my-mysql:
image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=123456
运行
[root@pg0 my-images]# docker-compose up -d
Starting my-images_my-mysql_1 ... done
Starting my-images_my-php_1 ... done
Starting my-images_my-nginx_1 ... done
[root@pg0 my-images]# docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------------------------------
my-images_my-mysql_1 docker-entrypoint.sh mysqld Up 3306/tcp, 33060/tcp
my-images_my-nginx_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:80->80/tcp,:::80->80/tcp
my-images_my-php_1 docker-php-entrypoint php-fpm Up 0.0.0.0:9000->9000/tcp,:::9000->9000/tcp
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2c6de77b3b53f5b741c603b9cbd373a8.png)