如何强制 Docker 使用 bash 而不是 sh 运行我的脚本

如何强制 Docker 使用 bash 而不是 sh 运行我的脚本

这是我的脚本:

#!/bin/bash

# some bash code here

这是我的Dockerfile

FROM node:lts-bullseye-slim
COPY . .
RUN /Script.sh

这是我得到的错误:

Step 5/5 : RUN /Script.sh
 ---> Running in 09bbdebbc3d7
/Script.sh: line 25: syntax error: unexpected end of file
The command '/bin/sh -c /Script.sh' returned a non-zero code: 2

但是,如果我以交互模式运行容器并使用docker exec -it container_name bash并运行脚本输入它,它就会起作用。

我认为它在构建过程中失败是因为 Docker 想要使用sh而不是bash.

如何强制 Docker 使用 bash,而不是 sh?

更新

这是我真正的脚本,它的真实名称是BuildScript,它没有扩展名,它位于根目录中,并且具有 777 权限:

#!/bin/bash

function RemoveDevelopmentItems()
{
    echo "Removing development items ..."
    rm -rf ${RepositoryPath}/Host
}

function BuildDirectories()
{
    echo "Building directories ..."
    mkdir -p ${RepositoryPath}/src
    mkdir -p ${RepositoryPath}/public
    mkdir -p ${RepositoryPath}/public/favicons
    mkdir -p ${RepositoryPath}/public/Fonts
    mkdir -p ${RepositoryPath}/src/Base
    mkdir -p ${RepositoryPath}/src/Contexts
    mkdir -p ${RepositoryPath}/src/Components
    mkdir -p ${RepositoryPath}/src/Hooks
    mkdir -p ${RepositoryPath}/src/Panel
}

function CopyCommon()
{
    echo "Copying common ..."
    cp -a /${Organization}/Common/Branding/Favicons/* ${RepositoryPath}/public/favicons
    cp -r /${Organization}/Common/Branding ${RepositoryPath}/src/Branding
    cp -r /${Organization}/Common/Logo.jsx ${RepositoryPath}/src/Logo.jsx
}

function CopyBase()
{
    echo "Copying base ..."
    cp -r /HolismPanel/Infra/src/Base ${RepositoryPath}/src
    cp -r /HolismPanel/Infra/src/Components ${RepositoryPath}/src
    cp -r /HolismPanel/Infra/src/Contexts ${RepositoryPath}/src
    cp -r /HolismPanel/Infra/src/Fonts ${RepositoryPath}/public
    cp -r /HolismPanel/Infra/src/Hooks ${RepositoryPath}/src
    cp -r /HolismPanel/Infra/src/Panel ${RepositoryPath}/src
    cp /HolismPanel/Infra/index.html ${RepositoryPath}/index.html
    cp /HolismPanel/Infra/package.json ${RepositoryPath}
    cp /HolismPanel/Infra/postcss.config.js ${RepositoryPath}
    cp /HolismPanel/Infra/src/index.css ${RepositoryPath}/src
    cp /HolismPanel/Infra/src/main.jsx ${RepositoryPath}/src
    cp /HolismPanel/Infra/tailwind.config.js ${RepositoryPath}
    cp /HolismPanel/Infra/vite.config.js ${RepositoryPath}
}

function CopyDependencies()
{
    echo "Copying dependencies ..."
    find /HolismPanel -mindepth 1 -maxdepth 1 -not -name Infra |
    while read DependencyPath;
    do
        DependencyName=$(basename $DependencyPath);
        if [[ ${Repository} == *Admin* ]]; then
            mkdir -p ${RepositoryPath}/src/$DependencyName
            cp -a /HolismPanel/$DependencyName/Admin/* ${RepositoryPath}/src/$DependencyName
        fi
    done
}

function MoveMainRepoFiles()
{
    echo "Moving main repo files ..."
    mv ${RepositoryPath}/Menu.jsx ${RepositoryPath}/src/Menu.jsx
    mv ${RepositoryPath}/Routes.jsx ${RepositoryPath}/src/Routes.jsx
    mv ${RepositoryPath}/HeaderActions.jsx ${RepositoryPath}/src/HeaderActions.jsx
    find ${RepositoryPath} -mindepth 1 -maxdepth 1 -type d -not -name src -not -name public |
    while read MainRepoDirectory;
    do
        mv $MainRepoDirectory ${RepositoryPath}/src
    done
}

function LinkNodeModules()
{
    echo "Linking node_modules ..."
    ln -s /HolismPanel/Infra/node_modules ${RepositoryPath}
}

RemoveDevelopmentItems
BuildDirectories
CopyCommon
CopyBase
CopyDependencies
MoveMainRepoFiles
LinkNodeModules

并且请不要告诉我您应该添加扩展名,或者不创建 777 文件,或者不在根目录中工作。谢谢。

答案1

RUN /Script.sh是指令的 shell 形式RUN,它将执行/bin/sh -c <command>.要直接执行脚本,请使用执行数组形式改为:RUN ["/Script.sh"].确保您的脚本可执行(运行chmod +x Script.sh)。也不要使用根目录作为工作目录。

相关内容