Docker コマンドとかメモ

Docker のコマンドとかのメモ


取得できる docker image の取得
$ docker search {keyword}

docker image の取得
$ docker pull {image}

docker image の確認
$ docker images

コンテナの起動
$ docker run --name {container name} -it {docker image} {command)}
※ -it: 起動したコンテナをターミナルから操作する為に付けた
※ {command} は --entrypoint {command} とするのが行儀良いらしい

option 説明

  • i, --interactive: keep STDIN open even if not attached. インタラクティブモード
  • t, --tty: Allocate a pseudo-TTY. 仮想 tty を用意する
  • h: ホストネーム付ける
  • e: 環境変数を渡す 例.) $ docker run -it {image} -e ENV_VAL=value {command}

停止したコンテナの起動
$ docker start {docker name} -t
※ -i を付けるとコンテナへアタッチできる
※ -a では1コマンド実行できる

起動後のコンテナへログインする
$ docker exec -it {container name} {command (ex: /bin/sh)}

Docker image の作成
$ docker build -t {image name} {Dockerfile path}

Docker レジストリにログインする
$ docker login {host name}

image の名前を変更する (名前 == `$ docker images` で REPOSITORY として表示されるところ)
$ docker tag {変更前 name} {変更後 name}

レジストリに登録する時の命名規則
{registry}/{image name}:{version}

Docker レジストリに登録する
$ docker push {image name}

コンテナの標準出力を見る
$ docker logs {container name}

デタッチモードで実行
$ docker-compose up -d

終了
$ docker-compose down

ネットワークを見る
$ docker network ls

コンテナとホスト間でコピー
$ docker cp {copy元 file} {copy先 file}
※ docker 側はファイルの前にコンテナIDを付ける {container id}:{file path}

二項分布の計算と分布図

n=30, x=1~30でp=0.1, p=0.6, p=0.9の3通りについて二項分布を計算し、分布図を作成せよ。

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from scipy import stats

n = 30
p1, p2, p3 = 0.1, 0.6, 0.9

x = np.arange(0,n+1)
y1 = pd.Series(stats.binom.pmf(x, n, p1), index=x)
y2 = pd.Series(stats.binom.pmf(x, n, p2), index=x)
y3 = pd.Series(stats.binom.pmf(x, n, p3), index=x)

plt.plot(y1, 'o--', label='q=0.1')
plt.plot(y2, 'D--', label='q=0.6')
plt.plot(y3, '^--', label='q=0.9')
plt.legend(loc='upper center')
plt.xlabel('$x_{i}$')
plt.ylabel('$p (x | N, q)$')
plt.grid()

f:id:g5893:20170502165302p:plain

参考: Pythonで「データ解析のための統計モデリング入門」:第6章

Observableの使いかた

使い方だけメモ。

@Injectable()
export class Service {
  dataSource = new Subject<any>;
  dataStreams = this.dataSource.asObservable();

  getData {
    this.http.get(url)
        .subscribe((data: any) => {
          // データソースのnextに値を入れる度に
          // ストリームのsubscribeに値が流れる
          this.dataSource.next(data);
        });
  }
}
@Component({ provider: [ Service ] })
export class Component {
  data: any;

  constructor(private service: Service) {
    // データソースのnextに値が入る度に
    // ストリームからのsubscribeに値が流れる
    this.service.dataStream
        .subscribe((data: any) => {
          this.data = data;
        });
  }
}

Androidアプリ間での通信方法例

目的

AndroidアプリAとBがあるとして、
AからBのServiceを起動し、BのServiceでの処理結果をAで受け取るための方法です。

概要

A: BのServiceをintentに指定して投げます。BroadcastReceiverのonReceiveにてintentからExtraを受け取ります。
B: AのBroadcastReceiverをintentに指定して、Serviceの処理結果をputExtraしてbroadcastします。

実装例

Aサイド

AndroidManifest.xml

<manifest package="com.sample.A">
    <receiver
        android:name=".AsampleBroadcastReceiver"
        android:exported="true" />
</manifest>

Aのサービスを起動させる

Intent intent = new Intent();
intent.setClassName("com.sample.B", "com.sample.B.BsampleService");
startService(intent);

Bからのbroadcastを受け取る
AsampleBroadcastReceiver.java

public class AsampleBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        int hoge = bundle.getInt("key"); // キーを指定してint型の値を受け取る
    }
}
Bサイド

AndroidManifest.xml

<manifest package="com.sample.B">
    <service android:name=".BsampleService"
    android:exported="true" />
</manifest>

Aに対してbroadcastする
BsampleService.java

int value = 0;
Intent intent = new Intent();
intent.putExtra("key", value);  // int型の値を入れる
sendBroadcast(intent);

C言語の文字列比較

C言語では「文字列」を扱う変数はない。

「文字」の集合として扱っているため、
文字列を比較する際はループで1文字ずつ比較しなくてはならない。

比較できない例

if (str == "hogehoge"){
    printf("合致!");
}else{
    printf("合致しない...");
}

比較できる例

int compare_string(char str1[255], char str2[255]){
    int i, len = strlen(str1);
    for(i=0;i<len;i++){
        if(str1[i]!=str2[i]) return 0;
    }
    return 1;
}

if (compare_string(str1, "hogehoge")){
    printf("合致!");
}else{
    printf("合致しない...");
}