2

I'm trying to create an alias to help debug my docker containers.

I discovered bash accepts a --init-file option which ought to let us run some commands before passing over to interactive mode.

So I thought I could do

docker-bash() {
  docker run --rm -it "$1" bash --init-file <(echo "ls; pwd")
}

But those commands don't appear to be running:

 % docker-bash c7460dfcab50
root@9c6f64a9db8c:/#

Is it an escaping issue or.. what's going on?

bash --init-file <(echo "ls; pwd")

Alone in a terminal on my host machine works as expected (runs the command starts a new bash instance).

0

2 Answers 2

5

In points:

  • The <(...) is a bash extension process subtitution.
  • From the manual above: Process substitution is supported on systems that support named pipes (FIFOs) or the /dev/fd method of naming open files..
  • The process substitution works like this:
    • bash creates a fifo in /tmp or creates a new file descriptor in /dev/fd.
    • The filename, either the /tmp/.something or /dev/fd/<number> is substituted for <(...) when command is executed.
    • So for example echo <(echo 1) outputs /dev/fd/63.
  • Docker works by creating a new environment that is separated from the host. That means that:
    • Processes inside docker do not inherit file descriptors from the host process:
      • So /dev/fd/* files are not inherited.
    • Processes inside docker are accessing isolated filesystem tree.
      • So processes can't access /tmp/* files from the host.
  • So summarizing docker run -ti --rm alpine cat <(echo 1) will not work, because the filename substituted by <(...) is not available from docker environment.

An easy workaround would be to just:

docker run -ti --rm alpine sh -c 'ls; pwd; exec sh'

Or use a temporary file:

echo "ls; pwd" > /tmp/tempfile
docker run -v /tmp/tempfile:/tmp/tempfile bash bash --init-file /tmp/tempfile
3
  • I don't think that's strictly equivalent, is it? I should have specified what I really want to do is define an alias or two and exec sh will start another new shell so they'll be lost :-(
    – mpen
    Jan 19, 2020 at 22:28
  • 1
    Right, exactly. You can always do a temporary file. echo something > tempfile; docker run ... -v tempfile:/tmp/tempfile bash --init-file /tmp/tempfile. This is very similar problem to passing alias or anything else when doing ssh.
    – KamilCuk
    Jan 19, 2020 at 23:07
  • It took a while to realize the solution is underneath, maybe you could combine your answers? As your explanation is really valuable, but not solving the problem.
    – lony
    Mar 10, 2020 at 13:16
2

For my use-case I wanted to set an alias which won't persist if we re-exec the shell. However, aliases can be written to ~/.bashrc which will be reloaded on the subsequent exec. Ergo,

docker-bash() {
  docker run --rm -it "$1" bash -c $'set -o xtrace; echo "alias ll=\'ls -lAhtrF --color=always\'" >> ~/.bashrc; exec "$0"'
}

Works. --rm should clean up any files we create anyway if I understand properly how docker works.

Or perhaps this is a nicer way to write it:

docker-bash() {
read -r -d '' BASHRC << EOM
alias ll='ls -lAhtrF --color=always'
EOM
docker run --rm -it "$1" bash -c "echo \"$BASHRC\" >> ~/.bashrc; exec \"\$0\""
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.