没有不值得去解决的问题,也没有不值得去学习的技术!

From Zero to One: Set Up a Go and Gin Development Environment in Trae CN and Docker

Container action menu in the Docker extension panel

Preface

Recently, on Ubuntu 26.04, I tried using the Trae CN editor along with Docker containers to develop a Go + Gin project (go-gin-learning). I ran into a few pitfalls along the way, but I managed to get a fairly smooth workflow up and running. This article is a complete record of that process, which I hope will help others facing similar issues.

If you have read my previous blog post (such as “Setting Up a Basic Environment with Docker Compose + Go 1.26.4 on Ubuntu 26.04”), you can consider this one the “next chapter”—stringing everything together from code commits to running in a container.


1. Preparations on the Host Machine

1.1 Install Git and Configure User Information

First, ensure Git is installed on the host machine, then set your global username and email (this information will be included in every commit):

Bash
git config --global user.name "shuijingwan"
git config --global user.email "shuijingwanwq@163.com"

Verify the configuration:

Bash
git config --global user.name
git config --global user.email

1.2 Configure GitHub Authentication (Using a Token)

Since 2021, GitHub no longer supports password authentication, so you need to generate a Personal Access Token.

  • Log in to GitHub → Settings → Developer settings → Personal access tokens → Tokens (classic)
  • Click Generate new token (classic)
  • Check the repo permission, and copy the token once it is generated (it is only displayed once)
  • Run the following command in the host machine’s terminal:
Bash
git push origin main

When prompted for your username, enter your GitHub username, and paste the token you just copied for the password. Once successful, Git will remember the authentication credentials (if a credential helper is configured).

1.3 Install Go (Host Machine)

To ensure that code completion and syntax jumping work properly in Trae CN, you need to install Go on the host machine. I chose to install it via APT because it is simple and hassle-free:

Bash
sudo apt update
sudo apt install golang-go -y
go version

Output: go version go1.26.0 linux/amd64

Actually, the system “Software” center also offers a Snap version of Go (see screenshot), but APT is more lightweight and compatible with the Go version inside the container.

Available versions of Go in the Software Center
Available versions of Go in the Software Center

1.4 Install the Go Extension in Trae CN

Open the Trae CN extension store, search for Go, find the official extension (published by golang), and install it.

Go extension in the extension store
Go extension in the extension store

Once installed, press Ctrl+Shift+P, enter Go: Install/Update Tools, check the common tools such as gopls, dlv, and staticcheck from the pop-up list, and click OK.

Select Go tools to install
Select Go tools to install

Wait for the installation to complete, then reload the window.

At this point, the code editing environment on the host machine is ready.

Typing fmt. in main.go should trigger IntelliSense.
Typing fmt. in main.go should trigger IntelliSense.

2. Development Environment Inside the Container

2.1 Let the Container Reuse the Host’s Git Configuration

We want to use the host machine’s username and email when executing git commit inside the container. Modify docker-compose.yml to mount ~/.gitconfig:

Plaintext
volumes:
  - ./:/code
  - ~/.gitconfig:/root/.gitconfig:ro

After restarting the container, enter it to verify:

Bash
docker exec -it go-gin-learning sh
git config --global user.name   # 应显示 shuijingwan
git config --global user.email  # 应显示 shuijingwanwq@163.com

2.2 Fix Slow Go Module Downloads

Initially, running go mod tidy inside the container would always fail or time out because the default proxy goproxy.io is unstable, and the cache gets cleared every time the container is rebuilt.

There are two solutions:

  • Set a more stable proxy: go env -w GOPROXY=https://goproxy.cn,direct
  • Persist the module cache: Mount a host machine directory to the container’s /go/pkg/mod

I opted for the latter by adding the following to docker-compose.yml:

YAML
volumes:
  - ~/go/pkg-mod-go-gin-learning:/go/pkg/mod

This way, the dependencies inside the container are stored in a dedicated directory on the host machine, so they won’t be lost even if the container is deleted. When running go mod tidy again, all go: downloading succeeded.

2.3 Run the Gin Service

Execute the following inside the container:

Bash
go run main.go

Seeing the following output means the service started successfully:

Plaintext
[GIN-debug] Listening and serving HTTP on 0.0.0.0:8080

Access http://localhost:8080/albums in a browser; it returns JSON data with a 200 status code.

Normal request response in the browser
Normal request response in the browser

At this point, in the Trae CN Docker extension panel, you can right-click the container and select Attach Shell to easily open a container terminal.

Container action menu in the Docker extension panel
Container action menu in the Docker extension panel

3. Summary and Standardization Outlook

Current Workflow Summary

  1. Host machine: Handles Git operations, code editing, and the Go language server (gopls).
  2. Container: Responsible for running go run and go test, with dependency modules persisted via the mounted cache directory.
  3. Trae CN: Provides intelligent completion through the Go extension and attaches to the container terminal via the Docker extension, achieving a workflow of “editing on the host, running in the container.”

Although Dev Containers are temporarily unavailable in Trae CN, through the combination of “host Go + container execution + cache persistence,” we still achieved an efficient and reliable development environment.
(By the way, I did not delete .devcontainer/devcontainer.json because other team members using VS Code might still need it.)

Points for Future Standardization

  • Use a Makefile to wrap common commands: Such as make run, make test, and make tidy to reduce manual input.
  • Integrate Air for hot reloading: Install air inside the container to automatically restart the service after saving a file, boosting development efficiency.
  • Script the environment configuration: Write the volumes settings, proxy configuration, and dependency installation for docker-compose.yml into setup.sh to help new members get onboard quickly.
  • Consider using Remote – SSH: If the entire team develops on the same server in the future, we could switch to SSH remote development to completely unify the environment.

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系