Hi,
I tried two different ways for creating a go binary and then executing that
binary inside a Linux docker container. One option is working but the
second way gives errors. I am trying to understand the difference between
the two ways of creating the binary.
Thanks in advance for the help.
Here is the same code. It is a simple "hello world" app with date printed
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println("Hello World")
fmt.Printf("The time now is %v\n", time.Now())
}
Option # 1 (NOT Working)
Step 1. Compiled go file in to a binary using below
$ go build main.go
Step 2. Used docker container and run it
$docker run -it -v
/Users/XX/go/src/awesomeProject1:/Users/XX/go/src/awesomeProject1
--name raju_test ubuntu:latest /bin/bash
This is the error:
root@b942881ac13d:/Users/XX/go/src/awesomeProject1# ./main
bash: ./main: cannot execute binary file: Exec format error
root@b942881ac13d:/Users/XX/go/src/awesomeProject1#
Option #2 (Working)
Step 1. Compiled go file into binary using a docker go-builder image
docker run --rm -v
/Users/XX/go/src/awesomeProject1:/Users/XX/go/src/awesomeProject1
-w /Users/XX/go/src/awesomeProject1 -e GOPATH=/Users/XX/go go-builder:1.7.3
go build -o somebin main.go
Step 2
$docker run -it -v
/Users/XX/go/src/awesomeProject1:/Users/XX/go/src/awesomeProject1
--name raju_test ubuntu:latest /bin/bash
root@b942881ac13d:/Users/XX/go/src/awesomeProject1# ./somebin
Hello World
The time now is 2018-02-25 20:25:22.374418554 +0000 UTC
root@b942881ac13d:/Users/XX/go/src/awesomeProject1#
Notice the difference between the size of binaries created in the two
options above.
root@b942881ac13d:/Users/XX/go/src/awesomeProject1# ls -l
total 3364
drwxr-xr-x 15 root root 510 Feb 24 15:33 Common
-rwxr-xr-x 1 root root 1691696 Feb 24 16:38 main
-rw-r--r-- 1 root root 138 Feb 25 19:39 main.go
-rwxr-xr-x 1 root root 1726694 Feb 24 20:11 somebin
My end goal is simple. I am trying to build a Go binary and just move the
file to another container and run it there without any Go SDK.
Thanks
Raju
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.