はじめに
普段windows使ったり、macOS使ったり、Linux使ったり、自ら意図して環境を用意してこなかったので、環境構築でもしてみるか、という思いつきのメモです。
環境
macOS sierra
仮想化
実は仮想環境を作る前に、デュアルブートも考えましたが、どうやらインストールも大変だし、アンインストールも大変らしいという話を聞いてやめました。
仮想化を考えてからは以下を検討しました。
・VMwareなどのホスト型:ホストOS上で動作する仮想化ソフトウェアを利用して仮想マシンを管理
・KVMなどのハイパーバイザ型:ハードウェア上のハイパーバイザを利用して仮想マシンを管理
※どちらもオーバヘッドが大きいとのことでした。
・Docker コンテナ型:ホストOS上にコンテナを用意し仮想マシンのように仮想区画を利用
コンテナはホストOS上のカーネルを利用しています。オーバーヘッドが少ないです。
またインフラのコード化ができるのはとても便利な気がしました。
気分的にOS環境構築もしたかったので、ハイパーバイザ型
なんでもよかったのですがVirtualBoxよりも動作が早いとのことで、Parallels Desktop Liteを使うことにしました。
残念ながらmacOS Sierraだと使えず(いつも面倒なので後回しにしてしまうアップデート...)、mojavaにアップデートして、AppStoreでポチッと導入できました。
そうか有料なのか
14日間は無料で、それ以降は年間1万弱とのことでした。
OS選択
Linuxを使うことにして、どのディストリビューションがいいかなと考えましたが、とりあえず前から使っているUbuntuにしました。※正直あまり違いがわかってない
こんな感じ
例のごとくダサい見た目ですね

カッコよくする
こちら参照にしました。
Ubuntuの使い勝手をできるだけMacに近づける
UbuntuでMacレベルに使いやすくておしゃれな環境構築
Ubuntu 18.04 LTSをインストールした直後に行う設定 & インストールするソフト
Ubuntu 16.04のキーボード設定をMacBook向けにしてみる
gitいれたりテーマ変えたりしました。
$ sudo apt install git
$ sudo add-apt-repository ppa:papirus/papirus
$ sudo apt-get update
$ sudo apt-get install papirus-icon-theme
$ sudo apt install materia-gtk-theme
$ sudo apt install -y fonts-roboto fonts-noto fonts-ricty-diminished
最後に
$ sudo apt-get install notify-osd
をしてから適用を行います。
$ unity-tweak-tool
結果こんな感じ。ターミナルの字体含めDockの雰囲気もよくなりました。
一応
macOS側からubuntuに入ることができます。
まずはsshをインストールします。IPアドレスも確認します。
$ sudo apt install ssh
$ ip addr
下からしたから数行目のinetの部分のスラッシュより前の部分をメモしておきます。
そしたら今度はmac側から接続を行います。
ubunutuアカウント名と上記のIPアドレスを使います。
$ ssh [ubuntuアカウント]@000.000.00.0
The authenticity of host '000.000.00.0 (000.000.00.0)' can't be established.
ECDSA key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxx
Are you sure you want to continue connecting (yes/no)?
するとECDSAキーが出てくるので、
今度はubuntu側でキーを確認します。一緒であればOKです。mac側でyesを押します。
$ ssh-keygen -lf /etc/ssh/ssh_host_ecdsa_key_pub
SHA256:xxxxxxxxxxxxxxxxxxxxxxxx
yesを押した後に以下のように出た場合
Permission denied, please try again.
この時はubuntu側に行って設定をvimで書き換えます。
$ sudo vi /etc/ssh/sshd_config
vimで開いたら、以下のようなコメントアウトがあるので、書き加えます。[i]でインサートモードに。
# PermitRootLogin prohibit-password
PermitRootLogin no
書き終えたら、「:wq」で終了します。
最後に先ほどの同様に、以下のコマンドを打ち、パスワード確認のみでログインすることができます。
$ ssh [ubuntuアカウント]@000.000.00.0
一応
これらのssh接続ではIPアドレスとアカウントとパスワードで接続ができました。
でもこれ普通にめっちゃ危ないですよね。セキュリティは低いんです。
ポートの番号変えたり、秘密鍵作ったり、と本当はやらなくてはいけませんが、ここでは説明は割愛します。こちらを参考に。
でも
virtualboxより早いとは聞いていたものの、ややもったりしているのが気になる。。。
ということで一旦ここから離れて、Dockerを導入。
https://www.docker.com/
へ行ってDocker for macをインストール。
インストール場所とID/PW等をセットしするだけ。
※詳細は割愛します
ログインできたら、PC上部にクジラのマークが出ているはずです。
一応ターミナルから確認。
$ docker version
Client: Docker Engine - Community
Version: 18.09.2
API version: 1.39
Go version: go1.10.8
Git commit: 6247962
Built: Sun Feb 10 04:12:39 2019
OS/Arch: darwin/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.2
API version: 1.39 (minimum version 1.12)
Go version: go1.10.6
Git commit: 6247962
Built: Sun Feb 10 04:13:06 2019
OS/Arch: linux/amd64
Experimental: false
nginxを起動する
$ docker run -d -p 80:80 --name webservertest nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
6ae821421a7d: Pull complete
da4474e5966c: Pull complete
eb2aec2b9c9f: Pull complete
立ち上がっているか確認。
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b0268587494c nginx "nginx -g 'daemon of…" 58 seconds ago Up 56 seconds 0.0.0.0:80->80/tcp webservertest
ブラウザでhttp://localhost/
にアクセスしたら立ち上がってました

