在没有代理的机器上,我写了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))
}
Dockerfile
# Dockerfile
FROM golang:onbuild
EXPOSE 5000
搬运工-compose.yml
# docker-compose.yml
version: '2'
services:
web:
build: .
ports:
- 5000:5000
depends_on:
- redis
redis:
image: redis
然而,(当然)他们不会在代理后面的机器上运行。 我修改了Dockerfile和docker-compose.yml之类的;
Dockerfile
# 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
搬运工-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错误处理代理环境引起的。
我如何解决这个问题?
答案 0 :(得分:3)
在运行容器时使用环境参数,而不是在构建时使用。在您的情况下,构建图像失败。 您需要使用构建参数。可以将构建参数添加到compose文件中:
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
...