0

I trying to automate a docker server using docker-py. Need to check whether the host URL is pinging or not using python. Hence I need an equivalent command in python for docker port container.
docker port container_id

import docker
client = docker.from_env()
print(client.port('c03ebfb53a7d', 80))

1 Answers1

0

When instantiating DockerClient object whether through docker.DockerClient(base_url='unix://var/run/docker.sock') or docker.from_env(), inside constructor APIClient object is instantiated:

def __init__(self, *args, **kwargs):
    self.api = APIClient(*args, **kwargs)

APIClient itself is inheriting a bunch of classes:

class APIClient(
    requests.Session,
    BuildApiMixin,
    ConfigApiMixin,
    ContainerApiMixin,
    DaemonApiMixin,
    ExecApiMixin,
    ImageApiMixin,
    NetworkApiMixin,
    PluginApiMixin,
    SecretApiMixin,
    ServiceApiMixin,
    SwarmApiMixin,
    VolumeApiMixin)

One of the classes it inherits is ContainerApiMixin that exposes methods for interacting with containers, similar to docker container CLI.

As you can see, everything you can do through CLI is accessible through api object inside DockerClient object.

So, the answer to your question is:

client.api.port('<container_id>', <port>)

Resource: source code

Stefan Golubović
  • 1,225
  • 1
  • 16
  • 21
  • In addition to this, I need to verify whether the URL is reachable or not. I –  Mar 31 '20 at 15:54
  • Do you mean something like [this](https://stackoverflow.com/a/1949360/4778343)? Or something different? – Stefan Golubović Mar 31 '20 at 15:59
  • `host=cli.port(id, 80) port=host[0]['HostPort'] ip=host[0]['HostIp'] server_ip=ip+':'+port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) rep = os.system('ping ' + server_ip) if rep == 0: print ('server is up') else: print ('server is down')` –  Mar 31 '20 at 16:00
  • I need to check whether it is reachable or not That is my ultimate purpose –  Mar 31 '20 at 16:03
  • Code is not showing any syntax error. But it always show the server error –  Mar 31 '20 at 16:08
  • Even I tried ping '0.0.0.0:8060' it also outcome the same in Linux terminal –  Mar 31 '20 at 16:09
  • `ping` does not work with ports. Use netcat instead: `os.system('nc -z ' + ip + ' ' + port)`. – Stefan Golubović Mar 31 '20 at 16:32
  • how to check whether the space utilization is proper in servers using python (need to automate using docker-py) –  Apr 01 '20 at 06:20
  • Can you explain what do you need? – Stefan Golubović Apr 01 '20 at 09:11
  • I need to verify whether the mounting space of containers is proper or not. If the space utilization is wrong then I will execute `docker system prune` –  Apr 01 '20 at 10:10
  • Need to implement this using python docker-py –  Apr 01 '20 at 10:12
  • But what do you mean by "_mounting space of containers is proper or not_"? – Stefan Golubović Apr 01 '20 at 13:10
  • I need to check whether the space utilization of volumes mounted for the container is proper or not? –  Apr 01 '20 at 14:15
  • Or could you help me to know how to check the API testing in docker –  Apr 02 '20 at 04:55
  • It seems to be that all that depends on type of volume. Named volumes are stored under `/var/lib/docker` so it can grow up to total amount of free memory on the machine. Bind mounts are mounting points on the host, so they can grow up to total amount of free memory on the host. And finally there is `tmpfs` which is mounted in RAM memory and it can grow up to total amount of free RAM memory. I'm not sure how to inspect type of volume using Docker CLI, so you have to do a research yourself or ask another question on SO. – Stefan Golubović Apr 02 '20 at 12:01