docker 部署nginx、php-fpm

假设机器上已经安装好docker环境

一.运行nginx容器

1.查找Docker Hub 上的nginx 镜像

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.拉取官方镜像

runoob@runoob:~/nginx$ docker pull nginx

3.查看本地镜像

runoob@runoob:~/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.浏览器访问查看运行结果

  

猜你喜欢

转载自www.cnblogs.com/zxqblogrecord/p/9889493.html