【问题标题】:Add alias to Docker during build在构建期间向 Docker 添加别名
【发布时间】:2017-10-24 02:33:09
【问题描述】:

由于我试图在容器的构建阶段编译程序,因此我在 .bashrc 中包含容器构建过程中的别名:

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

当我启动容器时,所有别名都可用。这已经很好了,但不是我想要的行为。

我已经在 google 周围发现,只有在使用交互式 shell 时才会加载 .bashrc 文件,而在容器的构建阶段并非如此。

我正在尝试使用以下方法强制加载我的别名:

RUN shopt -s expand_aliases

RUN shopt -s expand_aliases && alias

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

这令人惊讶地没有达到预期的结果。 [/讽刺关闭]

现在我的问题是:如何为容器的构建阶段设置别名?

问候

【问题讨论】:

    标签: bash docker build dockerfile aliases


    【解决方案1】:

    当 docker 执行每个 RUN 时,它会调用 SHELL 并将行的其余部分作为参数传递。默认外壳是/bin/sh。记录在案的here

    这里的问题是你需要为每一层执行设置别名,因为每个RUN都会启动一个新的shell。我没有找到让 bash 每次都读取 .bashrc 文件的非交互式方式。

    所以,只是为了好玩我这样做了,它正在工作:

    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
    

    输出

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-03
      • 1970-01-01
      • 2017-09-09
      • 2020-01-22
      • 1970-01-01
      • 2019-09-09
      相关资源
      最近更新 更多