This guide demonstrates setting up live-reloading and debugging for Go applications within a Docker container, mirroring a Node.js development workflow. While online resources for this specific Go setup are scarce, this approach combines the benefits of consistent Docker environments with the efficiency of live-reloading and the power of debugging.
Key Differences: Live-reloading restarts the application on code changes; hot-reloading patches memory without restarts. Debugging, crucial for efficient development, surpasses log-based troubleshooting. Docker ensures consistent application behavior across environments.
Environment: This guide uses Windows 11 with WSL (Windows Subsystem for Linux) but is adaptable to Linux (identical steps) and macOS (similar). WSL is highly recommended for Windows Go development due to its superior speed compared to the native Windows file system.
Technology Stack:
The goal is automated server restarts on code changes, alongside full debugging functionality within the IDE.
Golang Server (Fiber Example):
Create main.go
:
<code class="language-go">package main import "github.com/gofiber/fiber/v2" func main() { app := fiber.New() app.Get("/", func(c *fiber.Ctx) error { str := "Hello, World!" return c.SendString(str) }) app.Listen(":3000") }</code>
Test this with go run .
Docker Setup (docker-compose.yml
):
<code class="language-yaml">api: build: context: ./api dockerfile: Dockerfile ports: - '3000:3000' - '2345:2345' stop_grace_period: 0.1s volumes: - ./api:/app networks: - internal</code>
build.context
: Specifies the Dockerfile location.ports
: Exposes ports 3000 (web server) and 2345 (debugger).volumes
: Mounts the local api
directory to /app
in the container.Dockerfile (api/Dockerfile
):
<code class="language-dockerfile">FROM golang:1.23.2-alpine3.20 WORKDIR /app RUN go install github.com/go-delve/delve/cmd/dlv@latest RUN go install github.com/air-verse/air@latest COPY go.mod go.sum ./ RUN go mod download USER root # For development only EXPOSE 2345 EXPOSE 3000 CMD ["air", "-c", "air.toml"]</code>
Air and Delve Configuration (api/air.toml
):
<code class="language-toml">root = "." tmp_dir = "tmp" [build] full_bin = "dlv debug --build-flags=\"-gcflags='all=-N -l'\" --listen 0.0.0.0:2345 --headless --continue --accept-multiclient --output=dist/debug"</code>
This configures Air to use Delve for building and running the application with debugging enabled.
VS Code Debug Configuration (api/.vscode/launch.json
):
<code class="language-json">{ "version": "0.2.0", "configurations": [ { "name": "Connect to container", "type": "go", "debugAdapter": "dlv-dap", "request": "attach", "mode": "remote", "port": 2345, "host": "localhost", "trace": "verbose", "substitutePath": [{"from": "/home/user/project/api", "to": "/app"}] } ] }</code>
This configures VS Code to connect to the Delve debugger running in the Docker container. Adjust /home/user/project/api
to match your project's path.
This complete setup enables live-reloading and debugging within a Docker container for efficient Go development. Remember to run docker-compose up
after setting up all files.
The above is the detailed content of Live-Reload and Debugging Go Applications Within a Docker Container. For more information, please follow other related articles on the PHP Chinese website!