LoginSignup
3
0

More than 5 years have passed since last update.

【nginx】phpコンテナへアクセスする

Last updated at Posted at 2018-01-07

phpコンテナへアクセスしたいのですが、nginxの設定周りで困ったのでメモ。

docker-compose.ymlについて

docker-compose.yml
version: "3"
services:
    nginx_docker:
        image: nginx
        ports:
            - "8888:80"
        volumes:
            - ../web/public:/var/www/html
            - ./default.conf:/etc/nginx/conf.d/default.conf
        links:
            - php_docker

    php_docker:
        image: php:7-fpm
        volumes:
            - ../php/:/var/www/html

nginx_dockerのvolumesでは、フロントエンドのソースコードのマウント、default.confの上書きを行う。
php_dockerのvolumesではサーバーサイド側のソースコードのマウントを行う。

default.confについて

default.conf
server {
    listen 80;
    server_name localhost;

    root  /var/www/html;
    index index.php;

    access_log /var/log/nginx/access.log;
    error_log  /var/log/nginx/error.log;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(\.+)$;
        fastcgi_pass php_docker:9000; 
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

fastcgi_passの設定で困ったが、ここではdocker-compose.ymlでphpコンテナにつけた名前が入る。ただ、どこで名前解決されているんだろう。ということで名前解決を行ってみる。

(補足)名前解決を行う

digコマンドを使えるようにして、名前解決を行ってみる。

apt-get install dnsutils
dig php_docker

以下のようになった。
DNSサーバは127.0.0.11#53ということだ。

; <<>> DiG 9.10.3-P4-Debian <<>> php_docker
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 50076
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;php_docker.            IN  A

;; ANSWER SECTION:
php_docker.     600 IN  A   172.18.0.2

;; Query time: 13 msec
;; SERVER: 127.0.0.11#53(127.0.0.11)
;; WHEN: Sun Jan 07 00:58:11 UTC 2018
;; MSG SIZE  rcvd: 54

127.0.0.11について以下のページを見つけたためメモしておく。

Docker 内蔵 DNS サーバ
Docker デーモンは内蔵 DNS サーバを動かし、ユーザ定義ネットワーク上でコンテナがサービス・ディスカバリを自動的に行えるようにします。コンテナから名前解決のリクエストがあれば、内部 DNS サーバを第一に使います。リクエストがあっても内部 DNS サーバが名前解決できなければ、外部の DNS サーバにコンテナからのリクエストを転送します。割り当てできるのはコンテナの作成時だけです。内部 DNS サーバが到達可能なのは 127.0.0.11 のみであり、コンテナの resolv.conf に書かれます。ユーザ定義ネットワーク上の内部 DNS サーバに関しては ユーザ定義ネットワーク用の内部 DNS サーバ をご覧ください。
http://docs.docker.jp/engine/userguide/networking/dockernetworks.html

resolv.confに書かれているらしいので、nginxコンテナを確認してみると、

/etc/resolv.conf
nameserver 127.0.0.11
options ndots:0

参考文献

【linksとdepends_onがまとまっている】
https://qiita.com/mochizukikotaro/items/b398076cb57492980447
【nginx-php連携がわかりやすかった】
https://tech.recruit-mp.co.jp/infrastructure/post-12795/
【digするときに参考にした】
https://qiita.com/tsukapah/items/677b1f5c89dcbe520344#dns%E3%81%AB%E5%95%8F%E3%81%84%E5%90%88%E3%82%8F%E3%81%9B%E3%81%A6%E3%81%BF%E3%82%8B

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0