4

Since I am trying to compile a program during the build phase of a container, I'm including my aliases during the build of the container inside the .bashrc:

RUN cat /path/to/aliases.sh >> ~/.bashrc

When I start the container, all aliases are available. This is already good, but not the behavior that I want.

I've already google around and found out, that the .bashrc file is only loaded when using an interactive shell, which is not the case during the build phase of the container.

I'm trying to force the load of my aliases using:

RUN shopt -s expand_aliases

or

RUN shopt -s expand_aliases && alias

or

RUN /bin/bash -c "both commands listed above..."

Which surprisingly does not yield to the expected outcome. [/irony off]

Now my question: How can I set aliases for the build phase of the container?

Regards

Karlo Kraljic
  • 173
  • 2
  • 11

1 Answers1

3

When docker executes each RUN, it calls to the SHELL passing the rest of the line as an argument. The default shell is /bin/sh. Documented here

The problem here is that you need for each layer execution, to set the aliases, because a new shell is launched by each RUN. I didn't find a non-interactive way to get bash read the .bashrc file each time.

So, just for fun I did this, and it's working:

aliasshell.sh

#!/bin/bash

my_ls(){
  ls $@
}

$@

Dockerfile

FROM ubuntu

COPY aliasshell.sh /bin/aliasshell.sh

SHELL ["/bin/aliasshell.sh"]

RUN ls -l /etc/issue
RUN my_ls -l /etc/issue

Output:

docker build .
Sending build context to Docker daemon 4.096 kB
Step 1/5 : FROM ubuntu
 ---> f7b3f317ec73
Step 2/5 : COPY aliasshell.sh /bin/aliasshell.sh
 ---> Using cache
 ---> ccdfc54dd0ce
Step 3/5 : SHELL /bin/aliasshell.sh
 ---> Using cache
 ---> bb17a8bf1c3c
Step 4/5 : RUN ls -l /etc/issue
 ---> Running in 15ae8f0bb93b
-rw-r--r-- 1 root root 26 Feb  7 23:55 /etc/issue
 ---> 0337da801651
Removing intermediate container 15ae8f0bb93b
Step 5/5 : RUN my_ls -l /etc/issue                   <-------
 ---> Running in 5f58e0aa4e95
-rw-r--r-- 1 root root 26 Feb  7 23:55 /etc/issue
 ---> b5060d9c5e48
Removing intermediate container 5f58e0aa4e95
Successfully built b5060d9c5e48
Robert
  • 33,429
  • 8
  • 90
  • 94