mattes
mattes

Reputation: 9439

docker run -i -t image /bin/bash - source files first

This works:

# echo 1 and exit:
$ docker run -i -t image /bin/bash -c "echo 1"
1
# exit

# echo 1 and return shell in docker container:
$ docker run -i -t image /bin/bash -c "echo 1; /bin/bash"
1
root@4c064f2554de:/# 

Question: How could I source a file into the shell? (this does not work)

$ docker run -i -t image /bin/bash -c "source <(curl -Ls git.io/apeepg) && /bin/bash"
# content from http://git.io/apeepg is sourced and shell is returned
root@4c064f2554de:/# 

Upvotes: 13

Views: 10462

Answers (4)

Erik Dannenberg
Erik Dannenberg

Reputation: 6106

You can use .bashrc in interactive containers:

RUN curl -O git.io/apeepg.sh && \
    echo 'source apeepg.sh' >> ~/.bashrc

Then just run as usual with docker run -it --rm some/image bash.

Note that this will only work with interactive containers.

Upvotes: 1

Daniel Mahu
Daniel Mahu

Reputation: 316

I wanted something similar, and expanding a bit on your idea, came up with the following:

docker run -ti --rm ubuntu \
  bash -c 'exec /bin/bash --rcfile /dev/fd/1001 \
                          1002<&0 \
                          <<<$(echo PS1=it_worked: ) \
                          1001<&0 \
                          0<&1002'
  • --rcfile /dev/fd/1001 will use that file descriptor's contents instead of .bashrc
  • 1002<&0 saves stdin
  • <<<$(echo PS1=it_worked: ) puts PS1=it_worked: on stdin
  • 1001<&0 moves this stdin to fd 1001, which we use as rcfile
  • 0<&1002 restores the stdin that we saved initially

Upvotes: 2

NHK
NHK

Reputation: 789

In my case, I use RUN source command (which will run using /bin/bash) in a Dockerfile to install nvm for node.js

Here is an example.

FROM ubuntu:14.04

RUN rm /bin/sh && ln -s /bin/bash /bin/sh
...
...
RUN source ~/.nvm/nvm.sh && nvm install 0.11.14

Upvotes: 4

Michael Sauter
Michael Sauter

Reputation: 271

I don't think you can do this, at least not right now. What you could do is modify your image, and add the file you want to source, like so:

FROM image
ADD my-file /my-file
RUN ["source", "/my-file", "&&", "/bin/bash"]

Upvotes: -2

Related Questions