docker-compose搭建nginx+php+mysql
阿新 • • 發佈:2018-11-06
安裝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
<?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}