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

Set Up a Go Gin Development Environment on Ubuntu 26.04 with Docker: A Complete Record

Set Up a Go Gin Development Environment on Ubuntu 26.04 with Docker: A Complete Record

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
sudo apt install git -y

1.2 Check Docker

Bash
docker --version
docker compose version

Example output:

Plaintext
Docker version 29.5.2, build 79eb04c
Docker Compose version v5.1.4

If not installed, please refer to the Docker official documentation to install it.

✅ Tip: Add the current user to the docker group to avoid entering sudo every time:

Bash
sudo usermod -aG docker $USER

Then log out and log back in.


🌐 2. Confirming the Existing Database Network Environment

I previously ran MySQL 8.0 and Redis 7.2 via docker-compose.yml in ~/docker/services and created a custom network services_dev-network.

2.1 View the network

Bash
docker network ls | grep dev-network

Output:

Plaintext
71f462c6ef91   services_dev-network   bridge    local

2.2 Check database container status

Bash
docker ps --filter "name=mysql80" --filter "name=redis72"

Output:

Plaintext
CONTAINER ID   IMAGE       ...   NAMES
9ec38ca384b6   redis:7.2   ...   redis72
4e93fd2e2c3f   mysql:8.0   ...   mysql80

💡 Key information:

  • MySQL container name: mysql80, internal port 3306, host-mapped port 3307
  • Redis container name: redis72, internal port 6379
  • Network name: services_dev-network (note the prefix, derived from the ~/docker/services directory name)

📁 3. Creating a Code Directory and Cloning the Project

Choose a semantically clear directory to store the project code; here I use ~/code (all lowercase, following Linux conventions).

Bash
mkdir -p ~/code
cd ~/code
git clone https://github.com/shuijingwan/go-gin-learning.git
cd go-gin-learning

Check the project structure:

Bash
ls -la

🐳 4. Writing the Dockerfile (Pre-installing git)

To avoid manually installing git every time we enter the container, we create a custom image.

Create Dockerfile:

Bash
FROM golang:1.26-alpine

# 安装 git(go mod 下载某些依赖时需要)
RUN apk add --no-cache git

# 设置工作目录,与后面的 docker-compose 保持一致
WORKDIR /code

# 保持容器运行(开发模式)
CMD ["tail", "-f", "/dev/null"]

📌 Note:

  • Uses the Alpine version image for a smaller size
  • apk add --no-cache does not keep the cache, reducing the image size
  • With WORKDIR /code, you can later omit working_dir in docker-compose, but keeping it on both sides for clarity is also fine

⚙️ 5. Writing docker-compose.yml (Connecting to the Existing Network)

Key points:

  • Use build: . instead of image: ...
  • Join the external network services_dev-network
  • Pass the environment variables required for database connections
  • Remove the outdated version field

Final docker-compose.yml content:

YAML
services:
  go:
    build: .
    container_name: go-gin-learning
    working_dir: /code
    volumes:
      - ./:/code
    ports:
      - "8080:8080"
    tty: true
    stdin_open: true
    environment:
      - GOPROXY=https://goproxy.io,direct
      - GOSUMDB=sum.golang.google.cn
      - CGO_ENABLED=0
      # 数据库连接配置(使用现有网络中的服务名)
      - DB_HOST=mysql80
      - DB_PORT=3306
      - DB_USER=root
      - DB_PASSWORD=mystrongpwd80
      - DB_NAME=go_gin
      - REDIS_HOST=redis72
      - REDIS_PORT=6379
      - REDIS_PASSWORD=myredispass
    networks:
      - services_dev-network

networks:
  services_dev-network:
    external: true

⚠️ Note:

  • DB_PASSWORD and REDIS_PASSWORD must be exactly the same as the password in ~/docker/services/docker-compose.yml.
  • Inter-container communication uses the internal ports 3306 and 6379, not the host-mapped 3307.

🚀 6. Building and Starting the Development Container

Bash
# 停止并删除旧容器(如果有)
docker compose down

# 构建镜像(--no-cache 确保重新安装 git)
docker compose build --no-cache

# 启动容器(后台运行)
docker compose up -d

Check the running status:

Bash
docker ps --filter "name=go-gin-learning"

🧪 7. Entering the Container and Verifying Connectivity

7.1 Enter the container

Bash
docker exec -it go-gin-learning sh

At this point, the prompt changes to /code #.

7.2 Verify git is installed

Bash
git --version

It should output something like git version 2.52.0.

7.3 Test network connectivity

Bash
# 安装测试工具(仅调试用,正式镜像可不加)
apk add iputils mysql-client redis

# ping 数据库容器
ping -c 2 mysql80
ping -c 2 redis72

7.4 Test MySQL connection (disable SSL)

MySQL 8.0 requires SSL by default, but using --ssl=0 inside the container bypasses certificate verification:

Bash
mariadb -h mysql80 -P 3306 -u root -pmystrongpwd80 -e "SHOW DATABASES;" --ssl=0

Expected output: See Figure 1

Plaintext
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

💡 If using the mysql command, please use --ssl-mode=DISABLED.

7.5 Test Redis connection

Bash
redis-cli -h redis72 -a myredispass PING

Expected output: See Figure 2

Expected output: See Figure 2
Plaintext
PONG

🏃 8. Running the Gin Application

Execute inside the container:

Bash
go run main.go

Seeing the following logs indicates success: See Figure 3

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
curl http://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

OperationCommand
Start the development containercd ~/code/go-gin-learning && docker compose up -d
Stop the containerdocker compose down
Rebuild the imagedocker compose build --no-cache
View logsdocker compose logs -f
Enter the containerdocker 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.

10.2 MySQL connection reports TLS/SSL error

Symptom:

Plaintext
ERROR 2026 (HY000): TLS/SSL error: Certificate verification failure

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:

  1. ✅ Set up a complete Go development environment on Ubuntu 26.04
  2. ✅ Utilized existing MySQL/Redis containers (deployed independently) and a custom network
  3. ✅ Built a Go 1.26 Alpine development image with git
  4. ✅ Wrote docker-compose.yml to mount the code and connect to the external network
  5. ✅ 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

🔗 References

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

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

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

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

关于我  ·  GitHub  ·  邮件联系

评论

一条对“Set Up a Go Gin Development Environment on Ubuntu 26.04 with Docker: A Complete Record”的回复