0

I'm trying to do automation. For that, I need to check whether the children of the source directory in host system are equal to children directory in the destination directory of the container.

docker inspect -f '{{ .Mounts }}' devtest 
[{bind  /home/usr/location/docker/myapp/target /app   true rprivate}]

From the above code /home/usr/location/docker/myapp/target is the source from the host and similarly the /app is the destination where the mount is binded.

So as per my requirement I need to find the count of sub directory in both the source and destination. In the case of source is calculated using os.walk. But I have no idea about how to find it from the container using python.

I'm using docker-py module as utility for docker. May I know how can I achieve this using python

Edward Arrow
  • 215
  • 2
  • 11

2 Answers2

1

You can use docker exec on the docker host machine and then use the Linux ls command along with wc to find the directory count.

You can use either the os or subprocess python modules to run the command. (subprocess is the pythons recommended option).

import os

os.system("docker exec <container-id/name> ls /app |wc -l")

You can use -d option in the command, if you want to run it as a daemon.

Tigran
  • 537
  • 1
  • 4
  • 20
dvs
  • 491
  • 1
  • 8
  • 26
  • What if I need only the subdirectories? – Edward Arrow May 07 '20 at 14:09
  • This should help to list the sub directories. https://stackoverflow.com/questions/5168071/list-sub-directories-with-ls – dvs May 07 '20 at 14:30
  • os.system not working but typing the same thing into the command prompt works – Edward Arrow May 07 '20 at 14:38
  • You should be able to get the results into a variable (result = os.system () ) and use that, could you provide any error messages, which is generated..? – dvs May 07 '20 at 14:40
  • Can we use subprocess to acheive these – Edward Arrow May 07 '20 at 14:55
  • Yes, this should help. https://stackoverflow.com/questions/89228/calling-an-external-command-from-python – dvs May 07 '20 at 15:03
  • I tried print(subprocess.run(["docker" ,"exec", "-d", "devtest", "ls", "/app/.", "|", "wc", "-l"])) but it output as CompletedProcess(args=['docker', 'exec', '-d', 'devtest', 'ls', '/app/.', '|', 'wc', '-l'], returncode=0) – Edward Arrow May 07 '20 at 15:07
  • remove the -d and try – dvs May 07 '20 at 15:14
  • args = split("docker exec devtest ls /app/. | wc -l") mnt_out = subprocess.run(args=args) it output as below `/app/.: total 16 drwxr-xr-x 3 1000 1000 4096 May 7 14:16 Children1 drwxr-xr-x 2 1000 1000 4096 May 7 09:38 Children2 drwxr-xr-x 2 1000 1000 4096 May 7 14:56 Children3 -rw-r--r-- 1 1000 1000 650 May 7 13:24 question ls: cannot access '|': No such file or directory ls: cannot access 'wc': No such file or directory None ` But same thing work on terminal – Edward Arrow May 07 '20 at 15:21
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/213343/discussion-between-dvs-and-edward-arrow). – dvs May 07 '20 at 15:27
0

You can run commands with subprocess and shlex

import subprocess
from shlex import split

args = split("docker inspect -f '{{ .Mounts }}' devtest")
mnt_out = subprocess.run(args=args, universal_newlines=True, stdout=subprocess.PIPE)

print(mnt_out.stdout)

This will print as if it was on terminal. From there you can build the logic you wish to archive with pure python using pathlib library for example.

Prayson W. Daniel
  • 9,790
  • 2
  • 35
  • 43