I had earlier written a note about troubles refactoring my project to have 
a "src" directory, but I've backed off on that for now.  I see that the 
main problem I saw with that hasn't changed when backing out those changes. 
It appears that running this build inside a docker image seems to be 
somehow causing this problem.

In any case, I have a relatively simple go application with main.go, 
go.mod, and go.sum in the root directory, with subdirectories containing 
other source files in packages.

When I just run "go build ..." from the command line in the root directory 
of my project, it works fine and produces a working executable.

However, I'm trying to get this build to work within a published go 
"builder" image, which I will use as the first stage in a multi-stage 
Docker build.

I will first state that this has something to do with GOPATH, and I've gone 
through 
https://www.digitalocean.com/community/tutorials/understanding-the-gopath , 
but I'd have to say that I still don't understand its proper role here.

Not sure what you would want to see first, but here's a plain "ls" of the 
root directory of my project:
-------------------------
./         Dockerfile        go.mod           Jenkinsfile        README.md
../         .dockerignore        go.sum           lib/               
 target/
app/         .git/                handlers/  main.go        trustStore/
config/  .gitignore        include/   Makefile        funcs/
-----------------------

Just running "make build" does this:
---------------------
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o ../target/dist/linux-amd64
---------------------

Which is fine.

My "dockerbuild" target in the Makefile is this:
--------------------
dockerbuild:
        docker build \
                --build-arg host_src_path=. \
                --build-arg packages=. \
                --build-arg executable_name=appgo \
                --build-arg cgo_enabled_01=1 \
                --build-arg goos=linux \
                --build-arg goarch=amd64 \
                -f Dockerfile -t appgo .
-----------------------

My Dockerfile, before the start of the second build stage, is this:
----------------------------
        FROM .../golang:1.17.6-bullseye as go-builder

        ARG packages
        ARG executable_name
        ARG host_src_path
        ARG cgo_enabled_01
        ARG goos
        ARG goarch

        COPY $host_src_path .
        RUN env
        RUN ls -lt
        RUN ls -lt $GOPATH
        #RUN go mod download

        RUN CGO_ENABLED=$cgo_enabled_01 GOOS=$goos GOARCH=$goarch go build 
-o $executable_name $packages
--------------------

When I run "make dockerbuild", I see this:
------------------
        Sending build context to Docker daemon  125.8MB
        Step 1/19 : FROM .../golang:1.17.6-bullseye as go-builder
         ---> 80d9a75ccb38
        Step 2/19 : ARG packages
         ---> Using cache
         ---> d98015e225b1
        Step 3/19 : ARG executable_name
         ---> Using cache
         ---> 1d336ac5cf1b
        Step 4/19 : ARG host_src_path
         ---> Using cache
         ---> 59595b09a376
        Step 5/19 : ARG cgo_enabled_01
         ---> Using cache
         ---> 3c4c08392ede
        Step 6/19 : ARG goos
         ---> Using cache
         ---> 6ed0d6881fb4
        Step 7/19 : ARG goarch
         ---> Using cache
         ---> 9e4b94e86b09
        Step 8/19 : COPY $host_src_path .
         ---> 4727e2fc3e80
        Step 9/19 : RUN env
         ---> Running in 8fcedaa86227
        HOSTNAME=8fcedaa86227
        HOME=/root
        packages=.
        cgo_enabled_01=1
        goarch=amd64
        host_src_path=.
        
