Docker + Golang HTTPS问题。OpenSSL SSL_connect: SSL_ERROR_SYSCALL

huangapple go评论121阅读模式
英文:

Docker + Golang HTTPS issue. OpenSSL SSL_connect: SSL_ERROR_SYSCALL

问题

我正在尝试从Docker容器中连接到Go创建的HTTPS会话,当我在本地运行时,它可以正常工作,但是一旦我尝试在容器中运行它,我无法访问URL。

以下是我的代码:

  1. func main() {
  2. // 我使用的是Go-Chi V5
  3. r := chi.NewRouter()
  4. // TLS连接。灵感来自于https://blog.cloudflare.com/exposing-go-on-the-internet
  5. tlsConfig := &tls.Config{
  6. PreferServerCipherSuites: true,
  7. CurvePreferences: []tls.CurveID{
  8. tls.CurveP256,
  9. tls.X25519,
  10. },
  11. MinVersion: tls.VersionTLS12,
  12. CipherSuites: []uint16{
  13. tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
  14. tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
  15. tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
  16. tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
  17. tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  18. tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  19. },
  20. }
  21. srv := &http.Server{
  22. Addr: "localhost:9090",
  23. ReadTimeout: 5 * time.Second,
  24. WriteTimeout: 20 * time.Second,
  25. IdleTimeout: 200 * time.Second,
  26. TLSConfig: tlsConfig,
  27. Handler: r,
  28. }
  29. r.Get("/", func(w http.ResponseWriter, r *http.Request) {
  30. w.Write([]byte("hello world"))
  31. })
  32. err := srv.ListenAndServeTLS("./.dev/localhost.crt", "./.dev/localhost.key")
  33. if err != nil {
  34. log.Fatalf("server failed to start: %v", err)
  35. }
  36. }

我的Dockerfile:

  1. FROM golang:1.17-buster AS build
  2. WORKDIR /app
  3. COPY go.mod .
  4. COPY go.sum .
  5. RUN go mod download
  6. COPY ./.dev/localhost.key .
  7. COPY ./.dev/localhost.crt .
  8. COPY . .
  9. RUN go build -o /dev-admin ./cmd/admin
  10. ##
  11. ## 部署
  12. ##
  13. FROM gcr.io/distroless/base-debian10
  14. WORKDIR /
  15. COPY --from=build /dev-admin /dev-admin
  16. COPY --from=build ./app/localhost.key ./.dev/localhost.key
  17. COPY --from=build ./app/localhost.crt ./.dev/localhost.crt
  18. EXPOSE 9090
  19. EXPOSE 443
  20. ENTRYPOINT ["/dev-admin"]

docker-compose.yml:

  1. version: '3.9'
  2. services:
  3. web:
  4. image: api_test
  5. build: .
  6. ports:
  7. - '9090:9090'
  8. volumes:
  9. - .:/app

当我尝试使用普通的HTTP版本时,容器可以正常工作,所以问题只出现在HTTPS上,我无法找出具体原因。

英文:

I'm trying to connect to an HTTPS session created in Go from a Docker container, it works fine when I run it on my local, but as soon as I try to run it in the container I can't access to the URL.

  1. func main() {
  2. // I'm using Go-Chi V5
  3. r := chi.NewRouter()
  4. // TLS connection. Inspired from https://blog.cloudflare.com/exposing-go-on-the-internet
  5. tlsConfig := &tls.Config{
  6. PreferServerCipherSuites: true,
  7. CurvePreferences: []tls.CurveID{
  8. tls.CurveP256,
  9. tls.X25519,
  10. },
  11. MinVersion: tls.VersionTLS12,
  12. CipherSuites: []uint16{
  13. tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
  14. tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
  15. tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
  16. tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
  17. tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  18. tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  19. },
  20. }
  21. srv := &http.Server{
  22. Addr: "localhost:9090",
  23. ReadTimeout: 5 * time.Second,
  24. WriteTimeout: 20 * time.Second,
  25. IdleTimeout: 200 * time.Second,
  26. TLSConfig: tlsConfig,
  27. Handler: r,
  28. }
  29. r.Get("/", func(w http.ResponseWriter, r *http.Request) {
  30. w.Write([]byte("hello world"))
  31. })
  32. err := srv.ListenAndServeTLS("./.dev/localhost.crt", "./.dev/localhost.key")
  33. if err != nil {
  34. log.Fatalf("server failed to start: %v", err)
  35. }
  36. }

My Dockerfile:

  1. FROM golang:1.17-buster AS build
  2. WORKDIR /app
  3. COPY go.mod .
  4. COPY go.sum .
  5. RUN go mod download
  6. COPY ./.dev/localhost.key .
  7. COPY ./.dev/localhost.crt .
  8. COPY . .
  9. RUN go build -o /dev-admin ./cmd/admin
  10. ##
  11. ## Deploy
  12. ##
  13. FROM gcr.io/distroless/base-debian10
  14. WORKDIR /
  15. COPY --from=build /dev-admin /dev-admin
  16. COPY --from=build ./app/localhost.key ./.dev/localhost.key
  17. COPY --from=build ./app/localhost.crt ./.dev/localhost.crt
  18. EXPOSE 9090
  19. EXPOSE 443
  20. ENTRYPOINT ["/dev-admin"]

docker-compose.yml:

  1. version: '3.9'
  2. services:
  3. web:
  4. image: api_test
  5. build: .
  6. ports:
  7. - '9090:9090'
  8. volumes:
  9. - .:/app

$ curl https://localhost:9090
curl: (35) OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to localhost:9090

The container works fine when I try the normal HTTP version, so there is a blockage on the HTTPS only and I can't figure out what it is.

答案1

得分: 3

你正在指定在Server定义中要监听的地址:

  1. srv := &http.Server{
  2. Addr: "localhost:9090",
  3. ...

将其更改为:

  1. srv := &http.Server{
  2. Addr: ":9090",
  3. ...

需要进行此更改的原因是地址“指定服务器要侦听的TCP地址”。当你指定localhost:9090时,你绑定到容器内的回环接口,在大多数情况下,这是无法从外部访问的(有关更多信息,请参见此问题)。

英文:

You are specifying the address to listen on in your Server definition:

  1. srv := &http.Server{
  2. Addr: "localhost:9090",
  3. ...

change this to:

  1. srv := &http.Server{
  2. Addr: ":9090",
  3. ...

The reason that this change is needed is that the address "specifies the TCP address for the server to listen on". When you specify localhost:9090 you are binding to the loopback interface within the container and, in most cases, this is not accessible externally (see this question for more info).

huangapple
  • 本文由 发表于 2021年12月29日 02:09:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/70510882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定