macOS CatalinaにMongoDBをインストール

概要

MacにMongodbをインストールする。

手順

以下のコメントではだめだった

$ brew install mongodb
~~(省略))
Error: No available formula or cask with the name "mongodb".
==> Searching for a previously deleted formula (in the last month)...
Error: No previously deleted formula found.
==> Searching for similarly named formulae...
Error: No similarly named formulae found.
==> Searching taps...
==> Searching taps on GitHub...
Error: No formulae found in taps.

mongodb-communityをインストール

この資料を参考にした

$ brew tap mongodb/brew
Updating Homebrew...
==> Tapping mongodb/brew
Cloning into '/usr/local/Homebrew/Library/Taps/mongodb/homebrew-brew'...
remote: Enumerating objects: 97, done.
remote: Counting objects: 100% (97/97), done.
remote: Compressing objects: 100% (80/80), done.
remote: Total 462 (delta 38), reused 42 (delta 17), pack-reused 365
Receiving objects: 100% (462/462), 101.47 KiB | 298.00 KiB/s, done.
Resolving deltas: 100% (207/207), done.
Tapped 11 formulae (39 files, 171.5KB).
$
$ brew install mongodb-community
Updating Homebrew...
==> Installing mongodb-community from mongodb/brew
==> Downloading https://fastdl.mongodb.org/tools/db/mongodb-database-tools-macos-x86_64-100.2.0.zip
######################################################################## 100.0%
==> Downloading https://fastdl.mongodb.org/osx/mongodb-macos-x86_64-4.4.1.tgz
######################################################################## 100.0%
==> Installing dependencies for mongodb/brew/mongodb-community: mongodb-database-tools
==> Installing mongodb/brew/mongodb-community dependency: mongodb-database-tools
🍺  /usr/local/Cellar/mongodb-database-tools/100.2.0: 13 files, 172.9MB, built in 4 seconds
==> Installing mongodb/brew/mongodb-community
==> Caveats
To have launchd start mongodb/brew/mongodb-community now and restart at login:
  brew services start mongodb/brew/mongodb-community
Or, if you don't want/need a background service you can just run:
  mongod --config /usr/local/etc/mongod.conf
==> Summary
🍺  /usr/local/Cellar/mongodb-community/4.4.1: 11 files, 136.8MB, built in 3 seconds
==> Caveats
==> mongodb-community
To have launchd start mongodb/brew/mongodb-community now and restart at login:
  brew services start mongodb/brew/mongodb-community
Or, if you don't want/need a background service you can just run:
  mongod --config /usr/local/etc/mongod.conf

以下のファイルが作成されるということだ。

設定ファイル(/usr/local/etc/mongod.conf)
ログディレクトリパス(/usr/local/var/log/mongodb)
データディレクトリパス(/usr/local/var/mongodb)

$ ls -ld /usr/local/etc/mongod.conf /usr/local/var/log/mongodb /usr/local/var/mongodb
-rw-r--r--  1 liu  admin  161 10 25 11:52 /usr/local/etc/mongod.conf
drwxr-xr-x  2 liu  admin   64 10 25 11:52 /usr/local/var/log/mongodb
drwxr-xr-x  2 liu  admin   64 10 25 11:52 /usr/local/var/mongodb

設定ファイルの内容

systemLog:
  destination: file
  path: /usr/local/var/log/mongodb/mongo.log
  logAppend: true
storage:
  dbPath: /usr/local/var/mongodb
net:
  bindIp: 127.0.0.1

バージョンを確認

$ mongod --version
db version v4.4.1
Build Info: {
    "version": "4.4.1",
    "gitVersion": "ad91a93a5a31e175f5cbf8c69561e788bbc55ce1",
    "modules": [],
    "allocator": "system",
    "environment": {
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

2020/10月時点で最新の 4.4.1 である。

起動と停止

mongdbのバックグランドプロセスを自動起動させたい場合は

$ brew services start mongodb-community

自動起動を停止する場合は

$ brew services stop mongodb-community

手動で起動したい場合は、設定ファイルを指定して起動を行う(以下はバックグランドで起動させる場合)

$ mongod --config /usr/local/etc/mongod.conf &
[1] 5301

手動起動したmongodbを停止する場合は、Ctrl + C で止めるか、バックグランド起動した場合は、$ fgでフォアグランドにしてからCtrl + Cで止めればいい。

mongodbへ接続

mondodbを起動させた状態で接続するには

$ mongo

起動していない状態で接続すると、以下のようなエラーが表示される Error: couldn’t connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :

DB操作

DB確認

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

mongodbでのテーブルと行の概念

RDB mongodb
テーブル コレクション
ドキュメント

DB作成

> use test_db
switched to db test_db

test_db が存在するならそのdbに接続し、存在しなければ新しく作成する。

接続中のdbを表示するには

> db
test_db

dbを作成しただけでは、show dbsコマンドでは追加されない

>show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

追加するには、コレクションを作成する必要がある(下の例では vuln というコレクションを作成した)

db.createCollection('vuln')
{ "ok" : 1 }
> show dbs
admin    0.000GB
config   0.000GB
local    0.000GB
test_db  0.000GB

ドキュメントの作成

mongodbでは、列名やデータ型などを先にきめてから行を追加することはない。
1件のドキュメントを作成するには inserOne コマンドを使う。

> db.vuln({ name: "Apache Struts 2の脆弱性", level: "S0", limit_date: "2020-12-31"})
{
    "acknowledged" : true,
    "insertedId" : ObjectId("5f954c22051b138e6af85465")
}

insertOneでドキュメントを作成する時、もし vuln コレクションが存在しない場合、その時に一緒に作成してくれる(便利!)。

作成したドキュメントを確認するには

> db.vuln.find()
{ "_id" : ObjectId("5f954d22051b138e6af85466"), "name" : "Apache Struts 2の脆弱性", "level" : "S0", "limit_date" : "2020-12-31" }

もう1つを作成する

> db.vuln.insertOne({ name: "ATS DDoS脆弱性", level: "S1", limit_date: "2021-01-31"})
{
    "acknowledged" : true,
    "insertedId" : ObjectId("5f95571d051b138e6af85467")
}
> db.vuln.find()
{ "_id" : ObjectId("5f954d22051b138e6af85466"), "name" : "Apache Struts 2の脆弱性", "level" : "S0", "limit_date" : "2020-12-31" }
{ "_id" : ObjectId("5f95571d051b138e6af85467"), "name" : "ATS DDoS脆弱性", "level" : "S1", "limit_date" : "2021-01-31" }

find()で確認する内容をきれいにしたい場合は、pretty() を使う。

> db.vuln.find().pretty()
{
    "_id" : ObjectId("5f954d22051b138e6af85466"),
    "name" : "Apache Struts 2の脆弱性",
    "level" : "S0",
    "limit_date" : "2020-12-31"
}
{
    "_id" : ObjectId("5f95571d051b138e6af85467"),
    "name" : "ATS DDoS脆弱性",
    "level" : "S1",
    "limit_date" : "2021-01-31"
}

まさにjson形式。

ドキュメントを探す

2つの方法があり、1つはObjectIdを使うこと、もう1つはキーとその値を使って探すことができる。

> db.vuln.find(ObjectId("5f954d22051b138e6af85466")).pretty()
{
    "_id" : ObjectId("5f954d22051b138e6af85466"),
    "name" : "Apache Struts 2の脆弱性",
    "level" : "S0",
    "limit_date" : "2020-12-31"
}
> db.vuln.find({"name": "Apache Struts 2の脆弱性"}).pretty()
{
    "_id" : ObjectId("5f954d22051b138e6af85466"),
    "name" : "Apache Struts 2の脆弱性",
    "level" : "S0",
    "limit_date" : "2020-12-31"
}

ドキュメントの更新

Apache Strusts 2の脆弱性のレベルをS1に更新したい、

> db.vuln.update({"name": "Apache Struts 2の脆弱性"}, {$set: {"level": "S1"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.vuln.find().pretty()
{
    "_id" : ObjectId("5f954d22051b138e6af85466"),
    "name" : "Apache Struts 2の脆弱性",
    "level" : "S1",
    "limit_date" : "2020-12-31"
}
{
    "_id" : ObjectId("5f95571d051b138e6af85467"),
    "name" : "ATS DDoS脆弱性",
    "level" : "S1",
    "limit_date" : "2021-01-31"
}

#### ドキュメントの削除
1件削除するにはdeleteOneを使う。

db.vuln.deleteOne({"name" : "ATS DDoS脆弱性"}) { "acknowledged" : true, "deletedCount" : 1 }

db.vuln.find().pretty() { "_id" : ObjectId("5f954d22051b138e6af85466"), "name" : "Apache Struts 2の脆弱性", "level" : "S1", "limit_date" : "2020-12-31" }

#### コレクション一覧

show collections vuln

#### コレクションの削除
drop()コマンドを使う。

db.vuln.drop() true

コレクションがなくなると、先に作成したtest_dbが表示されなくなった。

show dbs admin 0.000GB config 0.000GB local 0.000GB

#### DBの削除

use test_db switched to db test_db db.dropDatabase() { "dropped" : "test_db", "ok" : 1 }

### まとめ
mongodbをMacにインストールし、一番初歩的な操作方法を体験した。使った感想としては、DBのスキーマに気にする必要がなく、json形式なドキュメントであれば、簡単に追加することができた。しかし、手打ちでDB操作コマンドを打つには、ちょっと慣れが必要と感じた。

大量な構造化されたjsonデータを追加し、表示させるような単純な動作(Webスクレイピング等)であれば、mongodbは手軽に利用できていいのではないかと思った。

### 参考資料
- [https://reffect.co.jp/windows/mac-mongodb-install:title]
- [https://qastack.jp/programming/58283257/mongodb-cant-find-data-directory-after-upgrading-to-mac-os-10-15-catalina:title]

Docker centos:centos8のロケール等を設定

概要

dockerでcentos:centos8のコンテナを作った時に、日本語ロケールになっていなかったので、設定した。

設定した手順をメモ書きする。

現在のロケール値を確認する

# locale
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
# 
# locale -a | grep  ja_JP
#

日本語ロケールが定義されていない。

日本語の言語パックを入れる

glibc-locale-source と glibc-langpack-ja をインストール

# dnf install glibc-locale-source glibc-langpack-ja
~~(省略)~~
#
# locale -a | grep ja_JP
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_COLLATE to default locale: No such file or directory
ja_JP.eucjp
ja_JP.utf8

ja_JP.eucjp と ja_JP.utf8 が入るようになったが、以前とエラーが出ている。
エラーの原因は、おそらくen_US.utf8 がインストールされていないことを推測。
以下のように対処する。

英語の言語パックを入れる

#  LANG=C dnf install langpacks-en glibc-langpack-en
Last metadata expiration check: 2:17:15 ago on Thu Oct 22 13:03:32 2020.
Package langpacks-en-1.0-12.el8.noarch is already installed.
Dependencies resolved.
========================================================================================
 Package                    Architecture    Version               Repository       Size
========================================================================================
Installing:
 glibc-langpack-en          x86_64          2.28-101.el8          BaseOS          821 k

Transaction Summary
========================================================================================
Install  1 Package

Total download size: 821 k
Installed size: 6.0 M
Is this ok [y/N]: y
Downloading Packages:
glibc-langpack-en-2.28-101.el8.x86_64.rpm               3.6 MB/s | 821 kB     00:00
----------------------------------------------------------------------------------------
Total                                                   993 kB/s | 821 kB     00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                1/1
  Installing       : glibc-langpack-en-2.28-101.el8.x86_64                          1/1
  Running scriptlet: glibc-langpack-en-2.28-101.el8.x86_64                          1/1
  Verifying        : glibc-langpack-en-2.28-101.el8.x86_64                          1/1

Installed:
  glibc-langpack-en-2.28-101.el8.x86_64

Complete!

現在のロケール値を再確認する

# locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

これまで出ていたエラーがでなくなったが、英語ロケールになっている。

日本語ロケールに設定

# cat /etc/locale.conf
LANG=ja_JP.UTF-8
# source /etc/locale.conf ← これが重要!
# locale
LANG=ja_JP.UTF-8
LC_CTYPE="ja_JP.UTF-8"
LC_NUMERIC="ja_JP.UTF-8"
LC_TIME="ja_JP.UTF-8"
LC_COLLATE="ja_JP.UTF-8"
LC_MONETARY="ja_JP.UTF-8"
LC_MESSAGES="ja_JP.UTF-8"
LC_PAPER="ja_JP.UTF-8"
LC_NAME="ja_JP.UTF-8"
LC_ADDRESS="ja_JP.UTF-8"
LC_TELEPHONE="ja_JP.UTF-8"
LC_MEASUREMENT="ja_JP.UTF-8"
LC_IDENTIFICATION="ja_JP.UTF-8"
LC_ALL=
#
# echo $LANG
ja_JP.UTF-8
# date
2020年 10月 22日 木曜日 15:28:22 UTC

日本語環境にはなったが、TZが日本になっていない。

日本TZに設定する

# timedatectl set-timezone Asia/Tokyo
# date
2020年 10月 23日 金曜日 00:31:30 JST

ようやくロケールタイムゾーンを正しく設定できた。

参考資料

Adobe CS6は最新macOSでは起動できない

Adobe Illustrator&Photoshop CS6は最新のmasOS Catalinaでは動かない。
原因はmasOS Catalinaからは32bitアプリケーションが全部使えなくなったからだ。

以下のサイトにはいい参考情報が書かれている(ありがとうございます)

321web.link

ちょっと待ってよ、みれる方法があるみたい。

shinbido.com

不勉強だが、初めて「Parallels Desktop」というmasOSの最強と言われている仮想マシンを知った。

ゼロ幅スペースの謎を解く

ゼロ幅スペース(Zero Width Space, ZWSP)とは?

ゼロ幅スペース - Wikipedia

によれば、

  • コンピュータの組版に用いられる非表示文字で、文書処理システムに対して語の切れ目を示すのに用いる。
  • HTMLでは要素の代替として、長い単語の途中で改行可能な場所を示すのに使われる。

例として、以下の文字列では、キャメルケースの単語間には、ZWSPがそれぞれ入っている(非表示のため目には見えない)

Lorem​Ipsum​Dolor​Sit​Amet​Consectetur​Adipiscing​Elit​Sed​Do​Eiusmod​Tempor​Incididunt​Ut​Labore​Et​Dolore

Vimで開くと以下のように見える

Lorem<200b>Ipsum<200b>Dolor<200b>Sit<200b>Amet<200b>Consectetur<200b>Adipiscing<200b>Elit<200b>Sed<    200b>Do<200b>Eiusmod<200b>Tempor<200b>Incididunt<200b>Ut<200b>Labore<200b>Et<200b>Dolore

このなぞの「<200b>」がZWSPである。

<200b>は何のコードか

<200b>は「ゼロ幅スペース」のUnicodeである。

Unicode Character Table

をみると、<200b>のUTF-8での16進数は「E2 80 8B」である。

バイナリエディタでみてみる

それでは、文字列

Lorem​Ipsum​Dolor​Sit​Amet​Consectetur​Adipiscing​Elit​Sed​Do​Eiusmod​Tempor​Incididunt​Ut​Labore​Et​Dolore

バイナリエディタで16進数をdumpしてみる

$ echo "Lorem​Ipsum​Dolor​Sit​Amet​Consectetur​Adipiscing​Elit​Sed​Do​Eiusmod​Tempor​Incididunt​Ut​Labore​Et​Dolore" | od -t x1

0000000 4c 6f 72 65 6d e2 80 8b 49 70 73 75 6d e2 80 8b
0000020 44 6f 6c 6f 72 e2 80 8b 53 69 74 e2 80 8b 41 6d
0000040 65 74 e2 80 8b 43 6f 6e 73 65 63 74 65 74 75 72
(省略)

テキストファイルから<200b>文字を削除する

ファイルをvimで開いて、

:%s/\%u200b//g

これで、ファイル内すべての<200b>を削除できる。

vim以外の方法で、sedなどいろいろ試してみたが、うまく行かず諦めた。。

<200b>のコピペ

コピーについては、以下の資料を参考にうまく行けた。

unicodeでU+200B;幅なしスペース(幅が0)という文字があるん... - Yahoo!知恵袋

まず、文字幅0のスペースの左側に文字カーソルを置きます。 次に、Shift+→ で文字幅0のスペースを選択します。 (ただし、反転表示されないので、選択されたかどうか解り難いです)
Ctrl+C でコピー
Ctrl+V で貼り付け

<200b>の入力

あくまでもMacしか試していないので、Windowsの方はもっといろいろな方法があるかもしれない。

ChromeのConsoleから

Consoleを開いて、copy("\u200B")を打てば文字がクリップボードに入ったため、後はペストすればいい。 個人的にこの方法が好き。

Unicode Character Table から

Unicode Character Table から「Copy」ボタンを押せばコピーされ、後はペストすればいい。

coolsymbol.comから

Symbols に色々なシンボルがあり、画面下部に「Zero-Width Space (ZWSP) Character」あるので、コピーして後はペストすればいい。

普段仕事上めったに<200b>にあうことはないが、自分は今日仕事上遭ったため、調べてブログにした。 インターネット上で調べている方のいい参考になれればと思います。

参考資料

screwdriver.cdを使ってローカルでビルド

環境

macOS Catalina

前提

  • Docker Desktop がインストールされている
  • git コマンドが使える

手順

screwdriverクラスタをローカルに起動させる

$ python <(curl -L https://git.io/sd-in-a-box)
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100  9392  100  9392    0     0   7120      0  0:00:01  0:00:01 --:--:--  7120
🎁   Boxing up Screwdriver
👀   Checking prerequisites
🔐   Generating signing secrets
📤   Which SCM provider would you like to use? (github/gitlab/bitbucket) github
📦   Generating OAuth credentials

    Please create a new OAuth application on GitHub.com
    Go to https://github.com/settings/applications/new to start the process
    For 'Homepage URL' put http://192.168.0.2:9000
    For 'Authorization callback URL' put http://192.168.0.2:9001/v4/auth/login

    When done, please provide the following values:

    Client ID: 9baf4ee73bcbd1110a04
    Client Secret:

💾   Writing Docker Compose file
🚀   Screwdriver is ready to launch!

    Just run the following commands to get started!
      $ docker-compose pull
      $ docker-compose -p screwdriver up -d
      $ open http://192.168.0.2:9000

    Would you like to run them now? (y/n) y
Pulling api   ... done
Pulling ui    ... done
Pulling store ... done
Creating network "screwdriver_default" with the default driver
Creating screwdriver_store_1 ... done
Creating screwdriver_api_1   ... done
Creating screwdriver_ui_1    ... done

👍   Launched!

    A few more things to note:
      - To stop/reset Screwdriver
        $ docker-compose -p screwdriver down
      - If your internal IP changes, update the docker-compose.yml and your SCM OAuth application
      - In-a-box does not support Webhooks including PullRequests for triggering builds
      - To create your own cluster, see https://docs.screwdriver.cd/cluster-management/kubernetes
      - For help with this and more, find us on Slack at https://slack.screwdriver.cd

❤️   Screwdriver Crew

ui が立ち上がった。

screwdriver

Sign in with SCM Provider」リンクをクリックすると、以下のように権限認可画面が出る Authorize

状況確認

コンテナ
$ docker ps -a
CONTAINER ID        IMAGE                              COMMAND                  CREATED              STATUS                           PORTS                            NAMES
b42f1bb49d9f        screwdrivercd/ui:stable            "nginx -g 'daemon of…"   About a minute ago   Up About a minute                0.0.0.0:9000->80/tcp             screwdriver_ui_1
e18174d2bac6        screwdrivercd/screwdriver:stable   "docker-entrypoint.s…"   About a minute ago   Up About a minute                8080/tcp, 0.0.0.0:9001->80/tcp   screwdriver_api_1
eb1b350f4355        screwdrivercd/store:stable         "docker-entrypoint.s…"   About a minute ago   Up About a minute                0.0.0.0:9002->80/tcp             screwdriver_store_1
af6b538f8bee        centos:centos8                     "/sbin/init"             7 days ago           Exited (137) About an hour ago
イメージ
$ docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
centos                      centos8             0d120b6ccaa8        2 months ago        215MB
screwdrivercd/screwdriver   stable              bfc1a62afe5a        5 months ago        1.05GB
screwdrivercd/ui            stable              321c51c4e647        5 months ago        25.8MB
screwdrivercd/store         stable              692bb501be06        8 months ago        1.02GB

screwdrivercdだけで合計2GB以上もある。。

コンテナを停止

$ docker-compose -p screwdriver down
Stopping screwdriver_ui_1    ... done
Stopping screwdriver_api_1   ... done
Stopping screwdriver_store_1 ... done
Removing screwdriver_ui_1    ... done
Removing screwdriver_api_1   ... done
Removing screwdriver_store_1 ... done
Removing network screwdriver_default

コンテナ再確認

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
af6b538f8bee        centos:centos8      "/sbin/init"        29 hours ago        Exited (137) 25 hours ago                       centos8

メモ:コンテナ起動

$ docker-compose -p screwdriver up

Get Started

Getting Started with Screwdriver を参考し

$ git clone git@github.com:<YOUR_USERNAME_HERE>/quickstart-generic.git
$ cd quickstart-generic/
screwdriver.yaml

サンプルのscrewdriver.yamlにはコメント行が沢山あり、見づらいため、

$ sed -e '/^[\t ]*#/d' screwdriver.yaml 
---
shared:
  image: buildpack-deps

jobs:
  main:
    requires: [~pr, ~commit]
    steps:
      - export: export GREETING="Hello, world!"
      - hello: echo $GREETING
      - set-metadata: meta set example.coverage 99.95
  second_job:
    requires: main
    steps:
      - make_target: make greetings
      - get-metadata: meta get example
      - run_arbitrary_script: ./my_script.sh

でコメント行を除外して表示した。

Create a New Pipeline

screwdriver.cdのuiから、SCM(githubを選択していた)にログインして(OAuth認証)、Create Pipelineする。
下図にある[my_github_accoutn]部分は、実際に自分のアカウントが入る。 pipeline

Startボタンを押してビルド開始。 Start to build

以下のエラーが出てしまった

 (HTTP code 404) no such container - No such image: screwdrivercd/launcher:stable

参考資料

Install CentOS8 through Docker on macOS

概要

macOS Catalina上で、Dockerで以下の仮想環境を構築する:

手順

準備

Docker Desktop Community 2.4.0.0をインストール

$ docker version
Client: Docker Engine - Community
 Cloud integration  0.1.18
 Version:           19.03.13
 API version:       1.40
 Go version:        go1.13.15
 Git commit:        4484c46d9d
 Built:             Wed Sep 16 16:58:31 2020
 OS/Arch:           darwin/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.13
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       4484c46d9d
  Built:            Wed Sep 16 17:07:04 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.3.7
  GitCommit:        8fba4e9a7d01810a393d5d25a3621dc101981175
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

どのようなDockerイメージがあるかを調べるには、docker hub から探せばいい。

CentOS8のイメージをDocker Hubから取得

$ docker pull centos:centos8
centos8: Pulling from library/centos
3c72a8ed6814: Pull complete
Digest: sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd
Status: Downloaded newer image for centos:centos8
docker.io/library/centos:centos8

コンテナを作成して起動

コンテナの名前を「centos8」を指定し、コンテナのポート8080をホストの80ポートにバウンドさせて、バックグランドで起動する。

$ docker run --detach --name centos8 --privileged --publish=80:8080 centos:centos8 /sbin/init
af6b538f8beed2885d695f72ba0c29c0c00332f1dcb4787989f71105f622d3dc

Docker-docs-ja に各オプションの説明があります。

すべてのコンテナを表示してみる

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
af6b538f8bee        centos:centos8      "/sbin/init"              2 minutes ago       Up About a minute                      centos8
2fc5923004a3        hello-world         "/hello"                 22 hours ago        Exited (0) 22 hours ago                        gracious_euclid
5458576feea9        alpine/git          "git clone https://g…"   25 hours ago        Exited (0) 25 hours ago                        repo

昨日にDocker Desktopを入れた時にお試しに作ったコンテナが存在しているので、削除してみる。

コンテナを削除する

コンテナ削除のコマンド:docker rm [コンテナID](コンテナIDはspace区切りで複数指定してもOK)

やってみる

$ docker rm 2fc5923004a3 5458576feea9
2fc5923004a3
5458576feea9

削除後、$ docker ps -aで確認すると、centos:centos8だけ残っていることが確認できた。

イメージを削除する

まずはイメージの一覧をみる

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine/git          latest              94f8849864da        3 weeks ago         28.4MB
centos              centos8             0d120b6ccaa8        2 months ago        215MB
hello-world         latest              bf756fb1ae65        9 months ago        13.3kB

イメージを削除するコマンド:docker rmi [イメージID](イメージIDもspace区切りで複数指定してOK)

$ docker rmi 94f8849864da bf756fb1ae65
Untagged: alpine/git:latest
Untagged: alpine/git@sha256:9e2309b689ac74257941b0f7b72d8d34c9e94216806eed047338eb39e3b90c58
Deleted: sha256:94f8849864daf55267c12512ff521931842cd707adfde019d86e2d41179ae9e2
Deleted: sha256:c044252e47e67929e158bee2fbf759460445f256ee3376474abac9efd3af5f35
Deleted: sha256:d59fd62de84f6d6ca50712ef1e18ebd451ec306711c0270ba9efba28e6fe462e
Deleted: sha256:50644c29ef5a27c9a40c393a73ece2479de78325cae7d762ef3cdc19bf42dd0a
Untagged: hello-world:latest
Untagged: hello-world@sha256:8c5aeeb6a5f3ba4883347d3747a7249f491766ca1caa47e5da5dfcf6b9b717c0
Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b
Deleted: sha256:9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63

コンテナを起動する

$  docker start centos8
centos8

コンテナに接続して確認

docker exec コマンドを使用して、起動したコンテナ内にて bash シェルを実行する。

$ docker exec -it centos8 bash
[root@af6b538f8bee /]#  ← 接続できた!
root@af6b538f8bee /]# cat /etc/redhat-release
CentOS Linux release 8.2.2004 (Core)  ← CentOS8 がインストールされている
[root@af6b538f8bee /]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="8 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="8"

シェアから抜く

[root@af6b538f8bee /]# exit
exit

コンテナを停止する

$  docker stop centos8
centos8

停止まで十数秒かかった気がする。

コンテナに入れたいものを入れる

CentOS8になってから、システムが利用する python のバージョンも python3 系に上がっているため、phthon2 で書かれている yum が廃止せざるを得なくなった。 また、もともと yum にはいろいろ欠陥があり、dnf はそれを改善されるようになったこともあるらしい。

vim

# dnf install vim
~~(省略)~~
# # vim --version
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Nov 11 2019 19:08:24)

nginx

# dnf install nginx
~~(省略)~~
Complete!

デフォルトでは80/tcpで開放されているため、コンテナ起動時にバウンドさせていた8080/tcpに変更する。

# ls -l  /etc/nginx/conf.d/default.conf ← ファイルが存在しないことを確認した
# cat << EOF > /etc/nginx/conf.d/default.conf
server {
    listen       8080;
    server_name  localhost;
}
EOF

nginxをコンテナ接続時に起動ようにする

# systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

nginxを起動させる

# systemctl start nginx
# ps auxww | grep nginx
root         226  0.0  0.1 103328  2184 ?        Ss   12:31   0:00 nginx: master process /usr/sbin/nginx
nginx        227  0.0  0.3 122628  8004 ?        S    12:31   0:00 nginx: worker process
nginx        228  0.0  0.3 122628  8004 ?        S    12:31   0:00 nginx: worker process
root         230  0.0  0.0   9180   992 pts/0    S+   12:31   0:00 grep --color=auto nginx

コンテナ内でnginxサーバにアクセスしてみる

# curl -D - -s  -o /dev/null http://localhost
HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Sun, 18 Oct 2020 12:49:29 GMT
Content-Type: text/html
Content-Length: 4057
Last-Modified: Mon, 07 Oct 2019 21:16:24 GMT
Connection: keep-alive
ETag: "5d9bab28-fd9"
Accept-Ranges: bytes

ホストOS(macOS)にてブラウザからコンテナのngnixサーバにアクセスしてみる http://localhost

画像

ついでに、index.html をいじってみる

# cd /usr/share/nginx/html
# mv index.html index.html.org
# cat << EOF > index.html
<html>
<body>
Hello, World!
</body>
</html>
EOF
# curl -i http://localhost:8080
HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Sun, 18 Oct 2020 15:11:37 GMT
Content-Type: text/html
Content-Length: 44
Last-Modified: Sun, 18 Oct 2020 15:11:06 GMT
Connection: keep-alive
ETag: "5f8c5b0a-2c"
Accept-Ranges: bytes

<html>
<body>
Hello, World!
</body>
</html>

python3

python3系だけあればいいかな。

# dnf install  python3 python3-pip
CentOS-8 - AppStream                                    5.0 kB/s | 4.3 kB     00:00
~~(省略)~~
Installed:
  platform-python-pip-9.0.3-16.el8.noarch
  python3-pip-9.0.3-16.el8.noarch
  python3-setuptools-39.2.0-5.el8.noarch
  python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64

Complete!
#
# python3 -V
Python 3.6.8

pip3でnumpy、matplotlib、scrapy、requestをインストールする。

# pip3 install numpy matplotlib scrapy requests
(省略)
# python3
Python 3.6.8 (default, Apr 16 2020, 01:36:27)
[GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> import matplotlib
>>> import scrapy
>>> import requests
>>> exit()

無事にインストールできたことが分かる。

mac -> ssh -> dockerできるようにする

docker側の設定

  • openssh-server をインストール
# dnf install openssh-server
...省略...
Installed:
  openssh-8.0p1-5.el8.x86_64                       openssh-server-8.0p1-5.el8.x86_64

Complete!
  • サービスを起動
# systemctl start sshd.service
  • sshd.serviceのステータスがアクティブ状態になっていることを確認
# systemctl status sshd.service
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2021-01-05 21:26:45 JST; 1min 49s ago
     Docs: man:sshd(8)
           man:sshd_config(5)
...省略...
 105 21:26:45 af6b538f8bee systemd[1]: Starting OpenSSH server daemon...
...省略...
 105 21:26:45 af6b538f8bee systemd[1]: Started OpenSSH server daemon.
  • sshクライアントをインストール
# dnf install openssh-clients
...省略...
Installed:
  libedit-3.1-23.20170329cvs.el8.x86_64                 openssh-clients-8.0p1-5.el8.x86_64

Complete!

To be continured.

参考資料

新しいMacにgitクライアントを入れるまで

先週、MacBook Pro 13インチ(A2289)が到着したので、以前のMacBook Air を使っていた時に何気なくインストールし使ってきたソフトウェアのインストール手順を、Proを使っていくのをきっかけにまとめてみようと思った。

今回は、まずgitクライアントを入れるまでにメモ書きしておく。

以下の資料を参考させてもらった

Git のインストール 〜Git をMacにインストールしよう〜 | バージョン管理システム入門(初心者向け)

$which git
/usr/bin/git
$ git --version

ポップアップが出て、コマンドラインデベロッパ・ツールが必要だ、インストールしてくださいと言われた。「インストール」ボタンを押して、後のポップアップに「同意する」を押してやったが、しばらくするとなんとまたポップアップが出て、インストールできないと言われた(原因不明)。2回繰り返しても同じなので、この方法を諦めた。

参考資料には、別の方法として、インストーラーでgitをインストールする方法も紹介されているが、自分はその次の「Homebrewを使ってGitをインストールする」を選んでインストールすることにした(なぜならば、このMacにはまだ Homebrew が入っていないので、今後いろいろパッケージを入れる必要があり、どうせ必要だと思ったから)

Homebrew を入れる

まずは念の為に確認すうr

$ brew list
-bash: brew: command not found
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
~~(省略)~~
==> Installation successful!

==> Homebrew has enabled anonymous aggregate formulae and cask analytics.
Read the analytics documentation (and how to opt-out) here:
  https://docs.brew.sh/Analytics
No analytics data has been sent yet (or will be during this `install` run).

==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
  https://github.com/Homebrew/brew#donations

==> Next steps:
- Run `brew help` to get started
- Further documentation:
    https://docs.brew.sh

今後は git がちゃんと使えるようになった

$ git --version
git version 2.24.3 (Apple Git-128)