Skip to content
Advertisement

Return the ls -l output in docker container

So I have a docker container running which has a couple of folder/files inside.

When I go inside the container I can see that they are:

hostmachine$> docker exec -it testContainer bash

testContainer@testContainer:~$ ls -l

total 40200
-rw-r--r-- 1 root root     41156267 Apr 11 20:10 herp-4.2.2-SNAPSHOT-ap3.tar.gz
drwxr-xr-x 2 root root     4096 Apr 11 20:10 var
drwxr-xr-x 5 root root     4096 Apr 11 20:10 herp-4.2.2-SNAPSHOT
lrwxrwxrwx 1 root root     39 Apr 11 20:10 current -> /home/testContainer/herp-4.2.2-SNAPSHOT

My question is:

What script, how can I use it on my hostmachine that will:

1) Echo back (return) the following info from the container:

current -> /home/testContainer/herp-4.2.2-SNAPSHOT

2) And then taking it further, just echo back the last bit:

herp-4.2.2-SNAPSHOT

So essentially it could be like:

hostmachine$> ./getSnapshotName.sh

herp-4.2.2-SNAPSHOT

Advertisement

Answer

Just build it up in steps. First, no need to use -it and bash to run an interactive shell, just run the command directly:

$ docker exec testContainer ls -l current
lrwxrwxrwx 1 root root     39 Apr 11 20:10 current -> /home/testContainer/herp-4.2.2-SNAPSHOT

Next, use readlink to save you from parsing the ls output:

$ docker exec testContainer readlink current
/home/testContainer/herp-4.2.2-SNAPSHOT

Finally, run that through basename to strip out the directory parts:

$ basename $(docker exec testContainer readlink current)
herp-4.2.2-SNAPSHOT
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement