0

I am trying to set up a docker image that downloads Phantomjs and sets up the alias in ~/.bash_rc and path in ~/.profile. Below is the Dockerfile that does this.

FROM node:8.9.4

RUN mkdir -p /usr/src/app

RUN wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2 \
    && tar xvjf phantomjs-2.1.1-linux-x86_64.tar.bz2

RUN mv phantomjs-2.1.1-linux-x86_64 /usr/src/app

WORKDIR /usr/src/app

COPY package*.json /usr/src/app/

RUN npm install

COPY . /usr/src/app

RUN  . usr/src/app/test.sh
RUN . ~/.bashrc
RUN . ~/.profile

EXPOSE 4219

CMD ["node_modules/casperjs/bin/casperjs","selftest"]

Also the script test.sh sets up the environment variable. Here are the contents of the script.

  #!/bin/sh

   echo "alias phantomjs='/usr/src/app/phantomjs-2.1.1-linux-x86_64/bin/phantomjs'" >> ~/.bashrc

   echo "PATH=\"$HOME/bin:$HOME/.local/bin:$PATH:/usr/src/app/phantomjs-2.1.1-linux-x86_64/bin\"" >> ~/.profile

But it returns this error now

 Step 10/16 : RUN . ~/.profile
    ---> Running in 87cdccc4fef7
    stdin: is not a tty

What should I do to make this script run?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Anshul Tripathi
  • 529
  • 2
  • 11
  • 28
  • 1
    afaik `source` command only works on bash shell. However, docker CMD will use /bin/sh shell by default. Fix: use `. ~/.bash_rc`. – slackmart Jun 07 '18 at 20:56
  • looks like you are sourcing the wrong filename ... – chenchuk Jun 07 '18 at 22:16
  • @chenchuk good catch, instead of `source /test.sh`, he should execute the script by using `sh /test.sh`. – slackmart Jun 07 '18 at 22:49
  • Step 9/11 : RUN sh /test.sh ---> Running in d196dd140455 sh: 0: Can't open /test.sh it still won't run – Anshul Tripathi Jun 08 '18 at 00:05
  • I can see you're copying the current directory inside /usr/src/app, if test script is inside of it just pass the whole path to sh. RUN sh /usr/src/app/test.sh – slackmart Jun 08 '18 at 04:00
  • You create `~/.bashrc` in line 3 of test.sh and sourcing `~/.bash_rc` at line #4. Is `~/.bash_rc` available? – Robert Ranjan Jun 08 '18 at 04:44
  • @RobertRanjan I have edited the post and now I am getting the tty error. – Anshul Tripathi Jun 08 '18 at 15:04
  • @slackmart I did that and have edited the script. Yeah I missed the part where I had forgotten that I was sourcing bash_rc instead of bashrc. But I am facing a new issue now. When I try to source .profile it returns the error - stdin: is not a tty. All the edits to the script and Dockerfile are there in the main post. – Anshul Tripathi Jun 08 '18 at 15:08
  • I think there is no effect on running .profile and .bashrc towards the end of Dockerfile. You can remove them and give a try. Please share the command you are running as well. – Robert Ranjan Jun 08 '18 at 15:09
  • 1
    Every time you have a RUN command, it runs in a new and different shell. Things you sourced into old shells don't apply to new ones. – Charles Duffy Jun 08 '18 at 15:14
  • `CMD ["bash", "-c", ". usr/src/app/test.sh; . ~/.bashrc; . ~/.profile; exec \"$@\"", "_", "node_modules/casperjs/bin/casperjs","selftest"]`. That said, to fix `not a tty`, you'll need to use the `-i` and `-t` arguments to `docker run`. – Charles Duffy Jun 08 '18 at 15:16
  • @CharlesDuffy So everything is working now. The only issue that remains now is that it is not sourcing the alias and path. When I login to the container and then source the bashrc and profile, it works. How do I make sure that when I run the CMD in Dockerfile. It sources the bashrc and profile. – Anshul Tripathi Jun 09 '18 at 13:51
  • `.bashrc` and `.profile` aren't honored for noninteractive shells -- they aren't *supposed* to work in this scenario. For the PATH, use the Docker `ENV` directive. And even if you source `.bashrc` into a noninteractive shell, aliases are turned off in noninteractive shells anyhow. You *could* use an exported function, but why aren't you just putting the directory containing`phantomjs` directly in the PATH and avoiding the need? – Charles Duffy Jun 09 '18 at 14:28

0 Answers0