gpt4 book ai didi

带有 golang-onbuild 的 Docker-compose 在代理后面下载失败

转载 作者:IT王子 更新时间:2023-10-29 02:32:07 32 4
gpt4 key购买 nike

在没有代理的机器上,我像这样写了main.go、Dockerfile和docker-compose.yml,docker-compose up成功完成。

package main

import (
"fmt"
"github.com/garyburd/redigo/redis"
"log"
"net/http"
)

func main() {
redi, err := redis.Dial("tcp", "redis:6379")

/// SNIP ///

log.Fatal(http.ListenAndServe(":5000", nil))
}

docker 文件

# Dockerfile
FROM golang:onbuild
EXPOSE 5000

docker-compose.yml

# docker-compose.yml
version: '2'
services:
web:
build: .
ports:
- 5000:5000
depends_on:
- redis
redis:
image: redis

但是,(当然)它们不会在代理后面的机器上运行。我像这样修改了 Dockerfile 和 docker-compose.yml;

docker 文件

# Dockerfile
FROM golang:onbuild
ENV HTTP_PROXY http://proxy.mycompany.com:10080
ENV HTTPS_PROXY http://proxy.mycompany.com:10080
ENV http_proxy http://proxy.mycompany.com:10080
ENV https_proxy http://proxy.mycompany.com:10080
EXPOSE 5000

docker-compose.yml

# docker-compose.yml
version: '2'
services:
web:
build: .
ports:
- 5000:5000
volumes:
- ".:/redis-go-sample"
depends_on:
- redis
environment:
- HTTP_PROXY=${HTTP_PROXY}
- HTTPS_PROXY=${HTTP_PROXY}
- http_proxy=${HTTP_PROXY}
- https_proxy=${HTTP_PROXY}
redis:
image: redis

结果是;

Building web
Step 1/6 : FROM golang:onbuild
# Executing 3 build triggers...
Step 1/1 : COPY . /go/src/app
---> Using cache
Step 1/1 : RUN go-wrapper download
---> Running in 8e311bec859c
+ exec go get -v -d
github.com/garyburd/redigo (download)
# cd .; git clone https://github.com/garyburd/redigo /go/src/github.com/garyburd/redigo
Cloning into '/go/src/github.com/garyburd/redigo'...
fatal: unable to access 'https://github.com/garyburd/redigo/': Could not resolve host: github.com
package github.com/garyburd/redigo/redis: exit status 128
ERROR: Service 'web' failed to build: The command '/bin/sh -c go-wrapper download' returned a non-zero code: 1

如上一条错误信息所述,我猜问题是由于代理环境的 golang-onbuild 或 go-wrapper 处理不当造成的。

我该如何解决这个问题?

最佳答案

环境参数在运行容器时使用,而不是在构建时使用。在您的情况下,构建图像失败了。您需要使用构建参数。可以将构建参数添加到撰写文件中:

version: '2'
services:
web:
build:
context: .
args:
- HTTP_PROXY: http://proxy.mycompany.com:10080
- HTTPS_PROXY: http://proxy.mycompany.com:10080
- http_proxy: http://proxy.mycompany.com:10080
- https_proxy: http://proxy.mycompany.com:10080
ports:
- 5000:5000
volumes:
- ".:/redis-go-sample"
depends_on:
- redis
...

关于带有 golang-onbuild 的 Docker-compose 在代理后面下载失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47460092/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com