LoginSignup
6
6

More than 5 years have passed since last update.

Docker-machineでNginx, Node.js and mroongaの環境構築

Last updated at Posted at 2016-03-03

すばらしく参考になった海外のブログ

docker-composerで、Nginx, Node.js ,Redisの環境構築をしています。
node.jsのサーバを3台構築して、Ngnxでロードバランスしていますね。

実際に彼のgitHubにコードがアップされているので、cloneして[docker-compose up]を実行して起動。

ec2のパプリックIPにアクセスして、インクリメントされていくのを確認できました。

上記を参考にして、node.js,mroongaの環境を作成してみる

mroonga docker

dockerイメージないのかなというところで、探していたらありました。

今回はこちらを使います。

2015のアドベントカレンダーのこの記事も要チェックですね。

docker-machineを実行するディレクトリ構成

/Users/ashibuya/Docker/nodeapi


├── docker-compose.yml
├── mroonga
│   ├── Dockerfile
│   │   └── mysql579_mroonga510
│   │       ├── Dockerfile
│   │       ├── entrypoint.sh
│   │       └── my.cnf
│   ├── README.md
│   ├── dockerhub_description.md
│   └── test
│       ├── build.pl
│       ├── cpanfile
│       └── version.json
└── node
    ├── Dockerfile
    ├── index.js
    └── package.json


docker-compose.yml

node:
    build: ./node
    links:
        - mroonga
    ports:
        - "8080:8080"
mroonga:
    build: ./mroonga/Dockerfile/mysql579_mroonga510/
    ports:
        - "3306:3306"

上記を揃えた状態で、「docker-compose up」アップしてみましょうか。

docker-compose up

(ログ略)
Attaching to nodeapi_mroonga_1, nodeapi_node_1
node_1    | [nodemon] 1.8.1
node_1    | [nodemon] to restart at any time, enter `rs`
node_1    | [nodemon] watching: *.*
node_1    | [nodemon] starting `node /src/index.js`
node_1    | Running on http://localhost:8080

起動終了したら、みてみましょう

% docker images                                                                                                                                                            [9:54:17]
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
nodeapi_node        latest              709a2fd5afa9        2 minutes ago       389.8 MB
nodeapi_mroonga     latest              2a88ec7eaf6e        3 minutes ago       2.547 GB
ubuntu              latest              89d5d8e8bafb        8 days ago          187.9 MB
centos              centos6.6           bec9806dbc09        9 weeks ago         202.6 MB

ec2にアクセスしてみましょう

http://xxx.xxx.xxx.xxx:8080

起動時にはまったしょうもないコト

さて、環境もできたし、「Hello World」くらいは楽勝で終わるだろうと思ったのですが、ブラウザで確認したところWebページでアクセスできないという現象に見舞われました。
おかしいな、EC2のセキュリティグループでは、8080を許可しているし、なんでだろうと。

それはdocker-compose.ymlのポートの記述の問題でした

node:
    build: ./node
    links:
        - mroonga
    ports:
        - "8080" ★
mroonga:
    build: ./mroonga/Dockerfile/mysql579_mroonga510/
    ports:
        - "3306" ★

プロセスを確認してみると

EC2インスタンス側に割り当てられているポート番号は「32779」となっています。
というのは、上記でポート番号の指定をしていないからなんですね。

% docker ps                                                                                                                                                                                                                                          [21:06:07]
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS              PORTS                     NAMES
340361bcff51        nodeapi_node        "node /src/index.js"    9 hours ago         Up 9 hours          0.0.0.0:32779->8080/tcp   nodeapi_node_1
6048fe91099d        nodeapi_mroonga     "/root/entrypoint.sh"   9 hours ago         Up 9 hours          0.0.0.0:32778->3306/tcp   nodeapi_mroonga_1

ポート番号の指定を以下のとおりとしてあげると、

docker-compose.yml

node:
    build: ./node
    links:
        - mroonga
    ports:
        - "8080:8080"
mroonga:
    build: ./mroonga/Dockerfile/mysql579_mroonga510/
    ports:
        - "3306:3306"

