245

To start an interactive shell for the Ubuntu image we can run:

ole@T:~$ docker run -it --rm ubuntu
root@1a6721e1fb64:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

But when this is run for the Alpine Docker image, the following results:

ole@T:~$ docker run -it --rm alpine
Error response from daemon: No command specified

What is the command for starting an interactive shell in an Alpine base container?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ole
  • 41,793
  • 59
  • 191
  • 359

4 Answers4

426
ole@T:~$ docker run -it --rm alpine /bin/ash
(inside container) / # 

Options used above:

  • /bin/ash is Ash (Almquist Shell) provided by BusyBox
  • --rm Automatically remove the container when it exits (docker run --help)
  • -i Interactive mode (Keep STDIN open even if not attached)
  • -t Allocate a pseudo-TTY
Bruno Bronosky
  • 66,273
  • 12
  • 162
  • 149
Ole
  • 41,793
  • 59
  • 191
  • 359
  • 2
    Sure - good idea - ash is the shell and --rm removes the container after the run is complete. So if you want the container to still be available after the run then skip usage of --rm. I'm using it since I'm only experimenting ATM. – Ole Mar 02 '16 at 00:38
  • Then how to run it with detach property like ubuntu? – Benyamin Limanto Feb 05 '19 at 00:45
  • I believe in both cases ... Ubuntu or Alpine ... you just give it a `-d` option. – Ole Feb 05 '19 at 00:53
  • 6
    +1 for noting that Alpine has `ash` and not `bash`, which, not being very familiar with Alpine, is what got me and prevented me from accessing the container earlier. – code_dredd Mar 05 '19 at 01:21
  • If you are on RHEL and get permission denied error, it is SELinux again... gotta do that setenforce 0 temporarily :P – Parth Patel Jul 09 '19 at 18:28
97

Usually, an Alpine Linux image doesn't contain bash, Instead you can use /bin/ash, /bin/sh, ash or only sh.

/bin/ash

docker run -it --rm alpine /bin/ash

/bin/sh

docker run -it --rm alpine /bin/sh

ash

docker run -it --rm alpine ash

sh

docker run -it --rm alpine sh

I hope this information helps you.

jansanchez
  • 1,179
  • 8
  • 6
46

Nowadays, Alpine images will boot directly into /bin/sh by default, without having to specify a shell to execute:

$ sudo docker run -it --rm alpine  
/ # echo $0  
/bin/sh  

This is since the alpine image Dockerfiles now contain a CMD command, that specifies the shell to execute when the container starts: CMD ["/bin/sh"].

In older Alpine image versions (pre-2017), the CMD command was not used, since Docker used to create an additional layer for CMD which caused the image size to increase. This is something that the Alpine image developers wanted to avoid. In recent Docker versions (1.10+), CMD no longer occupies a layer, and so it was added to alpine images. Therefore, as long as CMD is not overridden, recent Alpine images will boot into /bin/sh.

For reference, see the following commit to the official Alpine Dockerfiles by Glider Labs:
https://github.com/gliderlabs/docker-alpine/commit/ddc19dd95ceb3584ced58be0b8d7e9169d04c7a3#diff-db3dfdee92c17cf53a96578d4900cb5b

valiano
  • 16,433
  • 7
  • 64
  • 79
37

In case the container is already running:

docker exec -it container_id_or_name ash
Marcos
  • 786
  • 7
  • 8