duanan5940 2015-06-29 21:05
浏览 119
已采纳

在Docker容器中启动golang Web服务器

I am in the process of creating a service that, amongst other things, allows users start/stop a Golang web server at will. To get a Nodejs server running under similar circumstances I simply issue a nodejs /path/to/index.js &disown from within a batch file that I run when the container starts up. The essential bits

ADD gorun.sh /usr/local/bin/gorun.sh
RUN chmod +x /usr/local/bin/gorun.sh

...
ENTRYPOINT ["/bin/bash"]
CMD ["/usr/local/bin/gorun.sh"]

This works perfectly, every time. In gorun.sh I have the nodejs /path/to/index.js & disown line.

Now that I am trying to get a Golang web server running under similar conditions I thought I would try the same approach. I modified the gorun.sh script to read

#! /bin/bash
go run /path/to/index.go & disown;
ps aux | grep go

Now here is the odd thing. I have tried to get the Go server started in two ways

  • Purely as an experiment I started the Docker container with a terminal attached at called gorun from the terminal. The Golang server fired up right away and everything was hunkry-dory.
  • To better emulate the way things work in the real world - I placed a file, startgo.txt at a pre-determined location which then gets picked up by a CRON job that calls gorun. This time round, no joy - the server does not start up.

Why not just call go run /path/to/index.go & disown from the Docker container startup script? Tried that too -same result: the server does not start up.

It is not clear to me what is happening here. Why does the Nodejs server fire up whilst the golang server refuses point blank? Why does it fire up if I run the same script from within a terminal? I'd much appreciate any help with these issues.

  • 写回答

1条回答 默认 最新

  • dpbz14739 2015-06-30 08:30
    关注

    This issue drove me round the bend with looming deadlines and it took me a whole 24h to figure out what was happening. The problem is a rather subtle one. So here is an explanation for the benefit of anyone running into this thread.

    Never forget: For Golang to deliver its magic the OS has to be able to find it. Enter the $GOPATH environment variable. If you are anything like me you will have followed instructions somewhere on the net on how to do this. I followed the instructions in this post on Vultr. The gist of what they suggest

    echo export GOPATH=/opt/gopkg >> ~/.bashrc
    echo export GOROOT=/opt/go >> ~/.bashrc
    echo export PATH=$PATH:$GOROOT/bin:$GOPATH/bin >> ~/.bashrc
    

    Excellent instructions - thank you Vultr.

    The Key Isssue

    As I mention in my original question what puzzled me was that when I executed the batch file gorun containing

    go run /path/to/index.go &
    

    from a terminal session attached to the Docker session it worked. However, when I tried to execute it from a bash script executed at the start of the Docker session it did not.

    When you think about this a bit and look at what is happening in the Vultr instructions above it is all blindingly obvious. The path variables are being set ONLY WHEN AN INTERACTIVE TERMINAL IS OPEN! - i.e. NOT when you attempt to execute a go dosomething instruction from any old batch file - for instance the one you run when the Docker container is launched.

    If you want to have Golang operate correctly from within a Docker container right at the top of the bash script you run on container startup you will need to issue

    export GOPATH="/opt/gopkg"; 
    export GOROOT="/opt/go";   
    export PATH=$PATH:$GOPATH/bin:$GOROOT/bin; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