假設機器上已經安裝好docker環境php
一.運行nginx容器html
1.查找Docker Hub 上的nginx 鏡像mysql
runoob@runoob:~/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.拉取官方鏡像nginx
runoob@runoob:~/nginx$ docker pull nginx
3.查看本地鏡像web
runoob@runoob:~/nginx$ docker images nginx REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 555bbd91e13c 3 days ago 182.8 MB
4.建立html靜態文件,運行nginx 容器sql
1)新建一個html
子目錄。docker
mkdir html
2)在這個子目錄裏面,放置一個index.html
文件,內容以下。apache
<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 了。ubuntu
mkdir /Users/playcrab/nginx-docker-demo cd /Users/playcrab/nginx-docker-demo
mkdir php
mkdir
5.配置nginx配置文件運行nginx容器,查看nginx是否起做用centos
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
2、運行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.瀏覽器訪問查看運行結果