一旦停止しておく
$ docker container stop webservertest
webservertest
ちなみに再起動はこちら
$ docker container start webservertest
webservertest
test用なので削除
$ docker container rm webservertest
nginxイメージも削除するなら
$ docker image rm nginx
Untagged: nginx:latest
Untagged: nginx@sha256:dd2d0ac3fff2f007d99e033b64854be0941e19a2ad51f174d9240dda20d9f534
Deleted: sha256:f09fe80eb0e75e97b04b9dfb065ac3fda37a8fac0161f42fca1e6fe4d0977c80
Deleted: sha256:355e42205f69706706acf084bc2ed484ea5c034a8861b5bcebb7d8bf141fd686
Deleted: sha256:70cc0be2e302cca187cfadaaf528005d004bec56d46ae890a8a06bdd96768ef1
Deleted: sha256:0a07e81f5da36e4cd6c89d9bc3af643345e56bb2ed74cc8772e42ec0d393aee3
せっかくなのでpythonイメージを。
docker searchで確認
$ docker search python
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
python Python is an interpreted, interactive, objec… 3897 [OK]
django Django is a free web application framework, … 792 [OK]
pypy PyPy is a fast, compliant alternative implem… 169 [OK]
kaggle/python Docker image for Python scripts run on Kaggle 108 [OK]
frolvlad/alpine-python3 The smallest Docker image with Python 3.5 (~… 94 [OK]
azuresdk/azure-cli-python For official Azure CLI 2.0 builds, see https… 37 [OK]
centos/python-35-centos7 Platform for building and running Python 3.5… 33
arm32v7/python Python is an interpreted, interactive, objec… 33
joyzoursky/python-chromedriver Python with Chromedriver, for running automa… 23 [OK]
circleci/python Python is an interpreted, interactive, objec… 23
centos/python-36-centos7 Platform for building and running Python 3.6… 14
centos/python-27-centos7 Platform for building and running Python 2.7… 12
iron/python Tiny Python Microcontainer 9
civisanalytics/datascience-python Common dependencies for data science workflo… 8 [OK]
microsoft/azure-functions-python3.6 Azure functions python image 8 [OK]
publicisworldwide/python-conda Basic Python environments with Conda. 5 [OK]
dockershelf/python Repository for docker images of Python. Test… 4 [OK]
bitnami/python Bitnami Python Docker Image 3 [OK]
komand/python-plugin DEPRECATED: Komand Python SDK 2 [OK]
centos/python-34-centos7 Platform for building and running Python 3.4… 2
civisanalytics/civis-jupyter-python3 Custom Jupyter Notebook image for Python 3 2 [OK]
civisanalytics/civis-jupyter-python2 Custom Jupyter Notebook image for Python 2. 1 [OK]
saagie/python Repo for python jobs 0
qbtrade/python python 3.5.0 with requirements 0
openshift/python-33-centos7 DEPRECATED: A Centos7 based Python v3.3 imag… 0
pullでイメージ入手
$ docker pull python
Using default tag: latest
latest: Pulling from library/python
741437d97401: Pull complete
34d8874714d7: Pull complete
0a108aa26679: Pull complete
7f0334c36886: Pull complete
65c95cb8b3be: Pull complete
9107d7193263: Pull complete
dd6f212ec984: Pull complete
43288b101abf: Pull complete
f68aede0db03: Pull complete
Digest: sha256:fb877e7ea5c40de100534c6d6f515c486247bfd899cb2767f2de0e876a8127fa
Status: Downloaded newer image for python:latest
確認
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
python latest ac069ebfe1e1 3 days ago 927MB
動かしてみる
$ docker run -it --name pythontest python
Python 3.7.2 (default, Feb 6 2019, 12:04:03)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
めっちゃ簡単に環境構築ができました。レイヤ管理だからなのかサクサクでした。
仮想化したubuntu上でDocker動かしたらもっと色々できますね。
ざっと
・CPUとかメモリとかの割りあてがない分、VMよりサクサク
・当然ストレージも
・Dockerがあれば、誰でも複数人でイメージを共有できるので楽そう※VMでsshで共有はできるか
・OSを変えられるのはVMのいいところ
ー個人で環境変えたいくらいならVMはいい
・セキュリティを考えてアプリごとにVMを用意してアイソレーションを立てれば立てるほど、お金もかかるし、リソースが逼迫する
・コンテナであれば、別々にアイソレーションが可能ですが、共通OSなので、OSがダメだった場合に影響範囲が広くなります。
参照
DockerでPythonの実行環境を作ったメモ
DockerでPython3.6の環境構築!matplotlibインストールで詰まった話とかも
Dockerのpython公式Imageを使ってみた
いまさらDockerに入門したので分かりやすくまとめます
第3回 Dockerコマンドの使い方
↓基本はこれ読めば完璧
ゼロからはじめるDocker