PATH=/go/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
        goos=linux
        GOPATH=/go
        executable_name=appgo
        PWD=/go
        GOLANG_VERSION=1.17.6
        Removing intermediate container 8fcedaa86227
         ---> 46e44a5a3539
        Step 10/19 : RUN ls -lt
         ---> Running in 75107286d337
        total 128
        -rw-rw-r-- 1 root root   635 Feb 11 18:37 Dockerfile
        -rw-rw-r-- 1 root root   737 Feb 11 18:29 Makefile
        -rw-rw-r-- 1 root root   131 Feb 10 18:59 Jenkinsfile
        drwxrwxrwx 2 root root  4096 Jan 27 10:09 bin
        drwxrwxrwx 2 root root  4096 Jan 27 10:09 src
        -rw-rw-r-- 1 root root   967 Jan 13 18:34 main.go
        drwxrwxr-x 3 root root  4096 Jan 11 23:46 target
        drwxrwxr-x 2 root root  4096 Jan 11 23:46 funcs
        drwxrwxr-x 3 root root  4096 Jan 11 21:06 trustStore
        -rw-rw-r-- 1 root root  1454 Jan 11 21:06 README.md
        drwxrwxr-x 2 root root  4096 Jan 11 21:06 app
        drwxrwxr-x 2 root root  4096 Jan 11 21:06 config
        -rw-rw-r-- 1 root root   806 Jan 11 21:06 go.mod
        -rw-rw-r-- 1 root root 65194 Jan 11 21:06 go.sum
        drwxrwxr-x 2 root root  4096 Jan 11 21:06 handlers
        drwxrwxr-x 3 root root  4096 Jan 11 21:06 include
        drwxrwxr-x 4 root root  4096 Jan 11 21:06 lib
        Removing intermediate container 75107286d337
         ---> a880f28ef2b2
        Step 11/19 : RUN ls -lt $GOPATH
         ---> Running in 220d4e8dbb33
        total 128
        -rw-rw-r-- 1 root root   635 Feb 11 18:37 Dockerfile
        -rw-rw-r-- 1 root root   737 Feb 11 18:29 Makefile
        -rw-rw-r-- 1 root root   131 Feb 10 18:59 Jenkinsfile
        drwxrwxrwx 2 root root  4096 Jan 27 10:09 bin
        drwxrwxrwx 2 root root  4096 Jan 27 10:09 src
        -rw-rw-r-- 1 root root   967 Jan 13 18:34 main.go
        drwxrwxr-x 3 root root  4096 Jan 11 23:46 target
        drwxrwxr-x 2 root root  4096 Jan 11 23:46 funcs
        drwxrwxr-x 3 root root  4096 Jan 11 21:06 trustStore
        -rw-rw-r-- 1 root root  1454 Jan 11 21:06 README.md
        drwxrwxr-x 2 root root  4096 Jan 11 21:06 app
        drwxrwxr-x 2 root root  4096 Jan 11 21:06 config
        -rw-rw-r-- 1 root root   806 Jan 11 21:06 go.mod
        -rw-rw-r-- 1 root root 65194 Jan 11 21:06 go.sum
        drwxrwxr-x 2 root root  4096 Jan 11 21:06 handlers
        drwxrwxr-x 3 root root  4096 Jan 11 21:06 include
        drwxrwxr-x 4 root root  4096 Jan 11 21:06 lib
        Removing intermediate container 220d4e8dbb33
         ---> 76c8e73bc8e8
        Step 12/19 : RUN CGO_ENABLED=$cgo_enabled_01 GOOS=$goos 
GOARCH=$goarch go build -o $executable_name $packages
         ---> Running in 28a92ca3224d
        $GOPATH/go.mod exists but should not
        The command '/bin/sh -c CGO_ENABLED=$cgo_enabled_01 GOOS=$goos 
GOARCH=$goarch go build -o $executable_name $packages' returned a non-zero 
code: 1
        make: *** [Makefile:11: dockerbuild] Error 1
--------------------

There are a few things to note here, starting with the error message at the 
end:
------------------
$GOPATH/go.mod exists but should not
------------------

Outside of my Docker container, $GOPATH is basically ~/go, and it just 
contains directories "bin" and "pkg".  However, inside the container, 
$GOPATH has all the code for my project, including the "go.mod" file, so at 
least the first part of that error message is accurate, but I don't 
understand the significance of that, or what I'm supposed to do to resolve 
this.

-- 
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 golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/7354bade-2c03-42b8-9fb1-530023ff5499n%40googlegroups.com.

Reply via email to