Having trouble getting app to build with Docker after moving to a "src" directory

49 views
Skip to first unread message

David Karr

unread,
Feb 10, 2022, 7:35:51 PM2/10/22
to golang-nuts
I had a small go application building successfully.  I had the go.mod and main.go in the root directory of the project, and I was building it pretty easily with a Makefile, which just did the following:

    CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o target/dist/linux-amd64

I decided I wanted to move the go source files out of the root dir, so I created a "src" directory, then moved "go.*" and my package subdirs to "src".  Getting this simple build to work was simple.

I also am constructing a multi-stage Dockerfile, which will be used in the CI build. This is a little more complicated, and I'm getting an error on the build command that I don't understand.

My Dockerfile looks something like this (... in some places):

------------------
        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 ls -lt
        #RUN go mod download

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

        FROM .../ubuntu-base:20.04

        COPY ...
        EXPOSE 8080

        ENTRYPOINT ...
------------------

The Docker build looks something like this (... in some places):
-------------------------
        docker build \
                --build-arg host_src_path=src \
                --build-arg packages=. \
                --build-arg executable_name=... \
                --build-arg cgo_enabled_01=1 \
                --build-arg goos=linux \
                --build-arg goarch=amd64 \
                -f Dockerfile -t target/dist/linux/amd64/... .
---------------------

When I run this, I see the following:
------------------------
        Step 10/17 : RUN CGO_ENABLED=$cgo_enabled_01 GOOS=$goos GOARCH=$goarch go build -o $executable_name $packages
         ---> Running in 62ef0147061e
        $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:12: dockerbuild] Error 1
-----------------------

I googled that error message about go.mod, but I don't understand the results, and I don't fully understand how to use GOPATH. I don't remember doing anything with it before.

I'm sure I'm operating on some misconceptions, but I'm not sure what I'm doing wrong.
Reply all
Reply to author
Forward
0 new messages