1

This is my script:

#!/bin/bash

# some bash code here

And this is my Dockerfile:

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

And here's the error I get:

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

However, if I run the container in the interactive mode and enter it using docker exec -it container_name bash and run the script, it works.

I think it fails in the build process because Docker wants to use sh and not bash.

How can I force Docker to use bash, and not sh?

Update

This is my real script, and its real name is BuildScript and it has no extensions and it's in the root directory and it has 777 permission:

#!/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

And please don't tell me that you should add an extension, or not make 777 files, or not work in the root directory. Thank you.

7
  • 1
    If it's running /bin/sh -c /Script.sh like the error says, the sh will execute the script using its shebang, which uses bash, so I'd say the problem is in your script. Maybe you have DOS line endings or something. How are you running the script inside docker exec -it container_name bash?
    – muru
    Commented Aug 24, 2022 at 4:31
  • 1
    That said, you can change the shell used for RUN using SHELL, or just do RUN bash /Script.sh
    – muru
    Commented Aug 24, 2022 at 4:32
  • @muru, using RUN bash /Script.sh did the trick. And it worked. The problem was not in my script file. Commented Aug 24, 2022 at 4:37
  • 1
    @SaeedNeamati The difference between RUN bash /script and RUN /script is the same as between sh -c 'bash /script' and sh -c '/script'. They should both have worked if the script had been executable and had the correct #! line. Moving to RUN bash /script would not by itself have made your issue go away. It is a pity that you don't show the script that generates the error.
    – Kusalananda
    Commented Aug 24, 2022 at 6:26
  • 1
    @SaeedNeamati And if you switch back it starts failing in the exact same way again? This would be a good test to see whether it was a temporary fluke or some actual error that was solved by changing the way you invoke the script.
    – Kusalananda
    Commented Aug 24, 2022 at 9:18

1 Answer 1

1

RUN /Script.sh is the shell form of the RUN instruction, which will execute /bin/sh -c <command>. To execute the script directly use the exec array form instead: RUN ["/Script.sh"]. Make sure your script is executable (run chmod +x Script.sh). Also don't use the root directory as working directory.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .