Home > Backend Development > Golang > Why does 'go version' fail in an Alpine Docker image?

Why does 'go version' fail in an Alpine Docker image?

DDD
Release: 2024-11-26 00:29:10
Original
602 people have browsed it

Why does

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
Copy after login
Copy after login

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
Copy after login
Copy after login

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}"
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template