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

8

446

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
Italicize answered 3/12, 2016 at 5:14 Comment(4)
For googlers: docker run --rm -i -t alpine /bin/shFinegrained
Start container ( interactively): docker exec -it container_id /bin/shDigestible
Possibly this could get edited to include the phrase "and I want to use bash, rather than sh as my shell"?Crafty
Another option is to alias ash to bash. ln -s /bin/ash /bin/bash. But you are setting yourself up for confusing errors :)Oreilly
R
688

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
Rodrich answered 3/12, 2016 at 5:18 Comment(6)
The apk upgrade is not required.Thebaine
I prefer use the recomendation of @Yuva using RUN /bin/sh instead of RUN bin/bashAge
That is entirely up to you, many people need bash specific feature.Rodrich
why should I use the --no-cache option?Utilitarianism
@aurelia, not to keep the downloaded app/package in the cache. usually, when you install an app in Linux with apk/apt-get they download the app first and install it but do not remove it after the installation is completed, which takes storage space.Sherer
Easier solution: https://mcmap.net/q/80427/-docker-how-to-use-bash-with-an-alpine-based-docker-imageCounterclockwise
S
456

Try using RUN /bin/sh instead of bash.

Sloth answered 3/12, 2016 at 5:24 Comment(13)
OP asked for bash. sh is not bash.Monograph
But this is very useful comment anyway - most people will be fine with sh - and it does not require additional 50mb of image sizeMortification
Easy and straightforward. Most times we only need to run shitty commands (ls, ps, whatever), sh covers those scenarios. thanks!Rummage
@Mortification comments go in the comment section. this is not an answer to the original question.Burnett
@Mortification the bash package adds about 4MB to the size of alpine:3.8, roughly doubling it, but still far from 50MB.Transpire
@Mortification 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.Evers
This answer takes in account that OP was confused about /bin/sh and /bin/bash; which will help people in any case.Ib
@Monograph I was looking for bash because I was expecting bash. Having sh instead is a perfectly acceptable alternative.Lowspirited
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.Niple
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.Butta
no. it's not. the /bin/sh in alpine is actually a symbol link to /bin/busybox. it's still ashErskine
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.Sematic
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.Deb
T
29
RUN /bin/sh -c "apk add --no-cache bash"

worked for me.

Trauma answered 4/1, 2017 at 2:28 Comment(1)
The initial part of the RUN command is unnecessary. You can just write RUN apk add --no-cache bash directlyHartle
C
15

Option 🐈: Start from Bash

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.

Option 🐕: Use your Existing Base Image

If you want to use your existing base image, while avoiding the need to install bash on every container boot, then you can add this to your Dockerfile.

# Use openjdk:8-jdk-alpine as the base image
FROM openjdk:8-jdk-alpine

# Install bash package
RUN apk add --no-cache bash
Counterclockwise answered 5/7, 2022 at 9:56 Comment(2)
I have upvoted this answerRodrich
@Rodrich I have upvoted your upvoting.Oates
C
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

Commune answered 2/12, 2019 at 11:35 Comment(0)
B
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.

Bedizen answered 15/2, 2022 at 22:6 Comment(0)
C
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)
Chlorothiazide answered 18/4, 2023 at 14:26 Comment(0)
C
0

USER root

RUN apk add --no-cache bash

/bin/sh is good, but not enough to use. Sometimes /bin/bash is necessary. So I used above code to install bash in alpine.

Cathedral answered 2/11, 2023 at 6:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.