gpt4 book ai didi

bash - 在Dockerfile中运行脚本以添加别名

转载 作者:行者123 更新时间:2023-12-02 19:21:36 24 4
gpt4 key购买 nike

我有这个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!!!字符串已打印到控制台),但是当我猛击Docker服务并查看我的 ~/.bashrc文件时,别名不存在。

编辑:

我在 Dockerfile配置文件中引用此 docker-compose.yml。该文件的相关部分如下:
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'

    关于bash - 在Dockerfile中运行脚本以添加别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59246533/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com