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
gorun
from the terminal. The Golang server fired up right away and everything was hunkry-dory.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.
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;