docker-compose搭建nginx+php+mysql
转自: https://blog.csdn.net/miwumuge/article/details/83386679
安装Docker Compose之前应先确保系统已经安装了Docker
安装Docker Compose
1.下载Docker Compose
sudo curl -L “https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m)” -o /usr/local/bin/docker-compose
2.添加可执行权限
sudo chmod +x /usr/local/bin/docker-compose
3.测试
$ docker-compose –version
docker-compose version 1.22.0, build 1719ceb
搭建nginx+php+mysql
系统环境
CentOS minimal 7.2.1511
Docker CE 18.06.1-ce
docker镜像版本:
php 7.2.11-fpm
mysql 8.0
nginx 1.15
构建目录结构
www
├── docker-compose.yml
├── html
│ └── index.php
├── mysql
├── nginx
│ └── default.conf
└── php
└── Dockerfile
docker-compose.yml
version: ‘3.7’
services:
nginx:
image: nginx
ports:
– “80:80″
depends_on:
– php
volumes:
– “$PWD/nginx/default.conf:/etc/nginx/conf.d/default.conf”
– “$PWD/html:/usr/share/nginx/html”
networks:
– app_net
container_name: “compose-nginx”
php:
build: ./php
image: php:fpm
ports:
– “9000:9000″
volumes:
– “$PWD/html:/var/www/html”
networks:
– app_net
container_name: “compose-php”
mysql:
image: mysql
ports:
– “3306:3306″
environment:
– MYSQL_ROOT_PASSWORD=123456
networks:
app_net:
ipv4_address: 10.10.10.1
container_name: “compose-mysql”
networks:
app_net:
driver: bridge
ipam:
config:
– subnet: 10.10.0.0/16
html/index.php
phpinfo();
nginx/default.conf
server {
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 index.htm;
}
error_page 404 /404.html;
location = /40x.html {
root /usr/share/nginx/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
#
# PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI协议默认配置.
# Fastcgi服务器和程序(PHP,Python)沟通的协议.
location ~ \.php$ {
# php容器的网站根目录
root /var/www/html/;
# 设置监听端口
fastcgi_pass php:9000;
fastcgi_index index.php;
# 设置脚本文件请求的路径
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# 引入fastcgi的配置文件
include fastcgi_params;
}
# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
location ~ /\.ht {
deny all;
}
}
因为nginx镜像里没有fastcgi.conf文件,就需要引入fastcgi_params文件并配置SCRIPT_FILENAME参数
php/Dockerfile
FROM php:fpm
RUN apt-get update \
&& apt-get install -y iputils-ping \
&& docker-php-ext-install mysqli && docker-php-ext-enable mysqli
启动服务
# www目录下执行
docker-compose up -d
测试
浏览器访问http://localhost,会出现php的介绍文档
如果在虚拟机里搭建的项目,localhost要改成虚拟机的IP地址
遇到的问题
在docker-compose.yml文件所在目录执行
$ docker-compose up -d
报错:
ERROR: Couldn’t connect to Docker daemon at http+docker://localhost – is it running?
If it’s at a non-standard location, specify the URL with the DOCKER_HOST environment variable.
解决方法是把当前用户添加到docker用户组,然后重新登录即可
sudo usermod -aG docker ${USER}
参考网址
https://docs.docker.com/compose/install/
https://docs.docker.com/install/linux/docker-ce/centos/#os-requirements