Goの拡張機能をDockerで使用する方法

ローカルにGoをインストールせずにGoの拡張機能を使用する方法を備忘録として記載します。
WSL2を使用しています。




以下のように最低限docker-compose.ymlとDockerfileを作成します。
```yml
version: "3.3"

services:
  go:
    build:
      context: .
      dockerfile: ./go/Dockerfile.golang
    volumes:
      - ./go:/app
```


```Dockerfile
FROM golang:1.23.1-alpine3.19

WORKDIR /app

COPY go/go.mod .
COPY go/go.sum .
COPY go/*air.toml .
COPY ./go .
RUN go mod download
RUN apk update && apk add --no-cache git
RUN go get -u github.com/ramya-rao-a/go-outline 
RUN go install golang.org/x/tools/gopls@latest
RUN go install github.com/air-verse/air@latest

CMD ["air"]

```
VScodeの画面でctrl+shift+pで「Add Dev Conrainer・・・」を選択します。

![スクリーンショット 2025-01-12 182154.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3439167/f950ec20-ef6f-cd2a-afc3-56a52e093068.png)

「docker-compose.ymlから」を選択します。

![スクリーンショット 2025-01-12 182225.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3439167/57b44382-194a-acac-7b22-0b0d5c37fbd3.png)

サービスを先ほど作成した「go」を選択します。

![スクリーンショット 2025-01-12 182249.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3439167/f7d9b00b-f5c1-e3c1-fee7-1a11bf0257fa.png)

そのあとの選択肢は全てチェックせずにOKを押します。
そして、生成されたdevcontainer.jsonの"customizations"を以下のように修正します。

```devcontainer.json
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/docker-existing-docker-compose
{
	"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",
		"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": "go",

	// 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": "/workspaces/${localWorkspaceFolderBasename}",

	// Features to add to the dev container. More info: https://containers.dev/features.
	// "features": {},

	// Use 'forwardPorts' to make a list of ports inside the container available locally.
	// "forwardPorts": [],

	// 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.
	// "postCreateCommand": "cat /etc/os-release",

	// Configure tool-specific properties.
  "customizations": {
    // Configure properties specific to VS Code.
    "vscode": {
      // Add the IDs of extensions you want installed when the container is created.
      "extensions": ["golang.go","mhutchie.git-graph"]
    }
  }

	// Uncomment to connect as an existing user other than the container default. More info: https://aka.ms/dev-containers-non-root.
	// "remoteUser": "devcontainer"
}

```

```docker-compose.yml
version: '3.3'
services:
  # Update this to the name of the service you want to work with in your docker-compose.yml file
  go:
    # Uncomment if you want to override the service's Dockerfile to one in the .devcontainer 
    # folder. Note that the path of the Dockerfile and context is relative to the *primary* 
    # docker-compose.yml file (the first in the devcontainer.json "dockerComposeFile"
    # array). The sample below assumes your primary file is in the root of your project.
    #
    # build:
    #   context: .
    #   dockerfile: .devcontainer/Dockerfile

    volumes:
      # Update this to wherever you want VS Code to mount the folder of your project
      - ..:/workspaces:cached

    # Uncomment the next four lines if you will use a ptrace-based debugger like C++, Go, and Rust.
    # cap_add:
    #   - SYS_PTRACE
    # security_opt:
    #   - seccomp:unconfined

    # Overrides default command so things don't shut down after the process ends.
    command: sleep infinity
 

```

その後、ctrl+shift+pで「Rebuild and Reopen・・・」を選択します。

![スクリーンショット 2025-01-12 182412.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3439167/ec2f2add-3f51-8e29-6960-8cee4724d9a4.png)

以上で、ローカルにGoをインストールしなくてもGoの拡張機能を使用できるようになります。

# 参考
https://blog.final-techblog.com/windows11vscodedev-containers%E3%81%A7go%E8%A8%80%E8%AA%9E%E9%96%8B%E7%99%BA%E7%92%B0%E5%A2%83%E6%A7%8B%E7%AF%89