linuxbashdockerdebian

Why does command from Dockerfile fail, while running it manually in the container works perfectly?


I'm creating a simple 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

But it fails at source /root/.bash_profile with the error:

/bin/sh: 1: source: not found
The command '/bin/sh -c source /root/.bash_profile' returned a non-zero code: 127

I commented that line away, built the image and then ran the same command from within the container, and then it runs fine.

I've tried many variations, including manually calling /bin/bash, but it doesn't seem to work from within the Dockerfile.

What am I doing wrong here?

Edit

I also tried replacing that line by

RUN export GOPATH=$HOME

and

RUN export GOPATH=/root

But the echo after it gives an empty result.


Solution

  • add this to your 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
    

    that is the correct way to provide the system Variable in Docker , source and export will only works in a single step in the dockerfile