Docker: How to use bash with an Alpine based docker image?

BashDockerDockerfileAlpine

Bash Problem Overview


I created a docker image from openjdk:8-jdk-alpine but 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

Bash Solutions


Solution 1 - Bash

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)

Solution 2 - Bash

Try using RUN /bin/sh instead of bash.

Solution 3 - Bash

RUN /bin/sh -c "apk add --no-cache bash"

worked for me.

Solution 4 - Bash

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

Solution 5 - Bash

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioniamdeitView Question on Stackoverflow
Solution 1 - BashanubhavaView Answer on Stackoverflow
Solution 2 - BashYuvaView Answer on Stackoverflow
Solution 3 - Bashuser1738546View Answer on Stackoverflow
Solution 4 - BashSahith VibudhiView Answer on Stackoverflow
Solution 5 - BashOnat KorucuView Answer on Stackoverflow