简体   繁体   中英

Create docker service with image from private registry through python library

I use the Docker SDK for Python to start a service on docker Swarm. The image I require is on a private registry. I would use the --with-registry-auth flag for the docker cli , but it seems it does not exist in the Python module. How can I achieve the behaviour of this flag through Python? Am I missing something?

I figured it out, and luckily it is quite easy. You simply need to login with the client object in use: After that it will automatically use the private registry if required:

import docker

# Create client and login
client = docker.DockerClient(base_url='unix://var/run/docker.sock')
client.login(
  username=USERNAME,
  password=PASSWORD,
  registry=REGISTRY_URL
)

# Define service
service_id = client.services.create(image=IMAGE_NAME, command=None, name='test')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM