Home > other >  How to install a golang package in a docker file?
How to install a golang package in a docker file?

Time:04-27

I'm new in docker and I want to setting-up a docker-compose for my django app. in the backend of my app, I have golang packages too and run that in djang with subprocess library.
But, when I want to install a package using go install github.com/x/y@latest and then copy its binary to the project directory, it gives me the error: package github.com/x/y@latest: cannot use path@version syntax in GOPATH mode
I searched a lot in the internet but didn't find a solution to solve my problem. Could you please tell me where I'm wrong?\

here is my Dockerfile:

FROM python:3.8.11-bullseye
       
# Install packages
RUN apt-get update \
    && apt-get -y install --no-install-recommends software-properties-common git golang \
    && rm -rf /var/lib/apt/lists/*
RUN go install github.com/x/y@latest && cp pacakge /usr/src/toolkit/toolkit/scripts/webapp/
 

CodePudding user response:

This looks like a really good candidate for a multi-stage build:

FROM golang:1.18.0 as go-build
       
# Install packages
RUN go install github.com/x/y@latest \
 && cp $GOPATH/bin/pacakge /usr/local/bin/
 
FROM python:3.8.11-bullseye as release
...
COPY --from=go-build /usr/local/bin/package /usr/src/toolkit/toolkit/scripts/webapp/
...

CodePudding user response:

GOPATH mode does not work with Golang modules, in your Dockerfile file, add:
RUN unset GOPATH

  • Related