Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

docker

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:/# 
like image 226
mattes Avatar asked Apr 12 '14 14:04

mattes


1 Answers

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
like image 52
Daniel Mahu Avatar answered Oct 21 '22 19:10

Daniel Mahu