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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?