0

I download and install Go programming language like this:

curl -OL https://golang.org/dl/go1.17.3.linux-amd64.tar.gz
tar -C /usr/local -xvf go1.17.3.linux-amd64.tar.gz
touch /etc/profile.d/go.sh
echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile.d/go.sh
source /etc/profile.d/go.sh

And it works. But I want to create a Docker image containing Go and ready to use. So I did like this in Dockerfile:

RUN curl -OL https://golang.org/dl/go1.17.3.linux-amd64.tar.gz
RUN tar -C /usr/local -xvf go1.17.3.linux-amd64.tar.gz
RUN touch /etc/profile.d/go.sh
RUN echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile.d/go.sh
RUN source /etc/profile.d/go.sh

But it gives me error:

 > [25/25] RUN source /etc/profile.d/go.sh:
#28 0.737 /bin/sh: 1: source: not found

How I have to do this? Im using ubuntu 20.04 as a base Docker image

Fcoder
  • 9,066
  • 17
  • 63
  • 100
  • 1
    `source` is only in bash, not sh; the comparable sh command is `.` (as in, `. somefile` instead of `source somefile`). – Charles Duffy Nov 04 '21 at 17:25
  • 2
    Also, `RUN source /etc/profile.d/go.sh` doesn't actually do anything useful. Each RUN happens in a new shell, so changes you make to variables in a previous RUN don't change which variables are set in the next one. – Charles Duffy Nov 04 '21 at 17:26
  • 1
    (BTW, https://pubs.opengroup.org/onlinepubs/009695299/utilities/dot.html is the POSIX specification for the `.` command). – Charles Duffy Nov 04 '21 at 17:30
  • ...if you want to permanently change the PATH (or any other environment variable) for all future `RUN`s in a Dockerfile, that's what the `ENV` statement is for. No need to muck with the shell profile at all. – Charles Duffy Nov 04 '21 at 17:31
  • There are official `go` docker images which do not require you to install go – jordanm Nov 04 '21 at 21:08

0 Answers0