Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make builder RUN use /bin/bash instead of /bin/sh #7281

Closed
dwatrous opened this issue Jul 28, 2014 · 17 comments
Closed

How to make builder RUN use /bin/bash instead of /bin/sh #7281

dwatrous opened this issue Jul 28, 2014 · 17 comments

Comments

@dwatrous
Copy link

I'm creating a Dockerfile build script and would like to use bash commands. RUN uses /bin/sh by default. Is there some way to tell RUN to use /bin/bash instead?

@cpuguy83
Copy link
Member

No, this hardcoded to "/bin/sh -c"
One could of course link /bin/bash to /bin/sh in a previous build step.

@dwatrous
Copy link
Author

I ended up changing my Dockerfile script to copy a file and use that rather than use the redirection I had been using in bash.

@tiborvass
Copy link
Contributor

@dwatrous did you try RUN ["echo", "hello"] will not be wrapped by /bin/sh IIRC. So if you absolutely want bash, you can invoke it: RUN ["/bin/bash", "-c", "echo hello all in one string"].
Let us know if it solves your problem.

@unclejack
Copy link
Contributor

bash can be used manually by using it in RUN instructions.

There's no intention or plan to make it possible to change it to bash.

I'll mark this as a doc issue because we might want to document this. It's the third or fourth time this question has popped up.

@duglin
Copy link
Contributor

duglin commented Aug 8, 2014

PR: #7489

@duglin
Copy link
Contributor

duglin commented Aug 10, 2014

This can be closed now due to: #7489

@ORESoftware
Copy link

so what's the story on this one?

@thaJeztah
Copy link
Member

@ORESoftware See the discussion above, and #22489, which added the SHELL Dockerfile instruction (Docker 1.12 and up)

  • If you want to use a different shell for a single RUN, use the Exec form (JSON) notation - as is being discussed above:

    RUN ["/bin/bash", "-c", "echo I am now using bash!"]
  • If you want to change the default shell in your image (and during build), use the SHELL Dockerfile instruction that was implemented in Builder shell configuration #22489:

    SHELL ["/bin/bash", "-c"]
    RUN echo I am now using bash!
  • Or, a combination of the above

    FROM ubuntu
    RUN echo I am using the default (/bin/sh)
    RUN ["/bin/bash", "-c", "echo I am using bash"]
    SHELL ["/bin/bash", "-c"]
    RUN echo I am using bash, which is now the default
    RUN ["/bin/sh", "-c", "echo I am using /bin/sh"]
    RUN ["echo", "I don't use a shell at all"]

@ghost
Copy link

ghost commented Mar 7, 2019

I know I am late to the party and yes this isn't the best solution, but it worked for what I needed

In my docker file after downloading and extracting the SDK I run these two commands

RUN cp /bin/bash /bin/sh
RUN echo "source ~/google-cloud-sdk/path.bash.inc" >> ~/.bashrc
RUN source ~/.bashrc

After that the gcloud command is available during the build and at run time

@qhaas
Copy link

qhaas commented Jan 3, 2020

For those interested in strict OCI reproducibility, the Dockerfile call SHELL doesn't seem to be in the OCIv1 specification, per podman 1.4:

STEP 2: SHELL ["/bin/bash", "-c"]
ERRO[0000] SHELL is not supported for OCI image format, [/bin/bash -c] will be ignored. Must use docker format

Update: Looks like thaJeztah is correct, some discussion regarding supporting this feature over at buildah: containers/buildah#507

Update2: BUILDAH_FORMAT=docker to the rescue

@thaJeztah
Copy link
Member

@qhaas don't think that has to do with OCI specifications, just the buildah implementation

@forkdevops
Copy link

RUN chsh -s /bin/bash
Add this in you docker file and the complete commands mentioned below this line will be executed with bash

@D-Pow
Copy link

D-Pow commented Apr 23, 2023

I know this is pretty old, but this worked for me to change the default shell to Bash both within RUN and CMD (place this at the top, before any RUN commands):

SHELL [ "/bin/bash", "-c" ]

ENV SHELL=/bin/bash

@Sciencoded
Copy link

I know this is pretty old, but this worked for me to change the default shell to Bash both within RUN and CMD (place this at the top, before any RUN commands):

SHELL [ "/bin/bash", "-c" ]

ENV SHELL=/bin/bash

By doing this, you only change the variable name. If you run ps -p $$from inside your container, you will see the old active shell.

image

@mghildiy
Copy link

mghildiy commented Sep 6, 2023

@Sciencoded , I tried this, but it gives me error for this part in my dockerfile:

RUN curl....
    && ....
    && ....
    && mv -nv -t /opt/nifi/nifi-current/lib/ -- /opt/nifi/nifi-1.10.0/lib/!(*jetty*).nar

/bin/bash: -c: line 1: syntax error near unexpected token ('`

mv in this form doesnt work.
Any idea of this?

@milinddeore
Copy link

What @tiborvass suggest is great, but in case you need a brust force using something like:

# Replace shell with bash so we can source files
RUN rm /bin/sh && ln -s /bin/bash /bin/sh

@edocollado
Copy link

What @tiborvass suggest is great, but in case you need a brust force using something like:

# Replace shell with bash so we can source files
RUN rm /bin/sh && ln -s /bin/bash /bin/sh

this works for me, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests