[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Home > Backend Development > Golang > Live-Reload and Debugging Go Applications Within a Docker Container

Live-Reload and Debugging Go Applications Within a Docker Container

Barbara Streisand
Release: 2025-01-27 02:09:09
Original
711 people have browsed it

Live-Reload and Debugging Go Applications Within a Docker Container

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:

  • Docker: Provides a consistent and isolated development environment.
  • Air: A lightweight Go tool for live-reloading. Alternatives like Nodemon or Inotify-tools are not Go-specific.
  • Delve: A powerful Go debugger integrating with IDEs like VS Code.

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>
Copy after login

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>
Copy after login
  • 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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template