Installing Go in Alpine Linux Docker Images: A Path Issue
In an attempt to install Go within an Alpine Docker image, an error ("sh: go: not found") appears when running the command "go version." This issue arises due to an incorrect PATH variable setup.
To rectify this issue, the correct approach is to set the PATH variable to include the directory where the Go binary is located, as shown below:
export PATH=/usr/local/go/bin:$PATH
In the provided steps to reproduce the error, the PATH variable is set to include the wrong directory (/usr/local/go/, which does not contain the Go binary) and should be corrected to the following:
export PATH=/usr/local/go/bin:$PATH
Alternatively, multi-stage builds can be used to resolve this issue. Here's an example of a multi-stage build:
FROM XXX # Your base image FROM golang:1.13-alpine COPY --from=golang:1.13-alpine /usr/local/go/ /usr/local/go/ ENV PATH="/usr/local/go/bin:${PATH}"
This multi-stage build copies the Go installation from the golang:1.13-alpine image into the final image, ensuring the PATH variable is correctly set.
The above is the detailed content of Why does 'go version' fail in an Alpine Docker image?. For more information, please follow other related articles on the PHP Chinese website!