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 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件