Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login to registry with Docker Python SDK (docker-py)

I am trying to use the Docker Python API to login to a Docker cloud:

https://docker-py.readthedocs.io/en/stable/client.html#creating-a-client1

What is the URL? What is the Port?

I have tried to get it to work with cloud.docker.com, but I am fine with any registry server, so long as it is free to use and I can use it to upload Docker images from one computer and run them on another.

I have already got everything running using my own locally hosted registry, but I can’t seem to figure out how to connect to a server. It’s kind of ridiculous that hosting my own registry is easier than using an existing registry server.

My code looks like this, but I am unsure what the args.* parameters should be:

client = docker.DockerClient(base_url=args.docker_registry)
client.login(username=args.docker_user, password=args.docker_password)

I’m not sure what the base_url needs to be so that I can log in, and the error messages are not helpful at all.

Can you give me an example that works?

like image 281
Florian Dietz Avatar asked Mar 09 '23 08:03

Florian Dietz


2 Answers

The base_url parameter is the URL of the Docker server, not the Docker Registry.

Try something like:

from docker.errors import APIError, TLSParameterError

try:
   client = docker.from_env()
   client.login(username=args.docker_user, password=args.docker_password, registry=args.docker_registry)
except (APIError, TLSParameterError) as err:
   # ...
like image 152
Ricardo Branco Avatar answered Mar 10 '23 20:03

Ricardo Branco


Here's how I have logged in to Docker using Python:

import docker

client = docker.from_env()
client.login(username='USERNAME', password='PASSWORD', email='EMAIL',
                       registry='https://index.docker.io/v1/')

and here's what it returns:

{'IdentityToken': '', 'Status': 'Login Succeeded'}

So, that means it has been logged in successfully.

like image 33
Abdul Rehman Avatar answered Mar 10 '23 21:03

Abdul Rehman