docker 部署nginx、php-fpm
假設機器上已經安裝好docker環境
一.執行nginx容器
1.查詢Docker Hub 上的nginx 映象
[email protected]:~/nginx$ docker search nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 3260 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker c... 674 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable ... 207 [OK] million12/nginx-php Nginx + PHP-FPM 5.5, 5.6, 7.0 (NG), CentOS... 67 [OK] maxexcloo/nginx-php Docker framework container with Nginx and ... 57 [OK] webdevops/php-nginx Nginx with PHP-FPM 39 [OK] h3nrik/nginx-ldap NGINX web server with LDAP/AD, SSL and pro... 27 [OK] bitnami/nginx Bitnami nginx Docker Image 19 [OK] maxexcloo/nginx Docker framework container with Nginx inst... 7 [OK] ...
2.拉取官方映象
[email protected]:~/nginx$ docker pull nginx
3.檢視本地映象
[email protected]:~/nginx$ docker images nginx REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 555bbd91e13c 3 days ago 182.8 MB
4.建立html靜態檔案,執行nginx 容器
1)新建一個html
子目錄。
mkdir html
2)在這個子目錄裡面,放置一個index.html
檔案,內容如下。
<h1>Hello World</h1>
docker container run \ -d \ -p 80:80 \ --rm \ --name mynginx \ --volume "$PWD/html":/usr/share/nginx/html \ nginx
3)開啟瀏覽器訪問127.0.0.1,應該就能看到 Hello World 了。
mkdir /Users/playcrab/nginx-docker-demo cd /Users/playcrab/nginx-docker-demo
mkdir php
mkdir
5.配置nginx配置檔案執行nginx容器,檢視nginx是否起作用
1)把容器裡面的 Nginx 配置檔案拷貝到本地。
$ docker container cp mynginx:/etc/nginx .
說明:上面命令的含義是,把mynginx
容器的/etc/nginx
拷貝到當前目錄。不要漏掉最後那個點。執行完成後,當前目錄應該多出一個nginx
子目錄。然後,把這個子目錄改名為conf
。
2)執行完成後,當前目錄應該多出一個nginx
子目錄。然後,把這個子目錄改名為conf
。
$ mv nginx conf
3)進入配置檔案目錄編輯本地配置檔案,此檔案會被對映到容器的對應目錄裡
server { listen 80; server_name localhost; location / { root /usr/share/nginx/html;//容器的目錄 index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html;//容器的靜態檔案目錄 } }
4)cd /Users/playcrab/nginx-docker-demo,執行如下命令
docker container run \ -d \ -p 80:80 \ --rm \ --name mynginx \ --volume "$PWD/html":/usr/share/nginx/html \ --volume "$PWD/conf":/etc/nginx\ nginx
ps:$PWD是當前目錄的意思
在瀏覽器檢視127.0.0.1可以正常訪問
6.配置與php容器通訊的nginx容器
1)編輯docker.conf配置檔案
server { listen 80; server_name docker.com; location / { root /usr/share/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { fastcgi_pass php:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name; include fastcgi_params; } }
ps: /var/www/html/ 容器中的php檔案目錄,會在執行php-fpm容器時用到
2)執行如下命令
docker container run \ --rm \ --name mynginx \ --volume "$PWD/html":/usr/share/nginx/html \ --volume "$PWD/conf":/etc/nginx \ -p 80:80 \ --link myphp-fpm:php \ -d \ nginx
ps:
--link myphp-fpm:php 把 myphp-fpm 的網路併入 mynginx,並通過修改 mynginx 的 /etc/hosts,把域名 php 對映成 127.0.0.1,讓 nginx 通過 php:9000 訪問 php-fpm。
3)配置本地host檔案,加入下面程式碼
127.0.0.1 docker.com
二、執行php容器
1.查詢Docker Hub 上的php 映象
zhangxueqing:~ playcrab$ docker search php NAME DESCRIPTION STARS OFFICIAL AUTOMATED php While designed for web development, the PHP … 3987 [OK] phpmyadmin/phpmyadmin A web interface for MySQL and MariaDB. 642 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 634 [OK] composer Composer is a dependency manager written in … 292 [OK] adminer Database management in a single PHP file. 180 [OK] php-zendserver Zend Server - the integrated PHP application… 155 [OK] webdevops/php-nginx Nginx with PHP-FPM 117 [OK] webdevops/php-apache-dev PHP with Apache for Development (eg. with xd… 82 [OK] webdevops/php-apache Apache with PHP-FPM (based on webdevops/php) 74 [OK] phpunit/phpunit PHPUnit is a programmer-oriented testing fra… 63 [OK] bitnami/php-fpm Bitnami PHP-FPM Docker Image 61 [OK] nazarpc/phpmyadmin phpMyAdmin as Docker container, based on off… 58 [OK] 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 47 [OK] tetraweb/php PHP 5.5, 5.6, 7.0, 7.1 for CI and running te… 30 [OK] wodby/drupal-php PHP for Drupal 25 [OK] circleci/php CircleCI images for PHP 20 centos/php-56-centos7 Platform for building and running PHP 5.6 ap… 16 clinta/phpipam phpIPAM web IP address management applicatio… 16 [OK] antage/apache2-php5 Docker image for running Apache 2.x with PHP… 11 [OK] graze/php-alpine Smallish php7 alpine image with some common … 11 [OK] 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mariadb-10 ubuntu-16-nginx-php-phpmyadmin-mariadb-10 9 [OK] appsvc/php Azure App Service php dockerfiles 6 [OK] phpspec/phpspec PHPSpec: A php toolset to drive emergent des… 5 [OK] lephare/php PHP container 4 [OK] isotopab/php Docker PHP 0 [OK]
2.拉取官方映象,標籤為phpdockerio/php71-fpm
zhangxueqing:~ playcrab$ docker pull phpdockerio/php71-fpm
3.本地建立php目錄,建立index.php檔案,檔案內容如下
<?php echo phpinfo();
4執行容器
docker run -p 9000:9000 --name myphp-fpm -v "$PWD/php":/var/www/html -d phpdockerio/php71-fpm
6.瀏覽器訪問檢視執行結果