0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

Posted at

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

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

version: "3.3"

services:
  go:
    build:
      context: .
      dockerfile: ./go/Dockerfile.golang
    volumes:
      - ./go:/app
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

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

スクリーンショット 2025-01-12 182225.png

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

スクリーンショット 2025-01-12 182249.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

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

参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?