docker tutorials

How to list the directories inside the docker container ?

This tutorial guides you on how to list the directories inside the docker container. Let’s use standard ubuntu container image for this exercise.

List directories inside the docker container

Before we learn how to list directories present inside the docker container, I would suggest to take a look about container’s Anatomy. Containers ultimately runs a processes. Docker provides many ways to run commands in containers. The following are the three basics ways to run containerized commands.

  1. Foreground Commands – Running commands in the foreground
  2. Interactive Commands – Running interactive commands in the foreground
  3. Background Commands – Running longer running commands in the background

Let’s use the standard ubuntu image as shown below for our example.

$ docker images

REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
ubuntu                             latest              adafef2e596e        3 days ago          73.9MB

Then run the following foreground command docker run ubuntu ls to list the directories inside the ubuntu container.

$ docker run ubuntu ls

bin
boot
dev
etc
home
lib
lib32
lib64
libx32
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

After, the above command is run, the container is stopped (Exited) as shown below since the process is ended. You can see the container around by using the following command docker ps -a even though it is not running.

$ docker ps -a

CONTAINER ID        IMAGE                   COMMAND                  CREATED              STATUS                          PORTS               NAMES
a7419d96a27f        ubuntu                  "ls"                     About a minute ago   Exited (0) About a minute ago                       sleepy_murdock

Since you don’t want to do anything with this container, because the process is ended/ finished, you can remove the container using the following command docker rm <container-id> as shown below.

$ docker rm a7419d96a27f

a7419d96a27f

You can also run the following docker run command with –rm docker run –rm ubuntu ls to automatically remove the container when it’s done.

$ docker run --rm ubuntu ls

bin
boot
dev
etc
home
lib
lib32
lib64
libx32
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

$ docker ps -a

CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS               NAMES

That’s all. Hope it helped 🙂

Also See:

References:

Leave a Reply

avatar
  Subscribe  
Notify of