git基礎: ブランチ

ブランチとは履歴の枝分かれ
変更をmasterブランチに取り込むことをマージ

program.txt

春の大運動会
* 開会式
* 閉会式

$ git add program.txt
$ git commit -m “プログラム作成開始”

### ブランチを作る
git branch ブランチ名
$ git add -u
※バージョン管理されていて、変更があったすべてのファイルがaddされる
$ git commit -m “玉関係の競技追加”
$ git branch proc
$ git branch
$ git branch -v
* master 69c78cb 玉関係の競技追加
proc 69c78cb 玉関係の競技追加
git branchに-vオプションを与えると、ブランチの先頭があるコミット情報も表示される

$ git add -u
$ git commit -m “走る競技追加”
$ git branch -v
* master 66006b7 走る競技追加
proc 69c78cb 玉関係の競技追加

### ブランチ切り替え
git checkout ブランチ
$ git checkout proc
$ git branch
$ cat program.txt
春の大運動会
* 開会式
* 玉入れ
* 大玉転がし
* 閉会式

$ git checkout master
$ git branch
$ cat program.txt
春の大運動会
* 開会式
* 玉入れ
* 大玉転がし
* 二人三脚
* 徒競走
* 閉会式

$ git log –all –graph
$ git log –all –graph –oneline
* df1adc9 (proc) 選手宣誓追加
| * 66006b7 (HEAD -> master) 走る競技追加
|/
* 69c78cb 玉関係の競技追加
* 38d5fe3 プログラム作成開始

### ブランチのマージ
$ git merge proc
$ cat program.txt
春の大運動会
* 開会式
* 選手宣誓
* 玉入れ
* 大玉転がし
* 二人三脚
* 徒競走
* 閉会式

$ git ls-files -s
100644 9dc5b70af547cf38876fd39e696ca12944609018 1 program.txt
100644 8dcdb3703137b61fd6286001320d39045e9ab679 2 program.txt
100644 1203c826d8f52af84d75362c8a59f69fa399a473 3 program.txt

### ブランチを削除
git branch -d ブランチ
$ git checkout master
$ git branch -d proc

なるほどー ブランチの概念はわかったわ

git基本コマンド2

git diff: ファイルの差分, 最後にstageした後に何を変更したかの確認によく使う
git diff –staged, git diff HEAD

$ git diff
diff –git a/hello.txt b/hello.txt
index 05a682b..4a49d86 100644
— a/hello.txt
+++ b/hello.txt
@@ -1 +1,2 @@
-Hello!
\ No newline at end of file
+Hello!
+I am a student.

### 履歴の中で違いを見る
git diff コミット1 コミット2
git diff コミット1 コミット2 — ファイル
$ git diff 7aad1 HEAD
$ git diff HEAD^ HEAD
$ git diff HEAD~1 HEAD
$ git diff @^^ @

headは@とも書ける

### コミット情報の表示
git show: 直近のコミットがどんなものだったかを表示する
$ git show

### 一度ステージした内容を変更
ファイルに変更を追加してgit add -uなどとする

### ステージの内容を見る
$ git ls-files

詳細を見る場合は-sオプションをつける
$ git ls-files -s
$ git show a5842752b

### ステージしたことを無かったことにする
git reset
git reset –file
git reset –hard   # ツリーの状態をHEADの上に戻す
$ git reset –hard
変更したものが、HEADの状態に戻る

### ステージした後の変更をなしにする
git checkout — file
$ git checkout — hello.txt
$ cat hello.txt

### ファイル名を変える
git mv 元のファイル 新しいファイル

### ファイルを管理下から外す
git rm ファイル

ステージだけから取り除き、作業ツリーにあるものは残したい場合 –cachedをつける
git rm –cached file
git rm -r –cached dir

### 昔のファイルをリポジトリから取り出す
$ git checkout HEAD~3 –hello.txt

チェックアウトせずに昔のファイルを見る
$ git show HEAD~3:hello.txt

$ git checkout HEAD~3 — hello.txt
$ git show HEAD~3:hello.txt

### 作業ツリーの内容をHEADの状態にする
$ git reset –hard
$ ls

### 最近の履歴を無かったものにする
$ git reset –hard コミット
$ git reset a82684a02
$ git log

### 直近のコミットをやり直す
$ git commit –amend

### gitにファイルを無視させる
.gitignoreを使う

*.log
!test.log

$ git commit -m “8th commit”

使いながら覚えないとどうにもならんな これ…

git基本コマンド1

$ git help
$ git help help

### git config
Gitの設定ファイルにある変数の値を設定
/etc/gitconfig : マシン全体に対する設定 –sytemオプションを使う
~/.gitconfig: ユーザごとの設定 –globalオプション
.git/config: プロジェクトごとの設定

$ git config –global -l
user.email=hoge@gmail.com
user.name=Hpscript

### バージョン管理を始める
$ git init
Initialized empty Git repository in /home/vagrant/dev/git/.git/

### ファイルやディレクトリを管理下に入れる
$ touch README.txt
$ git add README.text

