Last active
February 2, 2020 07:59
-
-
Save kkeisuke/6d33689a17203007656a486e8c197219 to your computer and use it in GitHub Desktop.
Docker + Go + Gin の開発環境を準備する ref: https://qiita.com/kkeisuke/items/7cd4d5834386666faab3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Dockerfile* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: '3' | |
# マウント時に vendor 以下を削除してしまうため、別 volume を指定する | |
# Gopkg.toml を編集する場合、vendor ボリュームも削除しイメージを再構築する | |
volumes: | |
vendor: | |
services: | |
api: | |
build: | |
context: . | |
dockerfile: docker/api/Dockerfile | |
volumes: | |
- ./src/api:/go/src/api | |
- vendor:/go/src/api/vendor | |
ports: | |
- 3001:3001 | |
tty: | |
true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# API Image 作成 | |
# ベースとなるイメージ | |
FROM golang:1.10.2-alpine3.7 | |
# Gopkg.toml 等を事前にコピーして dep ensure が実行できるようにする | |
COPY src/api /go/src/api/ | |
# dep ensure を行うプロジェクトルートに移動する | |
WORKDIR /go/src/api/ | |
# 必要なパッケージをイメージにインストールする | |
RUN apk update \ | |
&& apk add --no-cache git \ | |
&& go get -u github.com/codegangsta/gin \ | |
&& go get -u github.com/golang/dep/cmd/dep \ | |
&& dep ensure | |
# コンテナ実行時のデフォルトを設定する | |
# ライブリロードを実行する | |
CMD gin -i run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. | |
├── .dockerignore | |
├── docker | |
│ └── api | |
│ └── Dockerfile | |
├── docker-compose.yml | |
└── src | |
└── api | |
├── Gopkg.lock | |
├── Gopkg.toml | |
└── main.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Dockerfile* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# API Image 作成 | |
# ベースとなるイメージ | |
FROM golang:1.10.2-alpine3.7 | |
# Gopkg.toml 等を事前にコピーして dep ensure が実行できるようにする | |
COPY src/api /go/src/api/ | |
# dep ensure を行うプロジェクトルートに移動する | |
WORKDIR /go/src/api/ | |
# 必要なパッケージをイメージにインストールする | |
RUN apk update \ | |
&& apk add --no-cache git \ | |
&& go get -u github.com/codegangsta/gin \ | |
&& go get -u github.com/golang/dep/cmd/dep \ | |
&& dep ensure | |
# コンテナ実行時のデフォルトを設定する | |
# ライブリロードを実行する | |
CMD gin -i run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[[constraint]] | |
name = "github.com/gin-gonic/gin" | |
version = "1.2.0" | |
[prune] | |
go-tests = true | |
unused-packages = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "github.com/gin-gonic/gin" | |
func main() { | |
r := gin.Default() | |
r.GET("/ping", func(c *gin.Context) { | |
c.JSON(200, gin.H{ | |
"message": "ping", | |
}) | |
}) | |
// ポートを設定しています。 | |
r.Run(":3001") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment