[Windows] Dockerを使用してホスト環境を汚さずにgqlgenの開発環境を構築する

### TL;DR
- Go言語の開発環境をDockerをまとめたかった
- ローカル環境にはDocker Desktopとvisual studio codeのみインストール
- gqlgenでapiサーバを構築している想定
- vs codeのリモートコンテナ機能を使ってみた
- リモートコンテナ使うとgitの管理を考えないといけないことに気がついて頭が痛い

### 環境
- Windows10
- Docker Desktop 2.2.0.3
- docker-composeはDocker Desktopに同梱
- visual studio code 1.42.1[Remode Development使用]
- cosmtrek/air オートリロード

### 完成したリポジトリ
https://github.com/MegaBlackLabel/go-docker-sample

### ファイル
```
\---go-docker-sample
│  .gitignore
│  docker-compose.yml
│  docker-entrypoint.sh
│  Dockerfile
│  LICENSE
│  README.md
│
├─.devcontainer
│      devcontainer.json
│
└─api
    │  .air.conf
    │  go.mod
    │  go.sum
    │  gqlgen.yml
    │  server.go
    │
    ├─.vscode
    │      settings.json
    │
    ├─graph
    │  │  resolver.go
    │  │  schema.graphqls
    │  │  schema.resolvers.go
    │  │
    │  ├─generated
    │  │      generated.go
    │  │
    │  └─model
    │          models_gen.go
    │
    └─tmp
            server
```

```Dockerfile:Dockerfile
#-------------------------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
#-------------------------------------------------------------------------------------------------------------

FROM golang:1

# Avoid warnings by switching to noninteractive
ENV DEBIAN_FRONTEND=noninteractive

# This Dockerfile adds a non-root user with sudo access. Use the "remoteUser"
# property in devcontainer.json to use it. On Linux, the container user's GID/UIDs
# will be updated to match your local UID/GID (when using the dockerFile property).
# See https://aka.ms/vscode-remote/containers/non-root-user for details.
ARG USERNAME=vscode
ARG USER_UID=1000
ARG USER_GID=$USER_UID

# Configure apt, install packages and tools
RUN apt-get update \
    && apt-get -y install --no-install-recommends apt-utils dialog 2>&1 \
    #
    # Verify git, process tools, lsb-release (common in install instructions for CLIs) installed
    && apt-get -y install git iproute2 procps lsb-release \
    #
    # Install Go tools w/module support
    && mkdir -p /tmp/gotools \
    && cd /tmp/gotools \
    && GO111MODULE=on go get -v golang.org/x/tools/gopls@latest 2>&1 \
    && GO111MODULE=on go get -v \
        honnef.co/go/tools/...@latest \
        golang.org/x/tools/cmd/gorename@latest \
        golang.org/x/tools/cmd/goimports@latest \
        golang.org/x/tools/cmd/guru@latest \
        golang.org/x/lint/golint@latest \
        github.com/mdempsky/gocode@latest \
        github.com/cweill/gotests/...@latest \
        github.com/haya14busa/goplay/cmd/goplay@latest \
        github.com/sqs/goreturns@latest \
        github.com/josharian/impl@latest \
        github.com/davidrjenni/reftools/cmd/fillstruct@latest \
        github.com/ramya-rao-a/go-outline@latest  \
        github.com/acroca/go-symbols@latest  \
        github.com/godoctor/godoctor@latest  \
        github.com/rogpeppe/godef@latest  \
        github.com/zmb3/gogetdoc@latest \
        github.com/fatih/gomodifytags@latest  \
        github.com/mgechev/revive@latest  \
        github.com/go-delve/delve/cmd/dlv@latest 2>&1 \
    #
    # Install Go tools w/o module support
    && go get -v github.com/alecthomas/gometalinter \
        github.com/uudashr/gopkgs/cmd/gopkgs 2>&1 \
    #
    # Install gocode-gomod
    && go get -x -d github.com/stamblerre/gocode 2>&1 \
    && go build -o gocode-gomod github.com/stamblerre/gocode \
    && mv gocode-gomod $GOPATH/bin/ \
    #
    # Install golangci-lint
    && curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin 2>&1 \
    #
    # Create a non-root user to use if preferred - see https://aka.ms/vscode-remote/containers/non-root-user.
    && groupadd --gid $USER_GID $USERNAME \
    && useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME \
    # [Optional] Add sudo support
    && apt-get install -y sudo \
    && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
    && chmod 0440 /etc/sudoers.d/$USERNAME \
    # Add write permission for /go/pkg
    && chmod -R a+rwX /go/pkg \
    #
    # Clean up
    && apt-get autoremove -y \
    && apt-get clean -y \
    && rm -rf /var/lib/apt/lists/* /go/src /tmp/gotools

# Update this to "on" or "off" as appropriate
ENV GO111MODULE=auto

# Switch back to dialog for any ad-hoc use of apt-get
ENV DEBIAN_FRONTEND=dialog

# Add
RUN curl -fLo /go/bin/air https://git.io/linux_air \
  && chmod +x /go/bin/air

WORKDIR /
COPY docker-entrypoint.sh ./

# dir
RUN mkdir /go/api

WORKDIR /go/api

EXPOSE 5000

ENTRYPOINT ["/docker-entrypoint.sh"]
```
※、vscode-remote-try-goのDockerfileを流用

