425

I created a docker image from openjdk:8-jdk-alpine and I want to use bash, rather than sh as my shell, however when I try to execute simple commands I get the following errors:

RUN bash
/bin/sh: bash: not found

RUN ./gradlew build
env: can't execute 'bash': No such file or directory
anubhava
  • 761,203
  • 64
  • 569
  • 643
iamdeit
  • 5,485
  • 4
  • 26
  • 40

7 Answers7

662

Alpine docker image doesn't have bash installed by default. You will need to add the following commands to get bash:

RUN apk update && apk add bash

If you're using Alpine 3.3+ then you can just do:

RUN apk add --no-cache bash

To keep the docker image size small. (Thanks to comment from @sprkysnrky)

If you just want to connect to the container and don't need bash, you can use:

docker run --rm -i -t alpine /bin/sh --login
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
anubhava
  • 761,203
  • 64
  • 569
  • 643
448

Try using RUN /bin/sh instead of bash.

YYY
  • 6,107
  • 1
  • 12
  • 14
  • 101
    OP asked for bash. sh is not bash. – phil294 Mar 14 '18 at 06:46
  • 216
    But this is very useful comment anyway - most people will be fine with sh - and it does not require additional 50mb of image size – kboom Mar 30 '18 at 18:24
  • 12
    Easy and straightforward. Most times we only need to run shitty commands (ls, ps, whatever), sh covers those scenarios. thanks! – Bevilaqua Jun 15 '18 at 00:28
  • 2
    @kboom comments go in the comment section. this is not an answer to the original question. – scones Dec 20 '18 at 12:20
  • 21
    @kboom the `bash` package adds about 4MB to the size of `alpine:3.8`, roughly doubling it, but still far from 50MB. – valiano Feb 01 '19 at 14:02
  • 1
    @kboom I can confirm that adding bash adds about 4MB, not 50MB. I just changed `RUN apk add --no-cache python` to `RUN apk add --no-cache python bash`, which added 4MB. – towi May 15 '19 at 12:51
  • This answer takes in account that OP was confused about `/bin/sh` and `/bin/bash`; which will help people in any case. – fde-capu Oct 09 '20 at 21:56
  • 1
    @phil294 I was looking for bash because I was expecting bash. Having sh instead is a perfectly acceptable alternative. – WernerCD Dec 05 '20 at 05:45
  • 4
    This should be a comment rather than an answer. At the same time I agree that the accepted answer should include a comment that if possible one should stick to `sh` rather than install `bash` as it increases the size of the image. – Hermes May 15 '21 at 12:42
  • This answer helped me a lot. I didn't want to rebuild my image just to check on a file inside the container. I'm sure that many people are looking for bash but any alternative is also perfectly fine. – Hassan Al-Jeshi Jun 09 '21 at 10:45
  • 2
    no. it's not. the `/bin/sh` in alpine is actually a symbol link to `/bin/busybox`. it's still ash – Jinyu Jul 02 '21 at 09:58
  • Confirmed that bash adds about 4MB, not 50MB. If you experiment with a simple Dockerfile `FROM alpine` and `RUN apk update && apk add bash` the resulting image is only 9.65 MB. Alpine alone is about 5.32 MB. Of course if you `FROM python:alpine` you are going to get your 50MB but that's because of the Python in the image, not bash. – abulka Apr 12 '22 at 01:00
  • `FROM nginx:1.22.0-alpine` Adding Bash changes the size from 22.1 to 23.7. `/bin/sh` is extremely limited, it does not even support UP/DOWN arrows so it'll be a total PITA to shell into to debug. – Ivan Kleshnin Sep 05 '22 at 14:31
27
RUN /bin/sh -c "apk add --no-cache bash"

worked for me.

user1738546
  • 619
  • 6
  • 15
  • 16
    The initial part of the RUN command is unnecessary. You can just write `RUN apk add --no-cache bash` directly – Meiogordo Apr 01 '20 at 16:26
14

To Install bash you can do:

RUN apk add --update bash && rm -rf /var/cache/apk/*

If you do not want to add extra size to your image, you can use ash or sh that ships with alpine.

Reference: https://github.com/smebberson/docker-alpine/issues/43

Sahith Vibudhi
  • 4,935
  • 2
  • 32
  • 34
13

The official bash image is based on Alpine and prevents you from needing to install bash every time. Simply use

docker pull bash

This was first published on Oct 19, 2016 at 6:43 pm.

James Geddes
  • 742
  • 3
  • 10
  • 35
3

If you have the option (for instance if you are just creating the script), using an alpine image with bash installed such as alpine-bash might be clever.

Onat Korucu
  • 992
  • 11
  • 13
0

It doesn't work because this docker image uses Busybox. Busybox is a popular minimal Docker base image that uses ash, a much more limited shell than bash.

If you use sbt-native-packager you just need to add support

enablePlugins(AshScriptPlugin)
Roman Kazanovskyi
  • 3,370
  • 1
  • 21
  • 22