运行 Dockerfile 中用于添加别名的脚本

Run script in Dockerfile to add aliases

我有这个Dockerfile:

FROM webdevops/php-apache-dev:7.2
ADD docker-bootstrap.sh /docker-bootstrap.sh
RUN /docker-bootstrap.sh

docker-bootstrap.sh 看起来像这样:

#!/bin/bash
echo 'hello!!!'
cat >> ~/.bashrc <<EOT
alias hi='hello'
EOT

问题是,当我 运行 docker build --no-cache=true . 时,我看到这段代码正在 运行 (我看到 hello!!! 字符串打印到控制台),但是当我 bash 进入 Docker 服务并查看我的 ~/.bashrc 文件时,别名不存在。

编辑:

我在 docker-compose.yml 配置文件中引用此 Dockerfile。该文件的相关部分如下:

services:
  laravel:
    container_name: laravel
    build: .
    links:
      - laravelmysql
      - testmysql
    depends_on:
      - laravelmysql
      - testmysql
    ports:
      - 8445:443
    volumes:
      - .:/app
    environment:
      docker: 'true'
      WEB_DOCUMENT_ROOT: '/app/public'
      WEB_NO_CACHE_PATTERN: '\.(.*)$$'
      working_dir: '/app'

然后我用docker exec -it laravel /bin/bash进入服务

编辑:

我将 docker-compose.yml 文件剥离到下面,因为我没有依赖项:

version: '3.1'

services:
  laravel:
    container_name: laravel
    build: .
    ports:
      - 8445:443
    volumes:
      - .:/app
    environment:
      docker: 'true'
      WEB_DOCUMENT_ROOT: '/app/public'
      WEB_NO_CACHE_PATTERN: '\.(.*)$$'
      working_dir: '/app'

使用 docker-compose up 启动容器并 cat 加载 /root/.bashrc 文件表明 别名存在于此。

$ docker exec -it laravel bash
root@fb830bebfb0b:/#
root@fb830bebfb0b:/# cat /root/.bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.

# Note: PS1 and umask are already set in /etc/profile. You should not
# need this unless you want different defaults for root.
# PS1='${debian_chroot:+($debian_chroot)}\h:\w$ '
# umask 022

# You may uncomment the following lines if you want `ls' to be colorized:
# export LS_OPTIONS='--color=auto'
# eval "`dircolors`"
# alias ls='ls $LS_OPTIONS'
# alias ll='ls $LS_OPTIONS -l'
# alias l='ls $LS_OPTIONS -lA'
#
# Some more alias to avoid making mistakes:
# alias rm='rm -i'
# alias cp='cp -i'
# alias mv='mv -i'
alias hi='hello'
root@fb830bebfb0b:/#

需要注意的事项:

  • 您正在检查 root 用户的 .bashrc,因为 Dockerfile 中的 RUN 将以 root 用户身份执行命令,除非另有说明。
  • docker-bootstrap.sh 文件可用的执行权限。
  • 重建图像只是为了确定。

原答案

对我有用。我在这里错过了什么吗?请检查以下内容:

 ~/del $ cat docker-bootstrap.sh
#!/bin/bash
echo 'hello!!!'
cat >> ~/.bashrc <<EOT
alias hi='hello'
EOT
 ~/del $
 ~/del $
 ~/del $ cat Dockerfile
FROM webdevops/php-apache-dev:7.2
ADD docker-bootstrap.sh /docker-bootstrap.sh
RUN /docker-bootstrap.sh
 ~/del $
 ~/del $
 ~/del $ docker build --no-cache=true .
Sending build context to Docker daemon    126kB
Step 1/3 : FROM webdevops/php-apache-dev:7.2
 ---> 9c809301e050
Step 2/3 : ADD docker-bootstrap.sh /docker-bootstrap.sh
 ---> e90c2aa4cd44
Step 3/3 : RUN /docker-bootstrap.sh
 ---> Running in efdc2e899503
hello!!!
Removing intermediate container efdc2e899503
 ---> 567c22c68bb9
Successfully built 567c22c68bb9
 ~/del $
 ~/del $
 ~/del $ docker run -it 567c22c68bb9 -- cat /root/.bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.

# Note: PS1 and umask are already set in /etc/profile. You should not
# need this unless you want different defaults for root.
# PS1='${debian_chroot:+($debian_chroot)}\h:\w$ '
# umask 022

# You may uncomment the following lines if you want `ls' to be colorized:
# export LS_OPTIONS='--color=auto'
# eval "`dircolors`"
# alias ls='ls $LS_OPTIONS'
# alias ll='ls $LS_OPTIONS -l'
# alias l='ls $LS_OPTIONS -lA'
#
# Some more alias to avoid making mistakes:
# alias rm='rm -i'
# alias cp='cp -i'
# alias mv='mv -i'
alias hi='hello'