https://blog.csdn.net/u010953609/article/details/89337660php
1- 單獨創建一個 compose 學習的目錄 $ tree lnmp2 -d lnmp2 ├── mysql │ └── data │ ├── mysql │ ├── performance_schema │ └── sys ├── nginx │ ├── conf.d │ └── www │ └── html └── phpfpmhtml
2- 編寫 Dockerfilemysql
nginx 的 Dockerfile 只有一條 ~/compose/lnmp2$ cat ./nginx/Dockerfile FROM nginx:latestlinux
php 的 Dockerfile ** 我是從以前學習 Dockerfile 的抄寫過來的,裏面大量的 php 擴展 學習時能夠去掉後面的那些 也能夠只有一條nginx
~/compose/lnmp2$ cat ./phpfpm/Dockerfilegit
FROM php:5.6.37-fpm-alpine3.7 RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \ && apk update\ && apk add --no-cache libmcrypt-dev freetype-dev libjpeg-turbo-dev \ git \ # libfreetype6-dev \ # libjpeg62-turbo-dev \ libpng-dev \ && docker-php-ext-install mcrypt mysqli pdo pdo_mysql mbstring bcmath zip opcache\ && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/ \ && docker-php-ext-install -j$(nproc) gd
mysql 的 Dockerfile 實際也只有一條 ~/compose/lnmp2$ cat ./mysql/Dockerfile FROM mysql:5.7web
分別測試 nginx、php、mysql 鏡像和容器運行 只須要測試 image 和 docker run 能夠正確運行便可,詳細配置放在 compose 模版裏面sql
編寫模版 ss -tlnp |grep 3306 ss -tlnp |grep 82 確認好可使用的端口,本次我使用 3308 和 82 端口來分別對應 mysql 和 nginxdocker
~/compose/lnmp2$ cat docker-compose.ymlvim
version: "3" services: web_server: build: ./nginx container_name: nginx links: - php:php57 ports: - "82:80" - "443:443" volumes: - ./nginx/www/html:/var/www/html - ./nginx/conf.d:/etc/nginx/conf.d db_server: build: ./mysql container_name: mysql57 environment: MYSQL_ROOT_PASSWORD: 123456 ports: - "3308:3306" volumes: - ./mysql/data:/var/lib/mysql php: build: ./phpfpm container_name: php72 links: - db_server:mysql57 ports: - "9000:9000" volumes: - ./nginx/www/html:/var/www/html - ./nginx/conf.d:/etc/nginx/conf.d
運行測試 ** 爲了看得清晰,能夠將以前學習的項目都 down 掉以後進行 由於運行時會產生新的 images 和新的 docker 容器 docker-compose down
在當前目錄下 ~/compose/lnmp2$ docker-compose up -d
Creating network "lnmp2_default" with the default driver Creating mysql57 ... done Creating php72 ... done Creating nginx ... done
測試
curl 127.0.0.1:82 http://192.168.1.193:82/
第一次 up 以後,檢查運行的容器,發現只有 2 個 ,nginx 容器沒有起來!
docker ps -s CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE d07a05bdc966 lnmp2_php "docker-php-entrypoi…" 26 seconds ago Up 25 seconds 0.0.0.0:9000->9000/tcp php72 0B (virtual 77.4MB) 9018c7da0706 lnmp2_db_server "docker-entrypoint.s…" 27 seconds ago Up 25 seconds 33060/tcp, 0.0.0.0:3308->3306/tcp mysql57 4B (virtual 372MB)
查看 nginx 容器日誌 docker logs nginx
2019/04/17 01:47:18 [emerg] 1#1: invalid number of arguments in "fastcgi_pass" directive in /etc/nginx/conf.d/default.conf:23 nginx: [emerg] invalid number of arguments in "fastcgi_pass" directive in /etc/nginx/conf.d/default.conf:23
** 由於我是從前一天的 lnmp 直接 cp 過來目錄和 conf.d 的 default.conf 裏面的 php 容器名字改變了,修改後又忘記了後面分號
** 修改 default.conf 以後,直接啓動 nginx 容器便可,無需再次 up 如下是本次的 nginx 配置文件內容
~/compose/lnmp2$ cat ./nginx/conf.d/default.conf
server { listen 80; server_name localhost; location / { root /var/www/html; index index.html index.htm index.php; } #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 /var/www/html; } location ~ \.php$ { root /var/www/html; # fastcgi_pass 127.0.0.1:9000; # 必須修改成compose模版文件中php容器名字 fastcgi_pass php72:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # 修改成$document_root include fastcgi_params; } }