LoginSignup
9
9

More than 5 years have passed since last update.

Proxy配下のCoreOS上でPHP5.6+nginxのDockerを作ってみる

Posted at

PHP5.6+nginxのDockerを作ってみたくてやってみたのでメモ。ホストとしてはCoreOSも使ってみた

参考

CoreOSの起動

vagrantを使っての起動

下記のリンクの通り実行してCoreOSのVMをローカル環境に構すればOKです。

coreos/coreos-vagrant

$git clone https://github.com/coreos/coreos-vagrant/
$cd coreos-vagrant
$vagrant up

EC2を使っての起動

下記に各リージョンで使えるCoreOSのAMIのIDが記されされいてるのでそれを利用して起動します、

Running CoreOS on EC2

SSHログインのユーザー名は core という名前のようです。

$ssh core@11.11.11.11 -i ~/.ssh/HofeFuga.pem

Proxy配下での利用

docker pullなど実行時にProxyを利用できるように設定します。
以下をCoreOSにSSH接続してから設定します。

Customizing docker

$vagrant ssh
CoreOS alpha (766.0.0)
$sudo mkdir /etc/systemd/system/docker.service.d
$sudo vi
/etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://proxy.example.com:8080"
$systemctl daemon-reload
$systemctl restart docker

Ubuntuのコンテナを起動してみる

Ubuntuのコンテナを起動してみます。

まず、ubuntuのイメージを取得します。

$docker pull ubuntu
latest: Pulling from ubuntu
6071b4945dcf: Pull complete
5bff21ba5409: Pull complete
e5855facec0b: Pull complete
8251da35e7a7: Already exists
ubuntu:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
Digest: sha256:946dd32914a9b315d45e1983b75a597eb931cc59c179402d4fbc7d1e1dd8129d
Status: Downloaded newer image for ubuntu:latest

確認してみます。

$docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu              latest              8251da35e7a7        2 days ago          188.3 MB

取得の確認まで出来たので、次にコンテナ内でbashコマンドを実行するコンテナの作成、実行を行います。実行時に-itオプションを指定することでフォアグランドでの実行としています。

$docker run -it ubuntu:latest /bin/bash
root@986b3770e0ab:/#cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.2 LTS"
root@986b3770e0ab:/# exit
exit

実行後、/etc/lsb-releaseを表示することでコンテナのOSがUbuntu14.02であることが分かりました。

bashコマンドをexitで終了したのでコンテナは停止した状態となっており、docker psコマンドでは表示されません。

$docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

ただし、停止したコンテナもまだディスクデータが残っているのでdocker rmコマンドで終了させます。なお、停止したコンテナも表示する場合、docker psコマンドに-aオプションを追加します。

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                      PORTS               NAMES
986b3770e0ab        ubuntu:latest       "/bin/bash"         About a minute ago   Exited (0) 21 seconds ago                       ecstatic_brown

$docker rm 986b3770e0ab        
986b3770e0ab

なお、Proxy配下でDockerfile内でapt-getなど利用する場合、以下設定が必要です。

$ docker run -e http_proxy=http://proxy.example.com:8080/ -it ubuntu:latest /bin/bash

Dockerfileを使ってnginxを動かす

次にDockerfileを使ってnginxを動かしてみます。

ベースとなるイメージは先ほど取得したubuntuを利用します。
普通にnginxを起動するとデーモンとして起動してdockerが終了してしまうので、設定ファイルに deamon off; を追記してフォアグラウンドで実行するようにしています。

$mkdir nginx
$cd nginx
$vi Dockerfile
Dockerfile
FROM ubuntu:latest

MAINTAINER toshihirock

RUN apt-get update
RUN apt-get install nginx -y
RUN echo "daemon off;" >> /etc/nginx/nginx.conf

EXPOSE 80

CMD service nginx start

Dockerfileが出来たのでビルドしてイメージを作成します。

$docker build -t toshihirock/nginx_on_ubuntu:latest .
・・・
Successfully built f6ffa26e7cc9

確認します。

$docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
toshihirock/nginx_on_ubuntu   latest              e6f854d9f413        4 minutes ago       206.5 MB
ubuntu                        latest              63e3c10217b8        2 days ago          188.3 MB

無事作成できたようなので実行させてみます。-dオプションを利用してバックグラウンドでコンテナを実行させています。

