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; 
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题
  • ¥15 主机可以ping通路由器但是连不上网怎么办
  • ¥15 数据库一张以时间排好序的表中,找出多次相邻的那些行
  • ¥50 关于DynamoRIO处理多线程程序时候的问题
  • ¥15 kubeadm部署k8s出错
  • ¥15 Abaqus打不开cae文件怎么办?
  • ¥20 双系统开机引导中windows系统消失问题?
  • ¥15 小程序准备上线,软件开发公司需要提供哪些资料给甲方
  • ¥15 关于生产日期批次退货退款,库存回退的问题