最近docker.io国内无法访问,拉取不到最新的镜像,记录使用新的代理获取构建过程

使用镜像加速dockerpull.com拉取镜像

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
docker pull dockerpull.com/alpine:latest
latest: Pulling from alpine
c6a83fedfae6: Pull complete
Digest: sha256:0a4eaa0eecf5f8c050e5bba433f58c052be7587ee8af3e8b3910ef9ab5fbe9f5
Status: Downloaded newer image for dockerpull.com/alpine:latest
dockerpull.com/alpine:latest

➜  / docker pull dockerpull.com/golang:latest
latest: Pulling from golang
903681d87777: Pull complete
3cbbe86a28c2: Pull complete
6ed93aa58a52: Pull complete
aeaef56be71e: Pull complete
c4b14fb4020d: Pull complete
23d1f4306f6d: Pull complete
4f4fb700ef54: Pull complete
Digest: sha256:613a108a4a4b1dfb6923305db791a19d088f77632317cfc3446825c54fb862cd
Status: Downloaded newer image for dockerpull.com/golang:latest
dockerpull.com/golang:latest

查看拉取镜像的版本

1
2
3
4
➜  / docker run --rm -it dockerpull.com/golang:latest go version
go version go1.23.0 linux/amd64
➜  / docker run --rm -it dockerpull.com/alpine:latest cat /etc/alpine-release
3.20.2

修改镜像tag为版本号,方便后续使用

1
2
3
docker tag dockerpull.com/golang:latest golang:1.23.0

docker tag dockerpull.com/alpine:latest alpine:3.20.2

编写golang app Dockerfile文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
FROM golang:1.23.0 AS builder

MAINTAINER piao

LABEL stage=gobuilder

ENV CGO_ENABLED 1
ENV GOPROXY https://goproxy.cn,direct

# RUN apk update --no-cache && apk add --no-cache tzdata

WORKDIR /build

ADD go.mod .
ADD go.sum .
RUN go mod download

COPY . .

RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=1 \
    go build -mod=mod -ldflags="-w -s" \
    -o mart-server ./cmd/mart


FROM alpine:3.20.2

#RUN apk update --no-cache && apk add --no-cache ca-certificates
#COPY --from=builder /usr/share/zoneinfo/Asia/Shanghai /usr/share/zoneinfo/Asia/Shanghai
#ENV TZ Asia/Shanghai

# set timezone
RUN echo "https://mirrors.aliyun.com/alpine/v3.10/main/" > /etc/apk/repositories \
    && echo "https://mirrors.aliyun.com/alpine/v3.10/community/" >> /etc/apk/repositories \
    && apk add --no-cache tzdata \
    && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime  \
    && echo Asia/Shanghai > /etc/timezone \
    && apk del tzdata

WORKDIR /app

COPY --from=builder /build/mart-server .

COPY --from=builder /build/cfg.toml .

RUN chmod +x mart-server

RUN addgroup -S mart && adduser -S -G mart mart

ENTRYPOINT ["docker-entrypoint.sh"]

EXPOSE 80

VOLUME ["/app/cfg.toml","/app/log"]

CMD ["mart-server", "--conf", "cfg.toml"]

apk指令如果因为网络原因访问失败时,可以注释掉。

如果想编辑二进制文件本地或单独使用,可以直接修改Dockerfile直接编译到当前目录

或者使用docker cp命令从容器中拷贝文件到宿主机上使用。

或者直接在Docker容器中编译:

1
2
3
4
5
docker run --rm -v .:/root -v ${GOPATH}/go:/go -e GOPATH=/ -e GOROOT=/go \
    -w `pwd` golang:1.23.0 \  
    GOOS=linux GOARCH=amd64 CGO_ENABLED=1 \
    go build -mod=mod -ldflags="-w -s" \
    -o mart-server ./cmd/mart

或者选择执行脚本

1
docker run --rm -v .:/mart -w /mart golang:1.23.0 bash build_linux.sh

参考