Dockerfile: "RUN" command error with "export" and "apt-get" - "bad variable name"
This article addresses a common error encountered in Dockerfiles when using the "RUN" command to install packages via "apt-get" while also setting environment variables using "export." Specifically, the error arises due to improper usage of the "export" command, which leads to the message "bad variable name."
The problematic code snippet appears as follows:
RUN --mount=type=cache,sharing=locked,id=moby-build-aptlib,target=/var/lib/apt \
--mount=type=cache,sharing=locked,id=moby-build-aptcache,target=/var/cache/apt \
export http_proxy=http://192.168.44.37:7890 \
export https_proxy=http://192.168.44.37:7890 \
apt-get update && apt-get install --no-install-recommends -y \
clang \
lld \
llvm
The error occurs because the "export" command is used incorrectly. It should be followed by a variable name, but in this case, it's followed by the command "apt-get." The correct approach is to separate the "export" command from the "apt-get" command. Here's the corrected code:
RUN --mount=type=cache,sharing=locked,id=moby-build-aptlib,target=/var/lib/apt \
--mount=type=cache,sharing=locked,id=moby-build-aptcache,target=/var/cache/apt \
export http_proxy=http://192.168.44.37:7890 && \
export https_proxy=http://192.168.44.37:7890
RUN apt-get update && apt-get install --no-install-recommends -y \
clang \
lld \
llvm
By separating these commands into separate "RUN" instructions, you ensure that each command is executed correctly. This resolves the "bad variable name" error and allows your Dockerfile to build successfully.
原文地址: https://www.cveoy.top/t/topic/jms7 著作权归作者所有。请勿转载和采集!