Reputation: 9439
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
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
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 .bashrc1002<&0
saves stdin<<<$(echo PS1=it_worked: )
puts PS1=it_worked:
on stdin1001<&0
moves this stdin to fd 1001, which we use as rcfile0<&1002
restores the stdin that we saved initiallyUpvotes: 2
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
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