10

I'm trying to pull docker images from a private repository hosted in Docker hub https://registry.hub.docker.com/u/myname/myapp like this using the docker remote API. The doc is not clear as to how to specify the authentication credentials in a POST request like this

curl -XPOST -H "X-Registy-Auth: base64_encoded_authconfig_object" "http://localhost:4243/images/create?fromImage=myname/myapp"

This also does not elaborate on how exactly the authconfig is generated.

This talks about sending in a base 64 encoded json with a structure like this:

{
  "index_url": {
    "username": "string",
    "password": "string",
    "email": "string",
    "serveraddress": "string"
  }
}

But doesnt explain what is index_url and serveraddress. Are they

index_url = https://registry.hub.docker.com/u/myname/myapp
serveraddress = https://registry.hub.docker.com

The above configurations give me 404, probably the registry hub private repo is not being recognized.

I also tried base 64 encoding the contents of my ~/.dockercfg

{
  "https://index.docker.io/v1/": {
    "auth":"xxxxxxxxxxxxxxxxxxx==",
    "email":"myname@myemail.com"
  }
}

Could you tell me how to generate the base64 encoded authconfig object and get the above curl command working.

Thanks in advance

Docker version

Client version: 0.11.1
Client API version: 1.11
Go version (client): go1.2.1
Git commit (client): fb99f99
Server version: 0.11.1
Server API version: 1.11
Git commit (server): fb99f99
Go version (server): go1.2.1
mbarthelemy
  • 12,465
  • 4
  • 41
  • 43
alpha_cod
  • 1,933
  • 5
  • 25
  • 43
  • Hi @alpha_cod, did you pull images from private registry sucessfully using docker Remote API? – lephix Mar 28 '17 at 04:23
  • If anyone lands here because they wanted to use `docker login` credentials for pulling via the API, I've made an issue: [#40397](https://github.com/moby/moby/issues/40397) – Martti Laine Jan 21 '20 at 11:58

2 Answers2

12

I had the same issue.

Here's the "raw" AuthConfig object that you should use to pass the credentials:

{
  "username":"your_registry_username_or_email",
  "password":"*****",
  "auth":"",    // leave empty
  "email":"your@email.tld"
}

You then have to "encode" it using Base64.

You didn't tell what language you're using, but if needed, this awesome site will let you encode your object in one click. Or, from a shell:

echo '{"username":"username","password":"*****", "auth":"","email":"your@email.tld"}' | base64


Then, just pass the encoded value to the header:

X-Registry-Auth: eyJ1c2VybmFtZSI6InlvdXJfcmVnaXN0cnlfdXNlcm5hbWVfb3JfZW1haWwiLCJwYXNzd29yZCI6IioqKioqIiwiYXV0aCI6IiIsImVtYWlsIjoieW91ckBlbWFpbC50bGQifQ==

Here's a working example using curl and

  • a registry available at r.getitlive.io
  • a docker daemon listening at '192.168.60.10:8888' :
curl -X POST  -d ""  \
  -H "X-Registry-Auth: eyJ1c2VybmFtZSI6InlvdXJfcmVnaXN0cnlfdXNlcm5hbWVfb3JfZW1haWwiLCJwYXNzd29yZCI6IioqKioqIiwiYXV0aCI6IiIsImVtYWlsIjoieW91ckBlbWFpbC50bGQifQ==" \
  'http://192.168.60.11:8888/images/create?fromImage=r.getitlive.io/cool/repo&tag=latest'

Note : I couldn't make it work (yet) by putting the remote registry endpoint/URL in the serveraddress field of the AuthConfig object. That's why I'm adding the registry host to the fromImage=parameter.

mbarthelemy
  • 12,465
  • 4
  • 41
  • 43
  • Thank you for your help. I tried the above curl command. It says invalid fromImage value (Invalid namespace name (u), only [a-z0-9_] are allowed, size between 4 and 30) when I used https://registry.hub.docker.com/u/myname/myapp or registry.hub.docker.com/myname/myapp. From image does not seem to accept any URL but something of the form myname/myapp, in which case it says 404 as this repository is not public – alpha_cod Jul 18 '14 at 18:11
  • Mmm... Try removing completely `registry.hub.docker.com` since it's the default. Instead use `?fromImage=myname/myapp`. – mbarthelemy Jul 18 '14 at 18:19
  • That gives a 404, as the implicit mention of Dockerhub registry is not recognized – alpha_cod Jul 18 '14 at 18:21
  • `fromImage` does accept a registry host, but only a custom one. For some reason it refuses the defaults ('index.docker.io' and I guess 'registry.hub.docker.com' too). If you use the default registry you have to remove the host part completly. – mbarthelemy Jul 18 '14 at 18:27
  • Oh..., these are the results for different combinations curl -X POST -d "" -H "X-Registry-Auth: xxxx==" 'http://localhost:4243/images/create?fromImage=registry.hub.docker.com/myname/myapp&tag=latest' Error: {"status":"Pulling repository registry.hub.docker.com/myname/myapp"} {"errorDetail":{"message":"HTTP code: 404"},"error":"HTTP code: 404"} – alpha_cod Jul 18 '14 at 18:34
  • curl -X POST -d "" -H "X-Registry-Auth: xxxx==" 'http://localhost:4243/images/create?fromImage=registry.hub.docker.com/u/myname/myapp&tag=latest' Error: Invalid namespace name (u), only [a-z0-9_] are allowed, size between 4 and 30 – alpha_cod Jul 18 '14 at 18:35
  • curl -X POST -d "" -H "X-Registry-Auth: xxxx==" 'http://localhost:4243/images/create?fromImage=index.docker.io/myname/myapp&tag=latest' Invalid repository name, try "myname/myapp" instead – alpha_cod Jul 18 '14 at 18:36
  • If I remove the host part completely, it gives me a 404 – alpha_cod Jul 18 '14 at 18:38
  • Just an idea regarding bugs that may have affected Docker in past versions : could you upgrade to the latest release (1.1.1)? – mbarthelemy Jul 19 '14 at 12:27
  • Yup, upgraded, but still the same response – alpha_cod Jul 22 '14 at 00:37
  • Some important tips: The current docker docs use single quotes but this will fail since double quotes are the only accepted format. Also, the empty auth key must be set or it can fail. – David Pelaez Sep 13 '14 at 22:51
  • 1
    Please do not encourage people to encode the passwords in a remote site!!!! the answer should be edited to remove that link – Mauricio Oct 30 '18 at 18:12
3

From this merged docker pull request, it seems that X-Registry-Auth header should be a base-64 encoded json string of the form

{
  'username': string,
  'password': string,
  'email': string,
  'serverddress' : string
}

another reference link

David Xia
  • 5,075
  • 7
  • 35
  • 52