开发 Golang 项目的 Docker 化案例

开发 Golang 项目的 Docker 化案例

在这个案例中,我们将展示如何使用 Docker 容器化一个简单的 Golang Web 应用。我们将创建一个基于 Go 的 Hello World 应用,并使用 Docker 和 Docker Compose 管理容器化环境。

1. 创建 Golang Web 应用

首先,创建一个简单的 Golang Web 应用,用于展示一个简单的 Hello World 页面。

main.go:

go 复制代码
package main

import (
    "fmt"
    "net/http"
)

func helloHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, Dockerized Go App!")
}

func main() {
    http.HandleFunc("/", helloHandler)
    fmt.Println("Server is running on port 8080...")
    http.ListenAndServe(":8080", nil)
}
2. 编写 Dockerfile

创建 Dockerfile,用于构建 Docker 镜像并运行我们的 Golang 应用。

Dockerfile:

dockerfile 复制代码
# 使用官方 Golang 运行时镜像
FROM golang:1.18-alpine

# 设置工作目录
WORKDIR /app

# 复制当前目录的内容到容器的工作目录
COPY . .

# 构建 Go 应用
RUN go build -o main .

# 暴露应用运行的端口
EXPOSE 8080

# 运行 Go 应用
CMD ["./main"]
3. 编写 Docker Compose 配置文件

使用 Docker Compose 管理容器服务,包括我们的 Golang 应用。

docker-compose.yml:

yaml 复制代码
version: '3'
services:
  web:
    build: .
    ports:
      - "8080:8080"
4. 构建和运行 Docker 容器

现在,我们可以构建和运行我们的 Docker 容器。

在项目根目录下执行以下命令:

bash 复制代码
# 构建 Docker 镜像
docker-compose build

# 启动服务
docker-compose up
5. 访问应用程序

Golang 应用将在 http://localhost:8080 上运行,通过浏览器或 curl 访问可以看到 "Hello, Dockerized Go App!" 的输出。

通过这个案例,我们学会了如何使用 Docker 和 Docker Compose 来容器化一个简单的 Golang Web 应用。

扩展部分:添加 Nginx 反向代理

为了展示如何将多个容器组合在一起工作,我们可以添加一个 Nginx 容器作为反向代理服务器。

1. 编写 Nginx 配置文件

创建一个简单的 Nginx 配置文件,将请求代理到 Golang 应用。

nginx.conf:

nginx 复制代码
server {
    listen 80;
    server_name localhost;

    location / {
        proxy_pass http://web:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
2. 更新 Docker Compose 配置文件

使用 Docker Compose 管理容器服务,包括我们的 Golang 应用和 Nginx 服务。

docker-compose.yml:

yaml 复制代码
version: '3'
services:
  web:
    build: .
    ports:
      - "8080:8080"
  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
      - web
3. 构建和运行 Docker 容器

现在,我们可以构建和运行我们的 Docker 容器。

在项目根目录下执行以下命令:

bash 复制代码
# 构建 Docker 镜像
docker-compose build

# 启动服务
docker-compose up
4. 访问应用程序

Golang 应用将在 http://localhost:80 上通过 Nginx 反向代理运行,通过浏览器或 curl 访问可以看到 "Hello, Dockerized Go App!" 的输出。

通过这个扩展案例,我们学会了如何使用 Docker 和 Docker Compose 来容器化一个简单的 Golang Web 应用,并通过 Nginx 实现反向代理,希望这种组合可以帮助读者理解多容器应用场景的基本概念和工作原理。

相关推荐
侃侃_天下2 天前
最终的信号类
开发语言·c++·算法
muyun28002 天前
Docker 下部署 Elasticsearch 8 并集成 Kibana 和 IK 分词器
elasticsearch·docker·容器
echoarts2 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix2 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题2 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说2 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔2 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
我是菜鸟0713号2 天前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_2 天前
QT(4)
开发语言·汇编·c++·qt·算法
Brookty2 天前
【JavaEE】线程安全-内存可见性、指令全排序
java·开发语言·后端·java-ee·线程安全·内存可见性·指令重排序