新手入门-20170803-Docker 下 LNMP 的搭建(分离各个组件)

Centos 7 上安装 Docker

yum -y install docker
service docker start

容器文件权限

使用docker run -v /data:/mnt:rw -i -t ubuntu ,但是在docker容器里文件只可以读不可以写

setenforce 0

容器里文件只可以读不可以写

Docker 下配置 nginx + php-fpm

报错: file not found

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.html index.htm;
    }

    #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   /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
    #
    location ~ \.php$ {
        root           /var/www/html; # wtf!!!!
        fastcgi_pass   172.17.0.6:9000;
        fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name; # wtf!!!!
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

nginx、php-fpm 与 docker

Docker run

Nginx 与 PHP 通过共享 host 的 ~/lamp/nginx/www 目录,分别挂载为 /www/var/www/html,当然,Nginx 上可以不挂载 /www

Design

# 目录结构
~/lamp
├── mysql
│   └── Dockerfile
├── nginx
│   ├── Dockerfile
│   └── www
│       ├── index.php
│       └── user.php
│   └── conf
│       ├── default.conf
│       └── nginx.conf
└── php
    └── Dockerfile

Process

# mysql
docker run -p 3306:3306 --name lamp-mysql -v $PWD/conf/my.cnf:/etc/mysql/my.cnf -v $PWD/logs:/logs -v $PWD/data:/mysql_data -e MYSQL_ROOT_PASSWORD=123456 -d lamp-mysql

# php-fpm
docker run -p 9000:9000 --name lamp-php -v ~/lamp/nginx/www:/var/www/html -v $PWD/conf:/usr/local/etc/php -v $PWD/logs:/phplogs -d lamp-php

# nginx
docker run -p 83:80 --name lamp-nginx -v $PWD/www:/www -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/wwwlogs -v $PWD/conf/default.conf:/etc/nginx/conf.d/default.conf -d lamp-nginx

# nginx 修正(以上可以省略 nginx:/www 的挂载)
docker run -p 83:80 --name lamp-nginx -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/wwwlogs -v $PWD/conf/default.conf:/etc/nginx/conf.d/default.conf -d lamp-nginx

Docker 实例

Result

docker ps

# Output
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
7852e9647a5f        lamp-mysql          "docker-entrypoint.sh"   11 seconds ago      Up 10 seconds       0.0.0.0:3306->3306/tcp   lamp-mysql
5c53da0cb2ba        lamp-nginx          "nginx -g 'daemon off"   24 minutes ago      Up 24 minutes       0.0.0.0:83->80/tcp       lamp-nginx
ed152be766db        lamp-php            "docker-php-entrypoin"   About an hour ago   Up About an hour    0.0.0.0:9000->9000/tcp   lamp-php

How To Connect them Only by Docker Components

docker compose 这些方法有点鸡肋,还是回头用 k8s 全部连起来好了。现在的问题是,上面的 docker run 还不够完善,不能互相平滑调用服务。

当然,以下这篇关于 docker compose 的博客写得还是不错的,mark 一下。

使用docker compose部署服务

在我做完以上的时候,终于!找到了一个完整的解决方案了,但是,坑我都踩完了。。mark 一下链接。

Docker多容器部署LNMP环境

最后,加个 link 实现互访就好了。

docker run 的最终版本

export LAMP=`pwd`

# mysql
cd $LAMP/mysql;docker run -p 3306:3306 --name lamp-mysql -v $PWD/conf/my.cnf:/etc/mysql/my.cnf -v $PWD/logs:/logs -v $PWD/data:/mysql_data -e MYSQL_ROOT_PASSWORD=123456 -d lamp-mysql

# php-fpm
cd $LAMP/php;docker run -p 9000:9000 --name lamp-php -v $LAMP/nginx/www:/var/www/html -v $PWD/logs:/phplogs --link lamp-mysql -d lamp-php

# nginx
cd $LAMP/nginx;docker run -p 83:80 --name lamp-nginx -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/wwwlogs -v $PWD/conf/default.conf:/etc/nginx/conf.d/default.conf --link lamp-php -d lamp-nginx

修改 $LAMP/nginx/conf/default.conf 如下:

location ~ \.php$ {
    root           /var/www/html;
    fastcgi_pass   lamp-php:9000;
    fastcgi_index  index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

验证结果

[root@centos nginx]# curl localhost:83/index.php
123[root@centos nginx]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                    NAMES
2579d927ac14        lamp-nginx          "nginx -g 'daemon off"   About a minute ago   Up 58 seconds       0.0.0.0:83->80/tcp       lamp-nginx
42ab9129b086        lamp-php            "docker-php-entrypoin"   About a minute ago   Up About a minute   0.0.0.0:9000->9000/tcp   lamp-php
4b49dee04b2d        lamp-mysql          "docker-entrypoint.sh"   About a minute ago   Up About a minute   0.0.0.0:3306->3306/tcp   lamp-mysql
# 验证 hosts 表
[root@centos nginx]# docker exec -it 2579d927ac14 /bin/bash
root@2579d927ac14:/# cat /etc/hosts
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.5  lamp-php 42ab9129b086
172.17.0.6  2579d927ac14

php 连接测试 mysql


try {
    $conn = new PDO('mysql:host=lamp-mysql;port=3306;dbname=mysql;charset=utf8', 'root', '123456');
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
$conn->exec('set names utf8');
$sql = "SELECT * FROM `user` WHERE 1";
$result = $conn->query($sql);
while($rows = $result->fetch(PDO::FETCH_ASSOC)) {
    echo $rows['Host'] . ' ' . $rows['User']."
\\n"
; } ?>

连接失败,感觉应该是有 connect 的组件没有安装。

Mark 一下,查找 php.ini 配置的方法。

  • 方法一:

phpinfo();
  • 方法二:
php -i|grep php.ini

如何查找php.ini文件?

php.ini 位置:/usr/local/etc/php

在 docker 容器中,执行下列语句,结果在容器内直接 php mysql.php 显示正常,但是利用 nginx url 访问就出现异常。判断在生成 image 时,直接安装组件应该就没问题了。

docker-php-ext-install pdo_mysql

基于Docker的PHP调用基于Docker的Mysql数据库

修改 Dockerfile 如下:

FROM php:5.6-fpm
RUN docker-php-ext-install pdo_mysql mysqli

Git

1-11111 / docker-lnmp
http://git.oschina.net/zhoufen111111/docker-lnmp

你可能感兴趣的:(docker,云计算,中间件)