プログラミングと旅と映画の日々

普段はスマホ決済サービスの会社でバッグエンドを担当しているエンジニアです。プログラミングと趣味の映画、株、時々うどんに関してブログを書いていこうと思います。海外ドラマ、クロスバイクも好きです。

【Docker】Docker fileのCOPY命令で設定ファイルを保存【入門】

Docker fileのCOPY命令ADD命令

f:id:takanori5:20181028132956p:plain
COPY命令はホストマシン上のファイルをイメージ内にコピーする命令です。

nginxイメージ内に修正した設定ファイルの内容を保存していきたいと思います。

まずはnginxコンテナを立ち上げていきます。

$ docker run --name tmp-nginx --rm -d nginx
960e083f54488cf5815e4bd3bb6830b4135d27e2bfb6bbfadd28b7e8dcf4be5e

-rm オプションはdocker imagestop時にコンテナを自動削除するオプションです。

コンテナからホストへのコピー
$docker cp <コンテナID>:/etc/my.cnf my.cnf
ホストからコンテナへのコピー
$docker cp my.cnf <コンテナID>:/etc/my.cnf

では、やってみます

$ docker cp tmp-nginx:/etc/nginx/conf.d/default.conf ./
$ ls
default.conf

copyできました。
では設定ファイルに修正を加えます。

$ vim default.conf
#port番号を80から8080に変更します。
server {
    listen       8080;
    server_name  localhost;
...
}

以下のようなDockerfileを作成して、COPY命令を書きます。

nginxのイメージを元にしてCOPY命令を書きます。

$ cat Dockerfile
FROM nginx:latest
COPY default.conf /etc/nginx/conf.d/default.conf

ではbuildします。

$ docker build -t nginx:ver1 .
Sending build context to Docker daemon  4.096kB
Step 1/2 : FROM nginx:latest
 ---> dbfc48660aeb
Step 2/2 : COPY default.conf /etc/nginx/conf.d/default.conf
 ---> 9179d15926aa
Successfully built 9179d15926aa
Successfully tagged nginx:ver1

build成功。これを元にコンテナを起動します。

