Docker: How to use bash with an Alpine based docker image?
Solution 1
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
Try using RUN /bin/sh
instead of bash.
Solution 3
RUN /bin/sh -c "apk add --no-cache bash"
worked for me.
Solution 4
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
iamdeit
Computer engineer able to analyze problems and design solutions using multiple technologies. Data Science experience. Interested in academic research and algorithm analysis. Technologies: Python, R, NodeJs, Java, SQL, Ruby, C++, NoSQL Databases I support the free software movement and the ideology of sharing knowledge around the world.
Updated on July 27, 2022Comments
-
iamdeit almost 2 years
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
-
Matt over 7 yearsThe
apk upgrade
is not required. -
phil294 about 6 yearsOP asked for bash. sh is not bash.
-
kboom about 6 yearsBut this is very useful comment anyway - most people will be fine with sh - and it does not require additional 50mb of image size
-
Bevilaqua almost 6 yearsEasy and straightforward. Most times we only need to run shitty commands (ls, ps, whatever), sh covers those scenarios. thanks!
-
scones over 5 years@kboom comments go in the comment section. this is not an answer to the original question.
-
valiano over 5 years@kboom the
bash
package adds about 4MB to the size ofalpine:3.8
, roughly doubling it, but still far from 50MB. -
towi about 5 years@kboom I can confirm that adding bash adds about 4MB, not 50MB. I just changed
RUN apk add --no-cache python
toRUN apk add --no-cache python bash
, which added 4MB. -
Meiogordo about 4 yearsThe initial part of the RUN command is unnecessary. You can just write
RUN apk add --no-cache bash
directly -
Alex Montoya about 4 yearsI prefer use the recomendation of @Yuva using
RUN /bin/sh
instead ofRUN bin/bash
-
anubhava about 4 yearsThat is entirely up to you, many people need
bash
specific feature. -
fde-capu over 3 yearsThis answer takes in account that OP was confused about
/bin/sh
and/bin/bash
; which will help people in any case. -
WernerCD over 3 years@phil294 I was looking for bash because I was expecting bash. Having sh instead is a perfectly acceptable alternative.
-
Hermes about 3 yearsThis 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 installbash
as it increases the size of the image. -
Hassan Al-Jeshi almost 3 yearsThis 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.
-
Jinyu almost 3 yearsno. it's not. the
/bin/sh
in alpine is actually a symbol link to/bin/busybox
. it's still ash -
aurelia over 2 yearswhy should I use the
--no-cache
option? -
abulka about 2 yearsConfirmed that bash adds about 4MB, not 50MB. If you experiment with a simple Dockerfile
FROM alpine
andRUN apk update && apk add bash
the resulting image is only 9.65 MB. Alpine alone is about 5.32 MB. Of course if youFROM python:alpine
you are going to get your 50MB but that's because of the Python in the image, not bash.