ホスト側とコンテナ側でそろいましたね。

% docker ps                                                             [21:13:46]
CONTAINER ID        IMAGE               COMMAND                 CREATED              STATUS              PORTS                    NAMES
42ea555500e7        nodeapi_node        "node /src/index.js"    About a minute ago   Up About a minute   0.0.0.0:8080->8080/tcp   nodeapi_node_1
8d01be9b3d6d        nodeapi_mroonga     "/root/entrypoint.sh"   About a minute ago   Up About a minute   0.0.0.0:3306->3306/tcp   nodeapi_mroonga_1

ポート番号の指定には、気をつけてください。
https://docs.docker.com/compose/compose-file/

ports
Expose ports. Either specify both ports (HOST:CONTAINER), or just the container port (a random host port will be chosen).

Note: When mapping ports in the HOST:CONTAINER format, you may experience erroneous results when using a container port lower than 60, because YAML will parse numbers in the format xx:yy as sexagesimal (base 60). For this reason, we recommend always explicitly specifying your port mappings as strings.

コンテナにログインして確認してみよう

% docker ps                                                                                                                                                                    [11:29:58]
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS              PORTS                    NAMES
42ea555500e7        nodeapi_node        "node /src/index.js"    2 weeks ago         Up 2 weeks          0.0.0.0:8080->8080/tcp   nodeapi_node_1
8d01be9b3d6d        nodeapi_mroonga     "/root/entrypoint.sh"   2 weeks ago         Up 2 weeks          0.0.0.0:3306->3306/tcp   nodeapi_mroonga_1
OK  ~/Docker/nodeapi


% docker exec -it nodeapi_mroonga_1 /bin/bash                                                                                                                                     [11:31:11]
[root@42ea555500e7 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  sbin  selinux  src  srv  sys  tmp  usr  var
[root@42ea555500e7 /]#

Mroongaエンジンがありますね。


[root@8d01be9b3d6d ~]# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.10 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| Mroonga            | YES     | CJK-ready fulltext search, column store                        | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
10 rows in set (0.00 sec)

mysql>

ここにデータを登録したいですよね。しかも実行時に

docker-machine コマンドリファレンス

  • ssh

マシン名を確認してsshすることができる

% docker-machine ls                                                                                                                                                            

NAME       ACTIVE   DRIVER       STATE     URL                        SWARM
docker-2   *        amazonec2    Running   tcp://54.85.119.225:2376
OK  ~/Docker/nodeapi

% docker-machine ssh docker-2  

Welcome to Ubuntu 14.04.2 LTS (GNU/Linux 3.13.0-53-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Thu Dec 17 11:56:21 UTC 2015

  System load:  0.0                Processes:              111
  Usage of /:   29.2% of 15.61GB   Users logged in:        0
  Memory usage: 39%                IP address for eth0:    172.31.62.56
  Swap usage:   0%                 IP address for docker0: 172.17.0.1

  Graph this data and manage this system at:
    https://landscape.canonical.com/

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

131 packages can be updated.
62 updates are security updates.


*** System restart required ***
Last login: Thu Dec 17 11:56:23 2015 from pdf874c8f.tokynt01.ap.so-net.ne.jp
ubuntu@docker-2:~$

  • 注意:コンテナにログインしたい場合は以下。
% docker ps                                                                                                                                                                    [11:29:58]
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS              PORTS                    NAMES
42ea555500e7        nodeapi_node        "node /src/index.js"    2 weeks ago         Up 2 weeks          0.0.0.0:8080->8080/tcp   nodeapi_node_1
8d01be9b3d6d        nodeapi_mroonga     "/root/entrypoint.sh"   2 weeks ago         Up 2 weeks          0.0.0.0:3306->3306/tcp   nodeapi_mroonga_1
OK  ~/Docker/nodeapi


% docker exec -it nodeapi_node_1 /bin/bash                                                                                                                                     [11:31:11]
[root@42ea555500e7 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  sbin  selinux  src  srv  sys  tmp  usr  var
[root@42ea555500e7 /]#

do

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