KAEDE Hack blog

JavaScript 中心に ライブラリなどの使い方を解説する技術ブログ。

Docker usage

Docker 使い方

f:id:kei_s_lifehack:20200701235224p:plain
Dockerの悪魔

qiita.com

Docker Container, Docker Image, Docker File, Docker yaml,

などがある

Intro, Dockerの悪魔

Docker AppをMacにInstall

Update

Docker Version

でClientとServerのVersionと

Containerに何があるか?を確認できる

RYOs-MacBook-Pro:code kaede$ docker version
Client: Docker Engine - Community
 Version:           19.03.2
 API version:       1.40
 Go version:        go1.12.8
 Git commit:        6a30dfc
 Built:             Thu Aug 29 05:26:49 2019
 OS/Arch:           darwin/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.2
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.8
  Git commit:       6a30dfc
  Built:            Thu Aug 29 05:32:21 2019
  OS/Arch:          linux/amd64
  Experimental:     false
containerd:
  Version:          v1.2.6

私のはClientもServerも19.03.02で

v1.2.6がcontainerd??されている.

hello-world-image

dockerがterminalで動いていれば

docker run hello-world

と打つだけで

RYOs-MacBook-Pro:code kaede$ 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/

とHello from Docker!が表示される.

これによるとDockerはこれで Docker クライアントが「Dockerの悪魔」にContact,契約をして

f:id:kei_s_lifehack:20200701235224p:plain

「Dockerの悪魔」がDocker hubから"hello-world"イメージを持ってくる

Dockerの悪魔は現在詠んでいるイメージを実行する新しいコンテナを作成する

Dockerの悪魔はDocker クライアント(私たちのlocal世界)にさっきのアウトプットを 流し,それが終末 (ターミナル) に送られる

これがDockerが動くステップらしい

docker images, docker ps,

Docker images

でlistを表示できる

RYOs-MacBook-Pro:code kaede$ docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
mongo                latest              1bc58f3232ec        10 months ago       413MB
python               2.7-slim            5f759d36aead        11 months ago       137MB
my-ruby-app          latest              869f833217cc        11 months ago       843MB
ruby                 2.5                 869f833217cc        11 months ago       843MB
ruby                 latest              d529acb9f124        11 months ago       840MB
golang               1.11-alpine         d9aba3165478        11 months ago       312MB
hello-world          latest              fce289e99eb9        18 months ago       1.84kB
username/repo        <none>              c7f5ee4d4030        3 years ago         182MB

githubのrepoをcloneしてくるように,localに置くものでは無いらしい.

docker ps

を使うと現在起動中のimageを見せてくれる

RYOs-MacBook-Pro:code kaede$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

現在は何も無いようだ.

docker ps -a

を使うと

RYOs-MacBook-Pro:code kaede$ docker ps -a                                                                                              
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS                
      NAMES                                                                                                                            
a140e5d70446        hello-world         "/hello"                 43 minutes ago      Exited (0) 43 minutes ago   
........

と終了したdocker imageもlistにできる.

wernight/funbox

hub.docker.com

こういうリフレッシュ用のDocker イメージがあると教えてもらった

docker run --rm -it wernight/funbox nyancat

すると...にゃーん!!!めちゃくちゃデカくてカラフル!!!

f:id:kei_s_lifehack:20200702001451p:plain

ただし52 + 400MBなので割と重いw

水槽が落ち浮く

f:id:kei_s_lifehack:20200702003308p:plain

tmuxで分割してもresponsiveに動くので,

上部を水槽にして開発することもできるwww

f:id:kei_s_lifehack:20200702003454p:plain

なおこの

docker run --rm -it wernight/funbox nyancat

の--rmは 終わったら自動でコンテナが消されるオプションであるが

イメージは消されないので毎回400MB DLされることはないらしい

(つまり消さないとMacの256GB SSDを圧迫し続ける)

悪魔に持ってきてもらったイメージからコンテナが作成されるんだったな

イメージはコンテナという開発環境の設計図なのか??

f:id:kei_s_lifehack:20200702005159p:plain

Dockerfile 書いてnodeのweb appを動かす

medium.com

まず

npm init

をすると対話形式で

package name, desc, auther, license,...

などを聞かれる.mainとversionなどはデフォルトがある

npm i express --save

でexpressを導入

index.jsに

//now it load express module with `require` directive
var express = require('express')
var app = express()
//Define request response in root URL (/) and response with text Hello World!
app.get('/', function (req, res) {
  res.send('Hello World!')
})
//Launch listening server on port 8080 and consoles the log.
app.listen(8080, function () {
  console.log('app listening on port 8080!')
})

でexpressを使って,

/ にアクセスされた時に

resでHello Worldをだす

8080を開いた時にconsole.logで8080を読み取っていると出る

コードを作成する

RYOs-MBP:docker-intro kaede$ node index.js
app listening on port 8080!

これでconsole.logの内容がterminal で見れた

いったん閉じて,curl

curl http://localhost:8080/

するとrefuseされる

開いたまま別のpainでやると

RYOs-MacBook-Pro:docker-intro kaede$ curl http://localhost:8080/
Hello World!RYOs-MacBook-Pro:docker-intro kaede$ 

resのHello World!が帰ってきた

このdirectoryでDockerfileを作成

## it uses node js image alpine version from image registries.
FROM node:8.16.1-alpine
## it sets directory in the container to /app to store files and launch our app.
WORKDIR /app
## it copies the app to /app directory with dependencies.
COPY package.json /app
RUN npm install
COPY . /app
## it commands to run our app which is index.js.
CMD node index.js
##  it exposes the port where our app is running that is port 8080.
EXPOSE 8080

docker build -t web .

でビルドする

-t web はわからない

Docker composeとは

www.tohoho-web.com

Docker Compose は、Webサーバコンテナと DBサーバコンテナなど、関連する複数のコンテナをまとめて管理するツールです。

らしい.そもそもwebでは複数のコンテナを作成するのか.

さっきのweb のコマンドオプションはwebのコンテナって意味かもしれない.

とほほさんの記事を見るとcurlでdocker-composeを入れろって書いてあるが

dockerだけ入っている状態でdoocker-compose -hができた.

docker-compose.ymlを作成

version: ‘3’ # version of docker-compose
services: # defining service/s
  web: # name of the service
    image: nodejs/hello # image named given
    build: . # directory what to build, here it is root directory
    ports:
      – “8080:8080” # defining port for our app

todo: docker hubとgithubの違いをまとめる

Dockerの認識

私はさっきカラフルビックニャンコとアクアリウムをDocker Hubから持ってきた

のがあって,github: ソースコード置くところ

の環境版,つまり

Docker: 開発環境のまとまり,ファイル群置くところ

といった認識があった.

リーダーにそのことを発言したら