douqilin4296 2015-07-06 06:05
浏览 30
已采纳

Docker发布不适用于Golang应用程序

I am trying to run a golang web server application (which tries to run a go blog powered by same tools which power golang blog) via docker. It runs fine without docker but fails to give any response if I use docker. I have no clue why this is happening as for a much simpler go web application, the same docker based approach works great. See below for output of various commands I used. Any help would be greatly appreciated.

docker pull maddyonline/gotutorial
docker run -d --publish 8080:8080 --name gotut maddyonline/gotutorial

81bc36e385286326a6d9f74322515a7b9748e493275c3426bcc6848a4589a7e7

docker ps

CONTAINER ID IMAGE COMMAND
CREATED STATUS PORTS NAMES 81bc36e38528 maddyonline/gotutorial "go-wrapper run" 20 seconds ago Up 20 seconds 0.0.0.0:8080->8080/tcp gotut

curl localhost:8080

curl: (52) Empty reply from server

docker exec gotut curl localhost:8080
<!DOCTYPE html>
<html>
<head>

And rest of the file

The docker file I am using is very simple.

FROM golang:onbuild
EXPOSE 8080

I experimented with the same docker file with a much simpler golang application on the same server. And it works.

docker pull maddyonline/gowebapp
docker run -d --publish 8080:8080 --name gowebapp maddyonline/gowebapp
curl localhost:8080/icecream

Hi there, I think I love icecream!

  • 写回答

1条回答 默认 最新

  • doufubu2518 2015-07-08 18:03
    关注

    I found two issues with your current code.

    The first is that the string localhost in your main.go needs to be 0.0.0.0. The problem is that you've told your webserver to only serve requests sent to localhost. Because of the network mangling that Docker does, none of the traffic showing up to your webserver look to the webserver as though they were sent to localhost. By telling your webserver to listen on 0.0.0.0 you're effectively telling it that it doesn't care where the traffic was originally sent and it will accept it all.

    var (
        httpFlag   = flag.String("http", "0.0.0.0:8080", "HTTP listen address") // <--- Here it the change. It's roughly line 25 of [main.go](https://github.com/maddyonline/gotutorial/blob/master/main.go#L25)
        originFlag = flag.String("origin", "", "web socket origin for Go Playground (e.g. localhost)")
        baseFlag   = flag.String("base", "", "base path for articles and resources")
    )
    

    The second issue has to due with your import paths colliding with the predetermined directory structure of the onbuild Dockerfile from which you were extending. I saw that you were trying to hardcode the import path, which is not a good idea, but I think it was your only option due to the weirdness of the go-wrapper that was being forced onto you by the onbuild Dockerfile. Ultimately you should just have the docker build process stick a static Go binary in that container. That will simplify the process of actually starting up the server in the container.

    I didn't mess with your paths in your Go code, I figured you could work that out, but I did grab the upstream onbuild Dockerfile and customize it to what you need. BTW, if you look at the documentation for the onbuild Dockerfile it actually says that it's a bad idea to use that Dockerfile. ;) So, I took their advice and here is my port:

    FROM golang:1.4.2
    
    RUN mkdir -p /go/src/github.com/maddyonline/gotutorial
    WORKDIR      /go/src/github.com/maddyonline/gotutorial
    
    ADD . /go/src/github.com/maddyonline/gotutorial
    RUN go-wrapper download
    RUN go-wrapper install
    
    EXPOSE 8080
    CMD ["go-wrapper", "run"]
    

    Your other sample server app didn't have the same issues because the import paths weren't specified in the same way they are in this project. Basically, it didn't care where it was living because its dependencies were not being hardcoded to a file path. It is using the normal Go packaging conventions.

    Sorry, it probably seems like a lot of change, but I hope it at least helps you move forward on your project. Let me know if anything doesn't make sense.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?