Skip to content
Ricky
English

Search

Searching… No matching pages. Matching pages are listed below. Search is unavailable. Please try again.

VSCode Integration with DevSpace for Debugging!

  • Ricky Lin
  • 5 minutes
On this page

Install required tools πŸ“¦

# Check Go version
go version

# Install the Delve debugger
go install github.com/go-delve/delve/cmd/dlv@latest

# Check DevSpace version
devspace version

# Install VSCode Go extensions
code --install-extension golang.go
code --install-extension devspace.devspace

Project structure design πŸ“

.
β”œβ”€β”€ cmd
β”‚   └── main.go
β”œβ”€β”€ devspace.yaml
β”œβ”€β”€ Dockerfile.dev
β”œβ”€β”€ Dockerfile.prod
β”œβ”€β”€ go.mod
β”œβ”€β”€ go.sum
β”œβ”€β”€ internal
β”‚   β”œβ”€β”€ handler.go
β”‚   β”œβ”€β”€ model.go
β”‚   └── service.go
β”œβ”€β”€ k8s
β”‚   └── dev
β”‚       β”œβ”€β”€ deployment.yaml
β”‚       └── service.yaml
β”œβ”€β”€ main
└── start-dev.sh

Prepare the Go application πŸš€

First, build a simple but practical Go application with a few API endpoints so you can test breakpoints.

To debug Go, besides the API port you also need SSH port 22 and the Delve debugger port 40000.

Dockerfile.dev - Development container config

This file builds a container environment dedicated to development and debugging.

FROM golang:1.24

# πŸ› οΈ Install development tools
# - git: version control
# - curl/wget: network tools for API tests
# - vim/nano: edit files inside the container
# - openssh-server: SSH access
# - build-essential: build toolchain
RUN apt-get update && apt-get install -y \
    git \
    curl \
    wget \
    vim \
    nano \
    htop \
    openssh-server \
    sudo \
    build-essential \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# πŸ”§ Install the latest Delve debugger
# Delve is the official Go debugger and supports remote debugging
RUN go install github.com/go-delve/delve/cmd/dlv@v1.23.1

# πŸ‘€ Create a dev user (avoid running as root)
RUN useradd -m -s /bin/bash developer && \
    echo "developer:devspace123" | chpasswd && \
    usermod -aG sudo developer

# πŸ”‘ Configure SSH service (remote access)
RUN mkdir -p /var/run/sshd && \
    echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config && \
    echo 'PasswordAuthentication yes' >> /etc/ssh/sshd_config && \
    sed -i 's/#Port 22/Port 22/' /etc/ssh/sshd_config

# πŸ“ Set working directory
WORKDIR /app

# πŸ“¦ Copy dependency files and download
COPY go.mod go.sum ./
RUN go mod download

# πŸ“‹ Copy source code
COPY . .

# πŸ”¨ Build a debug binary
# -gcflags="all=-N -l" disables optimizations and keeps debug info
RUN go build -gcflags="all=-N -l" -o main cmd/main.go

# 🌐 Expose ports
# 8080: app service
# 40000: Delve debugger
# 22: SSH service
EXPOSE 8080 40000 22

# πŸš€ Copy startup script
COPY start-dev.sh /app/start-dev.sh
RUN chmod +x /app/start-dev.sh

# 🎯 Default command
CMD ["/app/start-dev.sh"]

πŸš€ start-dev.sh - Development startup script

This script orchestrates the startup flow of the development environment.

#!/bin/bash
set -e  # Exit immediately on error

echo "πŸš€ Starting development environment..."

# πŸ”‘ Start SSH service (remote access)
echo "πŸ”‘ Starting SSH service..."
service ssh start

# πŸ” Check Delve version (make sure the debugger is available)
echo "πŸ› Delve version:"
dlv version

# πŸ”¨ Rebuild the application
echo "πŸ”¨ Building application..."
go build -gcflags="all=-N -l" -o main cmd/main.go

# πŸ› Start the Delve debugger
# --listen=:40000: listen on port 40000
# --headless=true: no GUI
# --api-version=2: use API v2 (DAP support)
# --accept-multiclient: allow multiple clients
# --continue: continue running after start
# --log: enable logs
echo "πŸ› Starting Delve debugger on port 40000..."
dlv --listen=:40000 --headless=true --api-version=2 --accept-multiclient --continue --log exec ./main &

# ⏳ Wait for the debugger to start
sleep 5
echo "βœ… Delve debugger started successfully!"

# πŸ“Š Show service info
echo "🌐 Application available on port 8080"
echo "πŸ”‘ SSH available on port 22 (user: developer, pass: devspace123)"
echo "πŸ› Debug port available on 40000"

# πŸ” Check port status
echo "πŸ“Š Checking ports..."
netstat -tlnp | grep -E "(8080|40000)" || echo "Ports not ready yet"

echo "πŸ“± Development environment ready!"

# πŸ”„ Keep the container running
tail -f /dev/null

βš™οΈ devspace.yaml - DevSpace configuration

This file defines the full configuration for the development environment.

version: v2beta1
name: go-debug-demo

# πŸ–ΌοΈ Image config: define how to build the container image
images:
  app:
    image: go-debug-demo
    dockerfile: ./Dockerfile.dev # Use the dev Dockerfile
    tags:
      - dev-${DEVSPACE_RANDOM} # Dynamic tag to avoid cache issues

# πŸš€ Deployment config: define how to deploy to Kubernetes
deployments:
  app:
    kubectl:
      manifests:
        - k8s/dev/deployment.yaml
        - k8s/dev/service.yaml

# πŸ”„ Dev config: define dev-mode behavior
dev:
  app:
    # 🎯 Select the Pod to attach to
    labelSelector:
      app: go-debug-demo

    # πŸ“ File sync: sync local changes into the container
    sync:
      - path: ./
        container: /app
        excludePaths:
          - .git/
          - .devspace/
          - Dockerfile*

    # 🌐 Port forwarding: map container ports to local machine
    ports:
      - port: "8080:8080" # App service
      - port: "40000:40000" # Delve debugger
      - port: "2222:22" # SSH service

    # πŸ–₯️ Terminal: open an interactive terminal
    terminal:
      command: ./start-dev.sh

# 🎯 Variables
vars:
  - name: DEVSPACE_RANDOM
    source: none

πŸŽ›οΈ VSCode launch.json - Debugger config

This file tells VSCode how to connect to the remote debugger.

{
	"version": "0.2.0",
	"configurations": [
		{
			"name": "πŸ› DevSpace Remote Debug",
			"type": "go", // Use Go debugger
			"request": "attach", // Attach to an existing process (instead of launching a new one)
			"mode": "remote", // Remote debugging mode
			"remotePath": "/app", // Code path in the container
			"port": 40000, // Delve debugger port
			"host": "127.0.0.1", // Connect to local (port forwarding)
			"apiVersion": 2, // Use Delve API v2

			// πŸ“ Path mapping: local path <-> container path
			"substitutePath": [
				{
					"from": "${workspaceFolder}", // VSCode workspace path
					"to": "/app" // Container path
				}
			],

			// πŸ” Debugger logging
			"showLog": true,
			"logOutput": "debugger",

			// βš™οΈ Delve load config
			"dlvLoadConfig": {
				"followPointers": true,
				"maxVariableRecurse": 1,
				"maxStringLen": 64,
				"maxArrayValues": 64,
				"maxStructFields": -1
			}
		}
	]
}

Start and test πŸ§ͺ

# 1️⃣ Start the development environment
devspace dev

# 2️⃣ Set breakpoints in VSCode
# - Open cmd/main.go
# - Click the first line of healthHandler to set a breakpoint

# 3️⃣ Start the debugger
# - Press F5 or click "πŸ› DevSpace Remote Debug" in the debug panel

# 4️⃣ Trigger the breakpoint
curl http://localhost:8080/health