Seamlessly migrating from Windows WSL2 to bare-metal Ubuntu, building a Go development environment based on existing MySQL/Redis containers, and connecting it to a custom network.
📌 Introduction
I was previously learning the go-gin-learning project on Windows 10 + WSL2, and now I have switched to a brand-new Ubuntu 26.04 system. I want to continue using Docker to isolate the development environment while reusing the already deployed MySQL 8.0 and Redis 7.2 containers (located in the custom network services_dev-network).
This article documents the entire process of configuring it from scratch, including:
System tools check and installation
Docker environment verification
Project code cloning
Writing a Dockerfile to pre-install git
Writing docker-compose.yml to connect to the existing network
Running the Gin app inside the container and testing connectivity
Common issues and troubleshooting tips
🧰 1. Preparation: Checking Existing System Tools
Developing the habit of “check first, install later” in Ubuntu can avoid redundant operations.
1.1 Check Git
Bash
git--version
Example output (already installed):
Plaintext
git version 2.53.0
If not installed, run:
Bash
sudoaptinstallgit-y
1.2 Check Docker
Bash
docker--versiondockercomposeversion
Example output:
Plaintext
Docker version 29.5.2, build 79eb04cDocker Compose version v5.1.4
💡 If using the mysql command, please use --ssl-mode=DISABLED.
7.5 Test Redis connection
Bash
redis-cli-hredis72-amyredispassPING
Expected output: See Figure 2
Plaintext
PONG
🏃 8. Running the Gin Application
Execute inside the container:
Bash
gorunmain.go
Seeing the following logs indicates success: See Figure 3
Plaintext
go: downloading github.com/go-playground/locales v0.14.1[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production. - using env: export GIN_MODE=release - using code: gin.SetMode(gin.ReleaseMode)[GIN-debug] GET /albums --> main.getAlbums (3 handlers)[GIN-debug] GET /albums/:id --> main.getAlbumByID (3 handlers)[GIN-debug] POST /albums --> main.postAlbums (3 handlers)[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.Please check https://github.com/gin-gonic/gin/blob/master/docs/doc.md#dont-trust-all-proxies for details.[GIN-debug] Listening and serving HTTP on 0.0.0.0:8080[GIN] 2026/06/03 - 11:19:14 | 404 | 1.219µs | 172.18.0.1 | GET "/"[GIN] 2026/06/03 - 11:19:14 | 404 | 778ns | 172.18.0.1 | GET "/favicon.ico"[GIN] 2026/06/03 - 11:19:45 | 200 | 147.412µs | 172.18.0.1 | GET "/albums"
Test the API from the host
Open another terminal and execute:
Bash
curlhttp://localhost:8080/albums
Or access http://localhost:8080/albums directly in a browser.
📸 The browser displays JSON data, as shown in Figure 4
🧹 9. Common Management Commands
Operation
Command
Start the development container
cd ~/code/go-gin-learning && docker compose up -d
Stop the container
docker compose down
Rebuild the image
docker compose build --no-cache
View logs
docker compose logs -f
Enter the container
docker exec -it go-gin-learning sh
Restart MySQL/Redis (if needed)
cd ~/docker/services && docker compose restart
⚠️ 10. Troubleshooting Guide
10.1 Missing git inside the container causes go run to fail
Symptom:
Plaintext
unable to resolve git version: exec: "git": executable file not found in $PATH
Solution: Use a custom Dockerfile to install git, see Chapter 4.
Solution: Add --ssl=0 (mariadb) or --ssl-mode=DISABLED (mysql) to the connection command.
10.3 Container cannot access MySQL/Redis
Cause: Not joined to the correct network, or the network name is misspelled.
Solution:
Confirm that docker network ls contains services_dev-network
Check that the networks name in docker-compose.yml is exactly the same (including underscores)
Test with ping mysql80 inside the container
10.4 Host cannot access port 8080
Cause: Gin is listening on 127.0.0.1:8080 instead of 0.0.0.0:8080, causing port mapping to fail.
Solution: Set router.Run(":8080") or router.Run("0.0.0.0:8080") in the code.
📝 11. Summary
Through this practice, I achieved:
✅ Set up a complete Go development environment on Ubuntu 26.04
✅ Utilized existing MySQL/Redis containers (deployed independently) and a custom network
✅ Built a Go 1.26 Alpine development image with git
✅ Wrote docker-compose.yml to mount the code and connect to the external network
✅ Successfully ran the Gin app inside the container and verified database connectivity
This pattern of “separating database and code into different containers” is well-suited for multiple projects sharing infrastructure, saving resources while maintaining environment consistency.
Next steps:
Write business logic in main.go to connect to MySQL/Redis
Use any code editor (like VS Code, Sublime, etc.) to directly edit project files, with real-time synchronization inside the container upon saving
Add docker-compose.yml to version control to share environment configurations with teammates
评论
一条对“Set Up a Go Gin Development Environment on Ubuntu 26.04 with Docker: A Complete Record”的回复
[…] […]