【Docker】Docker ~ GO言語 ~

■ はじめに

 Docker をなんとなく使っていて
ちゃんと基本的なことを理解していなく
いきあたりばったりだったので、
図書館で本(補足1)を借りた。

 その中で、Go言語を使ったサンプルが出ていたので
参考にして、現状にあうように書き換えたりとかした
チュートリアル(Hello world)的なことをやってみる。

補足1:借りた本について

補足2:Python だったら

* 以下のサイトがよさげ。

https://www.ogis-ri.co.jp/otc/hiroba/technical/docker/part2.html

目次

【0】目的
 1)全体の流れ
 2)フォルダ構成
 3)独自アプリ(Go言語)のサンプル
 4)動作確認環境
【1】Dockerfile 作成
 1)サンプル
 2)説明
【2】Dockerコマンドでデプロイ
 0)コマンド例
 1)説明 - docker image -
 2)説明 - docker container -

【0】目的

簡単な独自アプリ(今回は、Go言語)を作成し、
それをDockerを使って、デプロイし、アクセスする

1)全体の流れ

* Docker で アプリをデプロイする際には、
 大きく以下のような手順にすればいい。

[1] 対象アプリの用意

* 今回はGo言語のアプリ。
* 後述「3)独自アプリ(Go言語)のサンプル」参照。

[2] Dockerfile 作成

* 後述「【1】Dockerfile 作成」参照。

[3] Dockerコマンドによるデプロイ

* 後述「【2】Dockerコマンドでデプロイ」参照。

2)フォルダ構成

+ main.go ... 独自アプリ(Go言語)
+ Dockerfile ... Dockerfile

3)独自アプリ(Go言語)のサンプル

main.go

package main

import (
    "fmt"
    "log"
    "net/http"
)

func main() {
    // Register simple function for Hello world!
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        log.Println("Received")
        fmt.Fprintf(w, "Hello World!!")
    })
    log.Println("Start server")
    http.ListenAndServe(":8080", nil)

    // Start the web server
    if err := http.ListenAndServe(":8080", nil); err != nil {
        log.Fatal("ListenAndServe:", err)
    }
}

4)動作確認環境

* 以下の関連記事で紹介した「katacoda」を使ってもいいかも。

学習用クラウドサービス ~ katacoda ~
https://dk521123.hatenablog.com/entry/2021/07/15/211044

【1】Dockerfile 作成

* アプリに沿ったDockerfile を作成する。

1)サンプル

Dockerfile

FROM golang:1.17.6

RUN mkdir /hello_world
COPY main.go /hello_world

CMD ["go", "run", "/hello_world/main.go"]

2)説明

* Dockerfile 内の「FROM」「RUN」などを
 「インストラクション(命令)」と呼ぶ
* 下記以外のインストラクションについては、以下の関連記事を参照のこと。

https://dk521123.hatenablog.com/entry/2020/04/14/000000

a) FROM

* ベースとなるイメージ
 => 「FROM golang:1.17.6」なので、
  Go言語 v1.17.6のイメージをベースにしている

b) RUN

* docker build 時に実行するコマンド
 => 「RUN mkdir /hello_world」なので、
  ディレクトリ名「hello_world」を作成

c) COPY

* ホストからコンテナへのファイルコピー
 => 「COPY main.go /hello_world」なので、
  ホストにある「main.go(独自Go言語アプリ)」を
  コンテナ内のディレクトリ名「hello_world」配下にコピー

d) CMD

* docker run 時に実行するコマンド
 => 「CMD ["go", "run", "/hello_world/main.go"]」なので、
  コンテナ内の「/hello_world/main.go」をgoコマンドで
  実行する

【2】Dockerコマンドでデプロイ

* 大きな流れとしては、
1)DockerファイルからDockerイメージを作成
2)作成したDockerイメージから新しいコンテナを実行

0)コマンド例

# 1)DockerファイルからDockerイメージを作成
docker image build -t hello/world:latest .

# 作成したDockerイメージを確認
docker image ls

# 2)作成したDockerイメージから新しいコンテナを実行
docker container run -d -p 9000:8080 hello/world:latest

# 作成したDockerコンテナを確認
docker container ls

# 独自アプリにアクセスする
curl http://localhost:9000

1)説明 - docker image -

* 使用した docker コマンドのDocker イメージについて、解説をしていく

[1] docker image build

* Dockerfile からイメージをビルドする
* 「-t」は「--tag」で、書式 'name:tag' により
 名前および任意のタグを指定する
 => 「-t hello/world:latest」なので
  名前「hello/world」でタグ「latest」

https://matsuand.github.io/docs.docker.jp.onthefly/engine/reference/commandline/image_build/

[2] docker image

* Dockerイメージを管理

https://matsuand.github.io/docs.docker.jp.onthefly/engine/reference/commandline/image/

2)説明 - docker container -

* 使用した docker コマンドのDocker コンテナについて、解説をしていく

[1] docker container run

* 新しいコンテナを実行
* 旧コマンド「docker run」と同じ

https://matsuand.github.io/docs.docker.jp.onthefly/engine/reference/commandline/container_run

[2] docker container ls

* コンテナ一覧の表示
* 旧コマンド「docker ps」と同じ

https://matsuand.github.io/docs.docker.jp.onthefly/engine/reference/commandline/container_ls/

参考文献

https://www.tohoho-web.com/docker/dockerfile.html
https://qiita.com/zembutsu/items/6e1ad18f0d548ce6c266

関連記事

Docker ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2020/04/24/160044
Docker ~ Windows / 環境構築編 ~
https://dk521123.hatenablog.com/entry/2017/09/23/235818
Docker ~ Linux / 環境構築編 ~
https://dk521123.hatenablog.com/entry/2018/04/10/234030
Docker ~ 基本編 / docker network ~
https://dk521123.hatenablog.com/entry/2022/04/30/000000
Docker ~ 基本編 / Data Volume ~
https://dk521123.hatenablog.com/entry/2018/09/08/222100
Docker ~ 基本編 / Dockerfile ~
https://dk521123.hatenablog.com/entry/2020/04/14/000000
Docker ~ 基本編 / dockerコマンド ~
https://dk521123.hatenablog.com/entry/2020/04/13/000000
Docker ~ 基本編 / docker-compose ~
https://dk521123.hatenablog.com/entry/2020/04/11/000000
Docker ~ Webサーバ ~
https://dk521123.hatenablog.com/entry/2022/02/22/000000
Docker ~ WordPress
https://dk521123.hatenablog.com/entry/2022/04/29/000000
学習用クラウドサービス ~ katacoda ~
https://dk521123.hatenablog.com/entry/2021/07/15/211044