$docker run -d toshihirock/nginx_on_ubuntu
6d9ee6990de8d58b29ea26b5667e194d0fa546e9366b75a9254e29cbf4d6c4eb

無事起動できたら確認してみます。バックグラウンドで実行されているはずなので、-aオプションはつけず、確認します。

$docker ps
CONTAINER ID        IMAGE                                COMMAND                CREATED              STATUS              PORTS               NAMES
6d9ee6990de8        toshihirock/nginx_on_ubuntu:latest   "/bin/sh -c 'service   About a minute ago   Up About a minute   80/tcp              fervent_colden

実行しているようなので、起動しているコンテナのIPアドレスを取得して、アクセスしてみます。

$docker inspect --format \
 '{{ .NetworkSettings.IPAddress }}' fervent_colden
172.17.0.4

$curl http://172.17.0.4
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

nginxのWelcomeページが確認できたので無事起動ができたようです!

確認ができたので、コンテナの停止と削除を行います。いちいちコンテナIDを調べるのが面倒なので以下のコマンドで停止、終了させます。(一つのみコンテナが起動している場合に利用可能)

$docker ps -a |tail -n 1|sed 's/\s\{1,\}/ /g' | cut -d ' ' -f 1|xargs docker stop

$docker ps -a |tail -n 1|sed 's/\s\{1,\}/ /g' | cut -d ' ' -f 1|xargs docker rm

PHP5.6+nginxのコンテナを作ってみる

次にDockerfileを別途作成し、nginxとPHP5.6が動くDockerの環境を作成します。

いきなりDockerfileを書き始めるのもありですが、自分は色々検証しながらやりたいため、bashコマンドで起動し、検証してから最終的にDockerfileに落とし込むようにしました。

$docker run -it ubuntu:14.04 /bin/bash
$ #something

また、通信環境の遅い場合、apt-get updateなどのコマンドで通信が必要になるため、検証に時間がかかります。

その場合、以下のようにapt-getの結果をキャッシュする用のコンテナを起動して利用しておくことで検証の効率化を図ることができます。

DockerのコンテナでUbuntuを使う時にキャッシュを効かせてパッケージインストール(apt-get)を早くする

PHP5.6を使うDockerfileと設定ファイルの構成は以下になりました。

Dockerfile
FROM ubuntu:14.04

ENV LANG=C.UTF-8

MAINTAINER toshihirock

# Setup
RUN apt-get update
RUN apt-get install wget -y
RUN apt-get install software-properties-common python-software-properties -y
RUN add-apt-repository ppa:ondrej/php5-5.6 -y
RUN add-apt-repository ppa:nginx/stable -y
RUN apt-get update

# Install PHP5.6 and latest nginx
RUN apt-get install php5 php5-fpm -y
RUN apt-get install nginx -y

# setting PHP and nginx
COPY conf/default /etc/nginx/sites-available/default 
RUN echo "cgi.fix_pathinfo = 0;" >> /etc/php5/fpm/php.ini
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
RUN echo "<?php phpinfo(); ?>" > /var/www/index.php

EXPOSE 80

CMD service php5-fpm start && nginx
conf/default
server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

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

    # Make site accessible from http://localhost/
    server_name localhost;

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

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index index.php;
        include fastcgi_params;
    }

}

出来たらビルドしてみます。

$docker build --no-cache --force-rm=true -t toshihirock/nginx_php56: .
$$ docker images
REPOSITORY                        TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
toshihirock/nginx_php_on_ubuntu   latest              91a988179ec5        3 minutes ago       320.9 MB

無事ビルドが成功したら次に起動してアクセスしてみます。

$docker run -d toshihirock/nginx_php56
$docker inspect --format '{{ .NetworkSettings.IPAddress }}' tender_wilson
172.17.0.58

$curl http://172.17.0.58/ |grep "PHP Version"
・・・
NxQsgNdAhmIfJN7wxgoWg9fxzPQ+c/g9YAIXgeUKCyipJO4uR/wswAOIwB/5IgxvbAAAAAElFTkSuQmCC" alt="PHP logo" /></a><h1 class="p">PHP Version 5.6.11-1+deb.sury.org~trusty+1</h1>

無事、作成したコンテナへのアクセスでphpのバージョンが5.6のものが使えていることを確認できました。

9
9
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
9
9