"docker container exec ... ls -l" - Files on Container

Q

How to list files on a Running Container using the "docker container exec ... ls -l" command?

✍: FYIcenter.com

A

If the running container is based on a Linux system, we can definitely run the "ls -l" command on the container using the "docker container exec ... ls -l" command as shown below.

1. Create a new container from the "openjdk" image and start it in interactive mode.

fyicenter$ docker container create --name java --tty --interactive openjdk

fyicenter$ docker container start --attach --interactive java

jshell> 1+2;
fyicenter$1 ==> 3

jshell>

2. Open another console and make sure that the container is running

fyicenter$ docker container list 
CONTAINER ID  IMAGE    COMMAND   CREATED        STATUS             NAMES
77b2863fe925  openjdk  "jshell"  2 minutes ago  Up About a minute  java

3. Run the "ls -l" command on the container:

fyicenter$ docker container exec java ls -l

total 52
lrwxrwxrwx   1 root root    7  bin -> usr/bin
dr-xr-xr-x   2 root root 4096  boot
drwxr-xr-x   5 root root  360  dev
drwxr-xr-x   1 root root 4096  etc
drwxr-xr-x   2 root root 4096  home
lrwxrwxrwx   1 root root    7  lib -> usr/lib
lrwxrwxrwx   1 root root    9  lib64 -> usr/lib64
drwxr-xr-x   2 root root 4096  media
drwxr-xr-x   2 root root 4096  mnt
drwxr-xr-x   2 root root 4096  opt
dr-xr-xr-x 225 root root    0  proc
dr-xr-x---   1 root root 4096  root
drwxr-xr-x   1 root root 4096  run
lrwxrwxrwx   1 root root    8  sbin -> usr/sbin
drwxr-xr-x   2 root root 4096  srv
dr-xr-xr-x  12 root root    0  sys
drwxrwxrwt   1 root root 4096  tmp
drwxr-xr-x   1 root root 4096  usr
drwxr-xr-x   1 root root 4096  var

4. Run the "java -version" command on the container:

fyicenter$ docker container exec java java -version
openjdk version "12.0.1" 2019-04-16
OpenJDK Runtime Environment (build 12.0.1+12)
OpenJDK 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing)

As you can see, we are able to run "ls -l" and "java -version" on the container, while the default command "jshell" is running.

So the container created from the "openjdk" image does support the Linux commands and JDK command.

 

"docker container exec --tty --interactive ... /bin/sh"

"docker container exec" - Execute Command on Container

"docker container ..." Commands

⇑⇑ Docker Container Platform - Tutorials

2021-10-10, 1665🔥, 0💬