$ docker run --name web -p 8080:8080 --rm nginx:ver1
172.17.0.1 - - [02/Nov/2018:07:32:24 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36" "-"

8080ポートで接続できました。

【Docker】nginxイメージでウェブサーバー構築【入門】

nginxイメージでウェブサーバー構築

f:id:takanori5:20181028132956p:plain
https://hub.docker.com/_/nginx/

nginxのコンテナを立ち上げるコマンドは以下

docker run --name <コンテナ名> -d \
 -p <ホスト側のポート番号>:<コンテナ側のポート番号> \
 <イメージ名>

コマンドの末尾の\は複数業に続くコマンドを指定する場合に使う

  • d detachモード(バックグラウンんどで動作。常駐型のものは-dを指定しましょう)

実際にnginxコンテナを起動していきます。

$ docker run --name test-nginx -d -p 8080:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
f17d81b4b692: Pull complete
d5c237920c39: Pull complete
a381f92f36de: Pull complete
Digest: sha256:b73f527d86e3461fd652f62cf47e7b375196063bbbd503e853af5be16597cb2e
Status: Downloaded newer image for nginx:latest
5b2ea9c3c6deda192a973468c5d3619c2e0a57582c42a11165542a2da0c960ae

localhostの8080にアクセルして確認します。
すると以下のデフォルトの画面が出ると成功です!
dockerコンテナが動作していますね。
f:id:takanori5:20181102150020p:plain

nginxの80番ポートをコンテナを実行している仮想マシンの8080ポートにマッピング
仮想マシンの8080にアクセスされるとnginxのネットワークポート80番ポートに繋がる
nginxがコンテンツを返しデフォルトページが表示される。

f:id:takanori5:20181102150341p:plain

host上のhtmlファイルをnginxで公開

host上のディレクトリをコンテナにマウントしていきます。
nginxのhubのページをみにいきます。
以下に記載があります。
f:id:takanori5:20181102153550p:plain

  • vオプションがついていますね。
-v <host側のディレクトリ>:<コンテナ側のマウントポイント>:<オプション>
//例
$ docker run --name some-nginx -v /some/content:/usr/share/nginx/html:ro -d nginx

/usr/share/nginx/htmlはnginxのドキュメントルートディレクトリです。roオプションはリードオンリーの略。

今回は以下のようなディレクトリ構成で -vを指定して実行してみます。

-v USER/<ユーザー名>/docer-tutorial/html:/usr/share/nginx/html:ro

マウントを利用することでイメージにはソースコードは含めずに
gitでソースは管理して、マウントして修正内容などをすぐに確認できます。
超便利ですね!

マウントするように簡単なhtmlファイルを以下のディレクトリないに作成します。

/Users/hodzumitakanori/docker-tutorial/html

$ cat index.html
<!DOCTYPE html>
<html>
<body>
<h1>Hello takanori5!<h1>
</body>
</html>

コマンドを実行!

$ docker run --name nginx -v /Users/hodzumitakanori/docker-tutorial/html:/usr/share/nginx/html:ro -d -p 8080:80 nginx
c401ad4ccdc84fdaed5ce047a684ceb2a831eec85e803f1e93fe3c4ae5cb0eec

f:id:takanori5:20181102155441p:plain

【Docker】Docker hubへのイメージのプッシュ方法【入門】

Docker hubへのイメージのpush 方法

f:id:takanori5:20181028132956p:plain
リポジトリにイメージを追加してみます。

pushするにはDocker hubにログインする必要あり。
以下のコマンドを実行

$ docker login
hodzumitakanori-no-MacBook-Air:imagebuild hodzumitakanori$ docker login
Authenticating with existing credentials...
Login Succeeded

Login Succeeded!!

docker hubのタグ付けのルール

/:<タグ名>
タグ名の指定を省略すると自動でlatestタグと判定される

$ docker tag docker-whale takanori5/docker-whale:ver1
$ docker images
REPOSITORY          TAG                   IMAGE ID            CREATED             SIZE
takanori5/docker-whale   ver1                  0c788d9552c9        5 days ago          277MB

tag付けできました。

ではdocker hubにpushしていきます。

docker push <docker ID>/<image名>:<タグ名>
$ docker push takanori5/docker-whale:ver1
The push refers to repository [docker.io/takanori5/docker-whale]
288eb47d521e: Pushed
5f70bf18a086: Mounted from docker/whalesay
d061ee1340ec: Mounted from docker/whalesay
d511ed9e12e1: Mounted from docker/whalesay
091abc5148e4: Mounted from docker/whalesay
b26122d57afa: Mounted from docker/whalesay
37ee47034d9b: Mounted from docker/whalesay
528c8710fd95: Mounted from docker/whalesay
1154ba695078: Mounted from docker/whalesay

ver1: digest: sha256:dd05678a4931ce83947b376e5ab0c39a632f80f8504a7072bfc7de2ff2ee7635 size: 2614

push できました。


アップできています。
f:id:takanori5:20181102144315p:plain

試しにローカルのイメージを削除した上でpullしてみましょう。

//削除
$ docker rmi -f 0c788d9552c9
Untagged: hoz/docker-whale:ver1
Untagged: docker-whale:latest
Untagged: takanori5/docker-whale:ver1
Untagged: takanori5/docker-whale@sha256:dd05678a4931ce83947b376e5ab0c39a632f80f8504a7072bfc7de2ff2ee7635
Deleted: sha256:0c788d9552c9375eb30546618e033a2cf7d72bc7b216aa9997b64fea2bcc612b
Deleted: sha256:e77d0b9fa75be179620913aad7f08dbea9b4374b183940fbd7463305f9f5517c

//結果確認
$ docker images
REPOSITORY          TAG                   IMAGE ID            CREATED             SIZE
hello-world         latest                4ab4c602aa5e        7 weeks ago         1.84kB
mysql               5.7.21                5195076672a7        7 months ago        371MB
nginx               1.13.9-alpine         537527661905        8 months ago        17.9MB
php                 7.2.2-fpm-alpine3.6   59d3e94a05b7        8 months ago        77.2MB
docker/whalesay     latest                6b362a9f73eb        3 years ago         247MB

//hubからpull
$ docker pull takanori5/docker-whale:ver1
ver1: Pulling from takanori5/docker-whale
e190868d63f8: Already exists
909cd34c6fd7: Already exists
0b9bfabab7c1: Already exists
a3ed95caeb02: Already exists
00bf65475aba: Already exists
c57b6bcc83e3: Already exists
8978f6879e2f: Already exists
8eed3712d2cf: Already exists
fa71efe4e5ff: Already exists
Digest: sha256:dd05678a4931ce83947b376e5ab0c39a632f80f8504a7072bfc7de2ff2ee7635

これでこのレポジトリにアクセスできるアカウント保持者は
誰でもこのイメージを取得できるようになりました!!

【Docker】Docker fileを使用したイメージの構築方法【入門】

Docker fileを使用したイメージの構築方法

f:id:takanori5:20181028132956p:plain
Docker fileを使用してイメージを構築することをイメージビルドと呼ぶ

docker fileからイメージをビルドするコマンド

docker build -t hoge .
-t hoge(タグ名を指定)/ . ビルドコンテキストを指定(この場合current directory)

current directory にあるDockerfileがビルドされる

イメージビルド時にビルドコンテキストのファイルはDockerデーモンに転送されるので重いファイルは置かないように注意。

【Docker】Docker imageとは何か【入門】

Docker imageとは何か

f:id:takanori5:20181028132956p:plain

Docker imageとは

コンテナ実行に必要なファイルをまとめたファイルシステム
(ファイル例:OSのライブラリやアプリケーションなど)
imageは階層構造でデータが管理されていて
角層をレイヤーと呼ばれる
例えば、dockerイメージにnginxをインストールするイメージを実行すれば
レイヤーが一層積まれる感じ。
そして一度作成したイメージのレイヤーは全て読み取り専用で
変更不可。
f:id:takanori5:20181028134840p:plain

dockerイメージを元にコンテナを起動すると
新たに新しく読み書き可能なコンテナレイヤーが作成される
ただし、過去のレイヤーは読み取り専用。
無駄なファイルがイメージに含まれないようにイメージを作成することが重要。

イメージが大きくなると
Dockerのメリットである軽量であるということが薄れる

Dockerのイメージ継承について

以下の図のようにベースとしたいイメージを元にして
アプリレイヤーを構築できたりします。
超便利!!
f:id:takanori5:20181028135542p:plain

同じDockerイメージを継承した場合のメリット
centOSのイメージを同一にして
上位のレイヤーはRuby用、MySQL用という風にすることで
レイヤーの重複を排除でき、無駄なファイルのインストールをしなくてもよくなります。
通信料も節約できますね。
f:id:takanori5:20181028135900p:plain

【Docker】Docker hubとは何か【入門】

Docker hubとは

f:id:takanori5:20181028132956p:plain

Docker hubはDockerのレジストリサービス

Dockerイメージの公開、検索、ダウンロードが可能

 

https://hub.docker.com/

 

試しにexploreをみると

色々なレポジトリがありますね

f:id:takanori5:20181028133655p:plain

 

hello worldを検索すると多くのレポジトリが出ます

f:id:takanori5:20181028134013p:plain

スラッシュで区切られていないものがオフィシャルのレポジトリです。

Docker社がレビューしてオフィシャル判定しているらしいです。

f:id:takanori5:20181028134024p:plain

 

tagでver管理も可能

f:id:takanori5:20181028134334p:plain

以下のようにtag指定でイメージの取得も可能

>|ruby|

$ docker run hello-world:latest

||<

 

【Docker】Dockerでhello wold【Docker】

Dockerでhello worldしてみる

f:id:takanori5:20181028132956p:plain
dockerを今更ながら学習し始めました。

まずはhello world

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
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.
    (amd64)
 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 ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

dockerコマンドをterminal上から打つと
dockerクライアント(terminal)はdockerデーモンに接続する
そこでhello wolrdのイメージを探す
PC上に存在しない場合、docker hubというdocker社が管理している場所に
ネットワークを経由して取得しにいく
次にdocker hub上にhello wold imageが見つかったら
hubからimageをダウンロードしPCに保存。
保存したイメージを元にコンテナを起動する。
同じイメージが存在する場合はPCから起動するため起動が早くなる

試しにもう一度docer run コマンドを実行します。

$ docker run hello-world
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.
    (amd64)
 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 ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

docker run コマンドを紐解く

docker run コマンドでは以下の複数のコマンドが一括実行されている
image取得

docker pull

コンテナ作成

docker create

コンテナ起動

docker start

【Docker】Dockerでhello wold【Docker】 - プログラミングと旅と映画の日々