linux - 为什么 Dockerfile 中的命令会失败,而在容器中手动运行它却可以正常工作?

标签 linux bash docker debian

我正在创建一个简单的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/

相关文章:

linux - 如何确保正在运行的进程是我期望正在运行的进程?

linux - 如何使用 linux 命令使用递归搜索查找具有给定目录下位置的文件名列表以及精确模式匹配

linux - 更好的解析输入的方法

docker - 为什么Docker中的uWSGI无法通过CMD而是通过命令行运行?

docker - 当我指定文件时,"docker-compose build"向我显示帮助文本

postgresql - 如何在 docker centos 6 镜像上启动 postgres 9.6 服务

linux - 从 gitlab ci 中的 shell 脚本安装后,节点和 npm 无法识别

linux - shell 脚本: Excecute a command in "screen"

linux - 如何使用 docker swarm 扩展具有多个暴露端口和多个卷的应用程序?

linux - 使用 sed 命令时,我收到以下错误 : Function cannot be parsed. 任何解决方案或原因?