我正在创建一个简单的Dockerfile
:
FROM debian:buster
RUN apt update && apt upgrade -y
RUN apt install git golang -y
RUN echo "export GOPATH=$HOME" >> ~/.bash_profile
RUN source /root/.bash_profile
RUN echo $GOPATH
但它在 source/root/.bash_profile
处失败,并出现错误:
/bin/sh: 1: source: not found
The command '/bin/sh -c source /root/.bash_profile' returned a non-zero code: 127
我评论了该行,构建了图像,然后从容器内运行相同的命令,然后它运行良好。
我尝试了很多变体,包括手动调用 /bin/bash
,但它似乎在 Dockerfile
中不起作用。
有人知道我在这里做错了什么吗?
[编辑]
我还尝试将该行替换为
RUN export GOPATH=$HOME
和
RUN export GOPATH=/root
但是它之后的回显给出了空结果。
最佳答案
将其添加到您的 dockerfile 中:
FROM debian:buster
RUN apt update && apt upgrade -y
RUN apt install git golang -y
ARG GOPATH=/PATH/TO/
ENV GOPATH ${GOPATH}
RUN echo $GOPATH
这是在 Docker 中提供系统变量的正确方法,源和导出只能在 dockerfile 中的单个步骤中工作
关于linux - 为什么 Dockerfile 中的命令会失败,而在容器中手动运行它却可以正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58816763/