チラウラヤーン3号

その辺のプログラマーのチラ裏です。

Python メモ

@TakesxiSximada さんに学ぶ

github にあるディレクトリからモジュールをインストールするときのスタイル

pip コマンドスタイル pip install -e "git+ssh://git@github.com/hoge/fuga.git#egg=hoge.piyo&subdirectory=hoge.poko"

requirements.txt スタイル -e git+ssh://git@github.com/hoge/fuga.git#egg=hoge.piyo&subdirectory=hoge.poko

TakesxiSximada氏制作 Python パッケージボイラープレート

TakesxiSximada/sximada.boilerplate

setup.py と buildout について

  • setup.py: Pythonのパッケージングスクリプト
  • buildout <- 環境構築 (今でいうDocker のaufs使わない版)
    • ただ、buildoutは時代遅れのものらしい

例えば OpenCV に依存するパッケージを作る場合

"setup.pyを記載してpackagingしたほうが良いです。" とのこと。 他、 "Dockerfileを置いてあげると親切かも" とも。

ディレクトリ構成として、

  • README.rst
  • setup.py
  • Dockerfile
  • src/….

"こんな感じで" とのこと。

Dockerfile って?

see: いまさら聞けないDocker入門(3):Dockerfileとdocker buildコマンドでDockerイメージの作成 (1/2) - @IT

「Dockerfile」ファイルは、プログラムのビルドでよく利用されるmakeツールの「Makefile」ファイルと同様に、Dockerコンテナーの構成内容をまとめて記述するシンプルなテキスト形式のファイルです。

とのこと。

つまり、こいつを使って、Docker (よくわかってないけど) コンテナを自動作成できるっぽい。

自動作成に使うコマンドが docker build コマンド。

Mac の Homebrew でやるの事前準備としてはとりあえず

$ brew cask install virtualbox docker-machine

$ docker-machine create --driver virtualbox default  # default という名前で仮想マシンを作る

$ docker-machine env default

export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/peketamin/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
# Run this command to configure your shell: 
# eval "$(docker-machine env default)"

$ echo 'eval "$(docker-machine env default)"' >> ~/.bash_profile  # (or .bashrc)
# 上記の export 群も .bash_profile に入れる

$ docker-machine start

$ docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b901d36b6f2f: Pull complete 
0a6ba66e537a: Pull complete 
Digest: sha256:8be990ef2aeb16dbcb9271ddfe2610fa6658d13f6dfb8bc72074cc1ca36966a7
Status: Downloaded newer image for hello-world:latest

Hello from Docker.
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker Hub account:
 https://hub.docker.com

For more examples and ideas, visit:
 https://docs.docker.com/userguide/

ですかね? (参考: Dockerのインストール - 74th)

分からない…。とりあえず Dockerfile なるものを使って、 AWS S3 のモックサーバーである jubos/fake-s3 を動かしてみることから始める。

fake-s3 を動かす

Docker Hub を見ると

$ docker pull lphoward/fake-s3

と書いてあるので、それをする。ここからどうしたらいいんだろう… hello-world に倣う。

$ docker run lphoward/fake-s3

[2015-12-19 08:10:56] INFO  WEBrick 1.3.1
[2015-12-19 08:10:56] INFO  ruby 2.1.5 (2014-11-13) [x86_64-linux-gnu]
[2015-12-19 08:10:56] INFO  WEBrick::HTTPServer#start: pid=1 port=4569

おっ、起動したっぽい。 ipython を起動して boto からアクセスしてみる

In [1]: from boto.s3.connection import S3Connection, OrdinaryCallingFormat

In [2]: connection = S3Connection("fake", "fake", is_secure=False, port=4569, host='127.0.0.1', calling_format=OrdinaryCallingFormat())

In [3]: conn = S3Connection("fake", "fake", is_secure=False, port=4569, host='localhost', calling_format=OrdinaryCallingFormat())

In [4]: conn.create_bucket('test_bucket')
...
...
...

例外吐いてタイムアウトした…。 Dockerについての調査まとめ、基本機能+主な特徴+Vagrantの連携 あたり にあるように -p オプションを使ってみる。

しかしそれでもダメっぽいので、更に、 MacでDockerを利用するときの初期設定 | Recruit Jobs TECHBLOG に従い、VirtualBox 上でポートフォワード設定をする。なるほど! VirtualBox 経由だからそういうのが必要なのね。 (これも見た: Docker初心者が遭遇するエラーをまとめてみた | Recruit Jobs TECHBLOG)

$ docker run -p 4569:4569 lphoward/fake-s3
# 続き
In [5]: conn.create_bucket('test_bucket')
Out[5]: <Bucket: test_bucket>

In [6]:  # 通ったっぽい!

docker 側の出力

[2015-12-19 09:07:10] INFO  WEBrick::HTTPServer#start: pid=1 port=4569
10.0.2.2 - - [19/Dec/2015:09:07:16 UTC] "PUT /test_bucket/ HTTP/1.1" 200 0
- -> /test_bucket/

おおっ! …そして、 test_bucket はどこに作られた…? コンテナにログインして見ればわかる?

^C  # 一度落とした
$ docker run -i -t -p 4569:4569 lphoward/fake-s3

… ジョブをバックグラウンドにもできない!だから ls も出来ないー!

doker --help してみると

exec      Run a command in a running container

というのがあった。これで ls できそう…?

docker exec 672518ad092c ls

bin
boot
dev
etc
fakes3_root
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

おあー! fakes3_root ディレクトリあったたよー!じゃーこの中に test_bucket 作られてるハズ!

$ docker exec 672518ad092c ls fakes3_root

…なにも出ない…。あ、さっき、再起動したからか?そういうもの? もう一度コンテナ起動 → ipython で テストバケット作成 → docker exec してみる。

$ docker exec 672518ad092c ls fakes3_root
127
$ docker exec 672518ad092c ls fakes3_root/127
test_bucket

なんか、127 っていうディレクトリはできているものの、作成出来ている!

なんとなくわかった!

で、 Dockerfile ってなんだっけか

コンテナを起動しただけで fakes3サーバーが立ち上がり、ファイルシステムにはファイル置き場のエンドポイントが出来ている。これを最初から定義してあるコンテナの定義ファイルが Dockerfile 、 という感じなのかな?

で、こういうのもパッケージに同梱してあげると、親切だという、そういうことですね…!?