```yaml:docker-compose.yml
version: '3'

services:
version: '3'

services:
  api:
    build: .
    ports:
        - "5000:5000"
    volumes:
        - "./api:/go/api"
    tty: true
```

```shell:docker-entrypoint.sh
#!/bin/bash
 
FILE_MOD="/go/api/go.mod"
if [ -e $FILE_MOD ]; then
  go mod download
fi

/bin/bash
```

```json:devcontainer.json
// For format details, see https://aka.ms/vscode-remote/devcontainer.json or this file's README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.106.0/containers/docker-existing-docker-compose
// If you want to run as a non-root user in the container, see .devcontainer/docker-compose.yml.
{
	"name": "Existing Docker Compose (Extend)",

	// Update the 'dockerComposeFile' list if you have more compose files or use different names.
	// The .devcontainer/docker-compose.yml file contains any overrides you need/want to make.
	"dockerComposeFile": [
		"..\\docker-compose.yml"
	],

	// The 'service' property is the name of the service for the container that VS Code should
	// use. Update this value and .devcontainer/docker-compose.yml to the real service name.
	"service": "api",

	// The optional 'workspaceFolder' property is the path VS Code should open by default when
	// connected. This is typically a file mount in .devcontainer/docker-compose.yml
	"workspaceFolder": "/go/api",

	// Set *default* container specific settings.json values on container create.
	"settings": { 
		"terminal.integrated.shell.linux": "/bin/bash",
		"go.useLanguageServer": true
	},

	// Add the IDs of extensions you want installed when the container is created.
	"extensions": [
		"ms-vscode.go"
	],

	// Uncomment the next line if you want start specific services in your Docker Compose config.
	// "runServices": [],

	// Uncomment the next line if you want to keep your containers running after VS Code shuts down.
	// "shutdownAction": "none",

	// Uncomment the next line to run commands after the container is created - for example installing git.
	// "postCreateCommand": "apt-get update && apt-get install -y git",

	// Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root.
	"remoteUser": "vscode"
}
```

```conf:.air.conf
# Config file for [Air](https://github.com/cosmtrek/air) in TOML format

# Working directory
# . or absolute path, please note that the directories following must be under root.
root = "."
tmp_dir = "tmp"

[build]
# Just plain old shell command. You could use `make` as well.
cmd = "go build -o ./tmp/server ./server.go"
# Binary file yields from `cmd`.
bin = "tmp/server"
# Customize binary.
full_bin = "APP_ENV=dev APP_USER=air ./tmp/server"
# Watch these filename extensions.
include_ext = ["go", "tpl", "tmpl", "html"]
# Ignore these filename extensions or directories.
exclude_dir = ["assets", "tmp", "vendor"]
# Watch these directories if you specified.
include_dir = []
# Exclude files.
exclude_file = []
# It's not necessary to trigger build each time file changes if it's too frequent.
delay = 1000 # ms
# Stop to run old binary when build errors occur.
stop_on_error = true
# This log file places in your tmp_dir.
log = "air_errors.log"

[log]
# Show log time
time = false

[color]
# Customize each part's color. If no color found, use the raw app log.
main = "magenta"
watcher = "cyan"
build = "yellow"
runner = "green"

[misc]
# Delete tmp directory on exit
clean_on_exit = true
```

### docker-composeビルド
```shell:コンテナのビルドを実施
docker-compose build
```

### リモートコンテナ接続
- vs codeのコマンドパレットを開いて以下を入力
- Remote-containers: Reopen in Container

### Go Modulesの設定・gqlgen初期化
```shell
go mod init  github.com/MegaBlackLabel/go-docker-sample
go get github.com/99designs/gqlgen
go run github.com/99designs/gqlgen init
```

### gqlgen起動
```shell
air -c .air.conf
```

### まとめ
- GO言語の環境もDockerに入れられた
- コンテナに入らないとvs code色々とエラーメッセージ出るのが辛い
- apiフォルダにマウントしているのでコンテナ内でgitコミットができない
- 1リポジトリで複数のコンテナを詰め込んでいる場合には要検討

以上