### コミットしてリポジトリに入れる
既にステージにある場合は git add -uを使う
$ git commit -m “first commit”
$ git add -u
$ git commit -m “second commit”

### 履歴を見る
$ git log
$ git log –oneline
7aad1d5 (HEAD -> master) second commit
8a0580a first commit

### ファイルを追加
hello.txt
$ git add .

### どのファイルを新たにステージしたか見る
$ git status
※ファイルを追加した場合、new file, 編集した場合はmodified
$ git add -u
$ git status
※stageされてない場合は untracked fileと表示される

うむ、基礎操作から結構大変だな

[Docker] スタンドアローンSwarmのオーバーレイネットワーク

オーバーレイネットワーク: 複数のホストを跨いだネットワーキングに使用されるネットワークドライバ
L ホストのDockerの情報を保存しておくためのキーバリューストアと呼ばれるデータを保持するサーバが必要
L Consul, Etcd, Zookeeperなどがある

TCP port: 2377
TCP/UDP port: 7946
UDP port: 4789

うむ、これはよくわからんわ…

[Docker] ノンネットワークとホストネットワーク

ノンネットワーク: ネットワークインターフェイスを持たない状態にしたい場合に使用するネットワーク
ホストネットワーク: Dockerホストと同じネットワークインターフェースをコンテナで使用したい場合に使用するネットワーク

$ sudo docker network ls
562eb5e84daa none null local … none

$ sudo docker network disconnect my_nw ubuntu1
$ sudo docker network disconnect bridge ubuntu1
$ sudo docker network connect none ubuntu1
$ sudo docker inspect ubuntu1

                "none": {
                    "IPAMConfig": {},
                    "Links": null,
                    "Aliases": [],
                    "NetworkID": "562eb5e84daad5b15117ed125a41d3cffaaf9884095f55949dd92bf1a9533620",
                    "EndpointID": "30ccbdc2f734fede12c9bbd56b4e2b9fa27c0b988b5d7feca224548c4c8622ce",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": {}
                }

ホストネットワークの場合は、ポートフォワーディング設定を行わなくても直にnginxにアクセスできる

なるほど、インフラも奥が深いな…

[Docker] ユーザ定義のブリッジネットワーク作成

デフォルトのブリッジネットワークとユーザ定義のブリッジネットワーク

ubuntuコンテナを起動して詳細を確認します
$ sudo docker run -itd –name ubuntu1 ubuntu /bin/bash

$ sudo docker inspect ubuntu1
L ipアドレスの箇所で 172.17.0.2 が割り当てられている

            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "9d63e39880cdedc5addbd10a11deb3fad9b4679dca150c8ab24c6080a777d0a5",
                    "EndpointID": "d990aa1ffeb44365cc97df34730bf143b2a0b0afbe461fe811b877fca9ad390f",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }

もう一つ同じようにコンテナを作成する
$ sudo docker run -itd –name ubuntu2 ubuntu /bin/bash
$ sudo docker inspect ubuntu2
L ipアドレスの箇所で 172.17.0.3 が割り当てられている

            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "9d63e39880cdedc5addbd10a11deb3fad9b4679dca150c8ab24c6080a777d0a5",
                    "EndpointID": "ea06868987306ff7b8f7419a287d4a8d7282914f2c9746afbe991d9d26e2d657",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:03",
                    "DriverOpts": null
                }
            }

$ sudo docker exec -ti 77573860f37e /bin/bash
$ apt-get update && apt install iputils-ping
root@77573860f37e:/# ping -c 3 172.17.0.3
PING 172.17.0.3 (172.17.0.3) 56(84) bytes of data.
64 bytes from 172.17.0.3: icmp_seq=1 ttl=64 time=0.079 ms
64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.064 ms
64 bytes from 172.17.0.3: icmp_seq=3 ttl=64 time=0.099 ms

— 172.17.0.3 ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2027ms
rtt min/avg/max/mdev = 0.064/0.080/0.099/0.014 ms
root@77573860f37e:/# ping -c 3 ubuntu2
ping: ubuntu2: Name or service not known

### コンテナ名で名前解決する方法
$ sudo docker network create my_nw
$ sudo docker network connect my_nw ubuntu1
$ sudo docker network connect my_nw ubuntu2
$ sudo docker exec -ti 77573860f37e /bin/bash
root@77573860f37e:/# ping -c 3 ubuntu2
PING ubuntu2 (172.22.0.3) 56(84) bytes of data.
64 bytes from ubuntu2.my_nw (172.22.0.3): icmp_seq=1 ttl=64 time=0.141 ms
64 bytes from ubuntu2.my_nw (172.22.0.3): icmp_seq=2 ttl=64 time=0.083 ms
64 bytes from ubuntu2.my_nw (172.22.0.3): icmp_seq=3 ttl=64 time=0.068 ms

おおおおおおお

$ sudo docker network ls

Docker基礎2

Dockerコンテナの状態
– created, running, exited, removing, paused, restarting, deadの状態がある

### コンテナへのシェル接続
$ sudo docker run -tid alpine /bin/sh

