一、环境准备

1.1 系统要求

  • Fedora 36+(推荐最新版)
  • 非 root 用户权限(需配置 SUID 权限)

1.2 安装 Podman

# 安装 Podman
sudo dnf install -y podman

# 验证安装
podman --version
#podman version 5.5.2

#修改 podman service 
 vim /usr/lib/systemd/system/podman.service
#将这个行改成 ExecStart=/usr/bin/podman $LOGGING system service -t 0 然后启动podman.socks 和podman service

# 配置windows 上面的 podman , 安装podman desktop 然后
podman system connection add wsl-podman  'unix:////wsl.localhost/fedoraremix/run/podman/podman.sock' //连接

二、常见问题与解决方案

2.1 Rootless 模式权限问题

执行podman ps 错误现象

WARN[0000] "/" is not a shared mount, this could cause issues or missing mounts with rootless containers 
ERRO[0000] running `/usr/bin/newuidmap 1071 0 1000 1 1 589824 65536`: newuidmap: write to uid_map failed: Operation not permitted 
Error: cannot set up namespace using "/usr/bin/newuidmap": should have setuid or have filecaps setuid: exit status 1

解决方案

# 设置 SUID 权限(关键步骤)
sudo chmod u+s /usr/bin/newuidmap
sudo chmod u+s /usr/bin/newgidmap

# 验证权限
ls -l /usr/bin/newuidmap  # 应显示 "srwxr-xr-x"

# 修复根文件系统挂载模式
sudo mount --make-shared /

# 查看当前挂载属性
findmnt -o PROPAGATION / 
#结果应该变成了shared

2.2 镜像源配置

修改 /etc/containers/registries.conf

#添加如下内容  替代docker.io
[[registry]]
# 仓库前缀(镜像前缀)
prefix = "docker.io"
# 加速器地址,此处配置的 docker 中国区源
#location = "zccif0gh.mirror.aliyuncs.com"
location = "docker.1ms.run"
# 允许通过 http 协议获取镜像
insecure = true

三、Golang简单的服务端

3.1 示例代码

// main.go
package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "Hello from Podman!")
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":5666", nil)
}

3.2 Dockerfile 构建

# 使用多阶段构建优化镜像体积
FROM golang:1.24 AS build
# 构建阶段
WORKDIR /web-server/
COPY . .
ENV CGO_ENABLED=0
ENV GO111MODULE=on
ENV GOPROXY=https://goproxy.cn,direct
RUN GOOS=linux go build -installsuffix cgo -o web-server main.go

# 运行阶段
#此处本来使用的alpine 结果发现那个镜像有问题 缺少glibc导致二进制无法运行 要手动添加安装
#例如 
#FROM alpine
#RUN apk add libc6-compat
FROM busybox 

COPY --from=build /web-server/web-server /web-server/web-server
EXPOSE 5667
WORKDIR /web-server/
ENTRYPOINT ["/web-server/web-server"]

四、镜像构建与运行

4.1 构建镜像

#启用 BuildKit 自动清理中间镜像
podman build --build-arg BUILDKIT_INLINE_CACHE=1  --layers=false -t hello_server:0.0.1  .

#或者运行完之后直接清理 dangling 镜像
docker rmi $(docker images -f "dangling=true" -q)
  

4.2 运行容器

podman run -d -p 15667:5667 --name my-golang-server 46c2136a85ed #此处的46c2136a85ed为实际构建出来的镜像id

五、验证服务

# 测试服务状态
curl http://localhost:15667
#显示正常输出
# 查看容器日志
podman logs my-server