インフラエンジニアとして、上で動くアプリケーションの勉強のため
表題の構成を作る。
まずは、MongoDBをDockerで建てるところから。
目次
MongoDB
もともとDoubleClick社で開発されていたデータベースで所謂NoSQLと呼ばれるもの。
オープンソースで、Failoverもサポートされていて、分散データベースのためスケールアウトもするとのこと。
ここからダウンロード可能。
https://github.com/mongodb/mongo
CentOSベースのDockerを動かす
CentOSベースのイメージをダウンロード
$ docker pull centos:latest
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
af4b0a2388c6: Pull complete
Digest: sha256:2671f7a3eea36ce43609e9fe7435ade83094291055f1c96d9d1d1d7c0b986a5d
イメージの確認
$ docker images centos:latest
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest ff426288ea90 7 weeks ago 207.2 MB
CentOSのdockerをデーモンとして動作させる
systemctlを使いたいので、--priviledged
を指定。
$ docker run -itd --privileged --name mongodb centos:latest /sbin/init
72059102d58eb931b92ee1d32b55c759e7e5a46153e99607495756ca47bf6d2a
CentOSのdockerに接続
$ docker exec -it mongodb /bin/bash
[root@72059102d58e /]#
YumからMongodbのインストール
ここを参考に
最後にsystemctl status mongod
で状態確認。
[root@72059102d58e /]# touch /etc/yum.repos.d/mongodb-org-3.6.repo
[root@72059102d58e /]# echo '[mongodb-org-3.6]' >> /etc/yum.repos.d/mongodb-org-3.6.repo
[root@72059102d58e /]# echo 'name=MongoDB Repository' >> /etc/yum.repos.d/mongodb-org-3.6.repo
[root@72059102d58e /]# echo 'baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/' >> /etc/yum.repos.d/mongodb-org-3.6.repo
[root@72059102d58e /]# echo 'gpgcheck=1' >> /etc/yum.repos.d/mongodb-org-3.6.repo
[root@72059102d58e /]# echo 'enabled=1' >> /etc/yum.repos.d/mongodb-org-3.6.repo
[root@72059102d58e /]# echo 'gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc' >> /etc/yum.repos.d/mongodb-org-3.6.repo
[root@72059102d58e /]# yum install -y mongodb-org
<snip>
[root@72059102d58e /]# systemctl enable mongod
[root@72059102d58e /]# systemctl start mongod
[root@72059102d58e /]# systemctl status mongod
● mongod.service - High-performance, schema-free document-oriented database
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2018-02-27 01:36:23 UTC; 2s ago
Docs: https://docs.mongodb.org/manual
Process: 171 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
Process: 170 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 169 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 168 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
Main PID: 173 (mongod)
CGroup: /system.slice/docker-72059102d58eb931b92ee1d32b55c759e7e5a46153e99607495756ca47bf6d2a.scope/system.slice/mongod.service
└─173 /usr/bin/mongod -f /etc/mongod.conf
Feb 27 01:36:22 72059102d58e systemd[1]: Starting High-performance, schema-free document-oriented database...
Feb 27 01:36:22 72059102d58e mongod[171]: about to fork child process, waiting until server is ready for connections.
Feb 27 01:36:22 72059102d58e mongod[171]: forked process: 173
Feb 27 01:36:23 72059102d58e systemd[1]: Started High-performance, schema-free document-oriented database.
[root@72059102d58e /]#
DBに接続してみる
[root@72059102d58e /]# mongo --host 127.0.0.1:27017
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017/
MongoDB server version: 3.6.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2018-02-27T01:36:23.109+0000 I CONTROL [initandlisten]
2018-02-27T01:36:23.109+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-02-27T01:36:23.109+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-02-27T01:36:23.109+0000 I CONTROL [initandlisten]
2018-02-27T01:36:23.109+0000 I CONTROL [initandlisten]
2018-02-27T01:36:23.109+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-02-27T01:36:23.109+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-02-27T01:36:23.109+0000 I CONTROL [initandlisten]
2018-02-27T01:36:23.109+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-02-27T01:36:23.109+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-02-27T01:36:23.109+0000 I CONTROL [initandlisten]
> show dbs
admin 0.000GB
local 0.000GB
> exit
bye
もうこのdockerはいらないので削除
$ docker kill mongodb
$ docker rm mongodb
Dockerfileを作成して、イメージをBuild
接続できたので、これらをdockerのイメージとしてBuildする
Dockerfileは以下の通り
FROM centos:7
MAINTAINER "yuki"
RUN yum update -y && yum clean all
RUN touch /etc/yum.repos.d/mongodb-org-3.6.repo
RUN echo '[mongodb-org-3.6]' >> /etc/yum.repos.d/mongodb-org-3.6.repo
RUN echo 'name=MongoDB Repository' >> /etc/yum.repos.d/mongodb-org-3.6.repo
RUN echo 'baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/' >> /etc/yum.repos.d/mongodb-org-3.6.repo
RUN echo 'gpgcheck=1' >> /etc/yum.repos.d/mongodb-org-3.6.repo
RUN echo 'enabled=1' >> /etc/yum.repos.d/mongodb-org-3.6.repo
RUN echo 'gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc' >> /etc/yum.repos.d/mongodb-org-3.6.repo
RUN yum install -y mongodb-org && yum clean all
# Enable Mongodb
RUN systemctl enable mongod
# Start init daemon
CMD ["/sbin/init"]
イメージをbuild
$ docker build -t ytsuboi/mongodb:1.0 .
Sending build context to Docker daemon 2.56 kB
Step 1 : FROM centos:7
---> ff426288ea90
Step 2 : MAINTAINER "ytsuboi"
---> Running in e29fc4bca889
---> 223b3391b4ee
Removing intermediate container e29fc4bca889
Step 3 : RUN yum update -y && yum clean all
---> Running in 5bd346e13c71
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: ftp.riken.jp
* extras: ftp.riken.jp
* updates: ftp.riken.jp
<snip>
Removing intermediate container da4c2a49ab06
Successfully built 42a0b42361fd
$ docker images ytsuboi/mongodb:1.0
REPOSITORY TAG IMAGE ID CREATED SIZE
ytsuboi/mongodb 1.0 42a0b42361fd 8 minutes ago 575.8 MB
試しにDockerを起動してみる
$ docker run -itd --privileged --name mongodb ytsuboi/mongodb:1.0 /sbin/init
69eacc89ba04324c1b679a724a478c31839355b5b29c15365be2c036fb536b1c
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
69eacc89ba04 ytsuboi/mongodb:1.0 "/sbin/init" 8 seconds ago Up 8 seconds mongodb
$ docker exec -it mongodb /bin/bash
[root@69eacc89ba04 /]#
[root@69eacc89ba04 /]# mongo --host 127.0.0.1:27017
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017/
MongoDB server version: 3.6.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2018-02-27T01:43:04.432+0000 I CONTROL [initandlisten]
2018-02-27T01:43:04.432+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-02-27T01:43:04.432+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-02-27T01:43:04.432+0000 I CONTROL [initandlisten]
2018-02-27T01:43:04.432+0000 I CONTROL [initandlisten]
2018-02-27T01:43:04.432+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-02-27T01:43:04.432+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-02-27T01:43:04.432+0000 I CONTROL [initandlisten]
2018-02-27T01:43:04.432+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-02-27T01:43:04.432+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-02-27T01:43:04.432+0000 I CONTROL [initandlisten]
>
bye
[root@69eacc89ba04 /]#
おわり
これでMongodbのdockerでの構築終了。
次は、Node.jsあたりからこのDBを操作するあたりを