-tフラグ: 擬似ターミナル(peudo-TTY)を割り当てる
-iフラグ: アタッチされていない状態でも標準入力を保持するためのフラグ
-dフラグ: detachedモードで動作させる場合のフラグ

うむ、なるほど、基礎中の基礎はわかってきた。

Docker基礎

Docker Machine: Docker Engineがインストールされた仮想マシンを作成したり管理するためのツール
Docker Swarm: Dockerのインストールされたサーバでクラスタ構成を組んだり、コンテナ数をスケールさせるなどのオーケストレーション

### Dockerエディション
– Docker CE(Community Edition)
– Docker EE(Enterprise Edition)

### Dockerのバージョン
$ sudo docker -v
Docker version 20.10.12, build e91ed57
2020年10月リリース リビジョン12

### Explore
Docker Hubで登録されているイメージを検索することができる
レジストリはこれ以外にもQuay.io, ECR(AWS), Container Registry(GCP)などがある
オフィシャルのイメージはリポジトリ名だが、ユーザがアップロードしたものは ${Docker ID} / ${イメージ名}のリポジトリ名

### Dockerイメージとは
・コンテナ実行に必要なファイルやメタ情報をまとめたのがイメージ
・AUFSなど特殊なファイルシステムが使用されている
・イメージ上のファイルやメタ情報はレイヤと呼ばれる階層構造で構成され、読み取り専用(編集不可)となっている
・コンテナはデフォルトで外部(Docker Engineの動作しているサーバ外)と通信できないようになっているため、必要に応じてポートフォワーディングの設定を行い、外部からアクセスできるようにする必要がある

### コンテナのコマンド実行
$ sudo docker run docker/whalesay cowsay boo
_____
< boo >
—–
\
\
\
## .
## ## ## ==
## ## ## ## ===
/””””””””””””””””___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\______/

“docker/whalesay”が使用するイメージで、”cowsay boo”がコンテナ内で呼び出すコマンド

$ sudo docker run docker/whalesay cowsay “hello! I’m a whale”

Linuxの基本コマンドも実行できる
$ sudo docker run docker/whalesay ls -la
total 68
drwxr-xr-x 1 root root 4096 May 25 2015 .
drwxr-xr-x 1 root root 4096 Mar 13 05:38 ..
drwxr-xr-x 1 root root 4096 May 25 2015 .git
-rw-r–r– 1 root root 931 May 25 2015 ChangeLog
-rw-r–r– 1 root root 385 May 25 2015 INSTALL
-rw-r–r– 1 root root 1116 May 25 2015 LICENSE
-rw-r–r– 1 root root 445 May 25 2015 MANIFEST
-rw-r–r– 1 root root 1610 May 25 2015 README
-rw-r–r– 1 root root 879 May 25 2015 Wrap.pm.diff
drwxr-xr-x 1 root root 4096 May 25 2015 cows
-rwxr-xr-x 1 root root 4129 May 25 2015 cowsay
-rw-r–r– 1 root root 4690 May 25 2015 cowsay.1
-rw-r–r– 1 root root 54 May 25 2015 install.pl
-rwxr-xr-x 1 root root 2046 May 25 2015 install.sh
-rw-r–r– 1 root root 631 May 25 2015 pgp_public_key.txt

docker runコマンドの引数にコンテナ起動後に実行したいコマンドをつなげることで、指定したコマンドをコンテナで実行することができる
DockerfileにCMD命令を記述することで、作成したイメージからコンテナを起動した際にデフォルトで実行されるコマンドを設定できる

FROM docker/whalesay:latest

RUN apt-get -y update && apt-get install -y fortunes

CMD /usr/games/fortune | cowsay

$ sudo docker build -t fortune-whale .
$ sudo docker run fortune-whale
______________________
/ Hangover, n.: \
| |
\ The burden of proof. /
———————-
\
\
\
## .
## ## ## ==
## ## ## ## ===
/””””””””””””””””___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\______/

### nginx
$ docker run -p 8080:80 nginx:1.21.6
detachedモードを意味する-dフラグを付けなかった場合、プロセスの入出力にattachされた状態になるため、-dをつける
-tと-iオプションをつけた場合は、コンテナを起動したままでも接続を解除できる。よく-tiとして使用する。

$ docker cp ${containerId}:/etc/nginx nginx
$ ls

$ docker cp nginx/nginx.conf ${containerId}:/etc/nginx/nginx.conf
$ docker stop ${containerId}
$ docker start ${containerId}

なるほど、超重要な概念のところを理解した。

dockerのログに関する整理

コンテナ内に入ってログを見る方法がある
解決法として以下のコマンドで実行できる
$ docker logs コンテナ名

dockerのコンテナログはコンテナの標準出力(stdout), 標準エラー出力に書き込まれた内容を表示する

eginx

access_log  /dev/stdout;
error_log   /dev/stderr;

wordpress

$path = 'php://stderr';

うわ、これ結構ヤバいな
dockerを勉強しないと…

ちなみに、ecsでログの出力先をcloudWatch logsに設定できる