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

255,245

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

Share:
255,245
iamdeit
Author by

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, 2022

Comments

  • iamdeit
    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
    Matt over 7 years
    The apk upgrade is not required.
  • phil294
    phil294 about 6 years
    OP asked for bash. sh is not bash.
  • kboom
    kboom about 6 years
    But this is very useful comment anyway - most people will be fine with sh - and it does not require additional 50mb of image size
  • Bevilaqua
    Bevilaqua almost 6 years
    Easy and straightforward. Most times we only need to run shitty commands (ls, ps, whatever), sh covers those scenarios. thanks!
  • scones
    scones over 5 years
    @kboom comments go in the comment section. this is not an answer to the original question.
  • valiano
    valiano over 5 years
    @kboom the bash package adds about 4MB to the size of alpine:3.8, roughly doubling it, but still far from 50MB.
  • towi
    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 to RUN apk add --no-cache python bash, which added 4MB.
  • Meiogordo
    Meiogordo about 4 years
    The initial part of the RUN command is unnecessary. You can just write RUN apk add --no-cache bash directly
  • Alex Montoya
    Alex Montoya about 4 years
    I prefer use the recomendation of @Yuva using RUN /bin/sh instead of RUN bin/bash
  • anubhava
    anubhava about 4 years
    That is entirely up to you, many people need bash specific feature.
  • fde-capu
    fde-capu over 3 years
    This answer takes in account that OP was confused about /bin/sh and /bin/bash; which will help people in any case.
  • WernerCD
    WernerCD over 3 years
    @phil294 I was looking for bash because I was expecting bash. Having sh instead is a perfectly acceptable alternative.
  • Hermes
    Hermes about 3 years
    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.
  • Hassan Al-Jeshi
    Hassan Al-Jeshi almost 3 years
    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.
  • Jinyu
    Jinyu almost 3 years
    no. it's not. the /bin/sh in alpine is actually a symbol link to /bin/busybox. it's still ash
  • aurelia
    aurelia over 2 years
    why should I use the --no-cache option?
  • abulka
    abulka about 2 years
    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.