Learn how to access shell and execute command inside Docker container. Explains running commands inside already running container or while launching container.
If you are following Docker series on my blog then you must have been gone through Docker basics and Docker container maintenance commands by now. In this tutorial, we will walk you though how to access shell inside Docker container and how to execute commands inside container.
First of all, you can not execute commands or access shells in any container. Basically, container image you are using to launch your container should have shell in it. If image does not support shell then you can not do anything inside container during launch or even after launch.
Read all docker or containerization related articles here from KernelTalk’s archives.
For example, if you are launching container from nginx image i.e. web-server container then you wont be able to access shell or execute command within it. Since its just a web-server process! But, if you are launching container from ubuntu image or alpine image then you will be able to access its shell since those images / software does support shell.
You can access shell inside docker container and execute commands inside container either of using two ways –
- Execute bash shell while launching container
- Use docker command to execute single command inside container
Remember, each Docker image has a default command defined in it which it executes whenever it launches any container. You can edit it anytime but if you want to change it on the fly then you need to specify it at the end of the
run command. So, image ignores default defined command and it executes command specified in
docker run command after it launches container.
Access shell & execute command in Docker container while launching it
Once you are confirmed that image you are using to launch container does support shell (mostly its bash) then you need to launch container using
-it switch. where –
-iis interactive mode.It keeps STDIN open even if you choose to detach container after launch
-tis to assign pesudo terminal through which STDIN is kept open for user input.
I launched ubuntu container with
-it switch and I presented with shell prompt within. Observe output below –
root@kerneltalks# docker container run -it ubuntu:latest
root@2493081de86f:/# ls -lrt
drwxr-xr-x. 2 root root 6 Apr 24 08:34 home
drwxr-xr-x. 2 root root 6 Apr 24 08:34 boot
drwxr-xr-x. 8 root root 96 Apr 26 21:16 lib
drwxr-xr-x. 10 root root 4096 Apr 26 21:16 usr
drwxr-xr-x. 2 root root 6 Apr 26 21:16 srv
drwxr-xr-x. 2 root root 6 Apr 26 21:16 opt
drwxr-xr-x. 2 root root 6 Apr 26 21:16 mnt
drwxr-xr-x. 2 root root 6 Apr 26 21:16 media
drwxr-xr-x. 2 root root 34 Apr 26 21:16 lib64
drwx------. 2 root root 37 Apr 26 21:17 root
drwxr-xr-x. 11 root root 4096 Apr 26 21:17 var
drwxr-xr-x. 2 root root 4096 Apr 26 21:17 bin
drwxrwxrwt. 2 root root 6 Apr 26 21:17 tmp
drwxr-xr-x. 2 root root 4096 Apr 27 23:28 sbin
drwxr-xr-x. 5 root root 58 Apr 27 23:28 run
dr-xr-xr-x. 13 root root 0 Jun 2 14:40 sys
drwxr-xr-x. 29 root root 4096 Jun 2 14:58 etc
dr-xr-xr-x. 114 root root 0 Jun 2 14:58 proc
drwxr-xr-x. 5 root root 360 Jun 2 14:58 dev
Sat Jun 2 15:00:17 UTC 2018
With the output, you can see after container is launched prompt is given
root@2493081de86f . Now you are within container with
root account. Keep in mind everything inside container happens with
root id. If you see hostname of Ubuntu container is set same as container ID. I executed couple of commands inside container in above output.
Keep in mind, since container is aimed to be very light weight they always consist of minimal software inside. So if you are running any Linux distribution container, you wont be able to run all commands as you would normally do in VM or Linux server.
Execute command inside already running container
Above process is applicable for container you are about to launch. But what if you want to execute command on container which is already running on system. Docker provided
exec switch to access running container shell. Syntax is
docker container exec <container name/ID> <command to run>
I have already ubuntu container running in my system. I used exec switch to execute hostname, date and
df commands inside container.
root@kerneltalks # docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ae0721fb8ecf ubuntu:latest "/bin/bash" 2 minutes ago Up 2 minutes loving_bohr
root@kerneltalks # docker container exec ae0721fb8ecf date
Sat Jun 2 15:41:24 UTC 2018
root@kerneltalks # docker container exec ae0721fb8ecf hostname
root@kerneltalks # docker container exec ae0721fb8ecf df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/docker-202:1-26198093-57ab60113158ca3f51c470fefb25a3fdf154a5309f05f254c660dba2a55dbab7 10474496 109072 10365424 2% /
tmpfs 65536 0 65536 0% /dev
tmpfs 507368 0 507368 0% /sys/fs/cgroup
/dev/xvda1 8376320 5326996 3049324 64% /etc/hosts
shm 65536 0 65536 0% /dev/shm
tmpfs 507368 0 507368 0% /proc/scsi
tmpfs 507368 0 507368 0% /sys/firmware
Observe about output and all 3 commands ran successfully inside container and shown output on our host machine terminal.