I create docker image for testing in my Jenkins pipeline, uploading this to Docker hub and deploy those to Kubernetes. At the end of the testing process, I want to delete the test image from Docker hub (not from test machine). How do I delete docker hub image from command line?
-
2Possible duplicate of How can I delete Docker's images?ipinak– ipinak2017-05-26 20:26:27 +00:00Commented May 26, 2017 at 20:26
-
15This is not a duplicate. I want to delete image from Docker hub using command line.codefx– codefx2017-05-27 12:08:11 +00:00Commented May 27, 2017 at 12:08
-
The original version didn't state that.ipinak– ipinak2017-05-30 09:03:04 +00:00Commented May 30, 2017 at 9:03
-
I have figured out how to do that . This is the final python script I came up with: github.com/appscode/libbuild/blob/master/docker.py#L31codefx– codefx2017-05-30 17:38:01 +00:00Commented May 30, 2017 at 17:38
-
4Wow, I can't believe there's no docker command line to delete an image from a registry.wisbucky– wisbucky2017-11-16 17:54:06 +00:00Commented Nov 16, 2017 at 17:54
6 Answers
You can delete any <TAG> from your Docker Hub <REPO> by using curl and REST API to the Docker Hub website (at https://hub.docker.com/v2/) rather that to the Docker Hub registry (at docker.io). So if you are not afraid of using an undocumented API, this currently works:
curl -i -X DELETE \
-H "Accept: application/json" \
-H "Authorization: JWT $HUB_TOKEN" \
https://hub.docker.com/v2/repositories/<HUB_USERNAME>/<REPO>/tags/<TAG>/
The HUB_TOKEN is a JSON Web Token passed using Authorization HTTP header, and it can be obtained by posting your credendials in JSON format to the /v2/users/login/ Docker Hub endpoint:
HUB_TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d "{\"username\": \"$HUB_USERNAME\", \"password\": \"$HUB_PASSWORD\"}" https://hub.docker.com/v2/users/login/ | jq -r .token)
2FA => Personal Access Token
Note than when you have 2FA enabled, you’ll need a personal access token (the only password accepted by the API when using 2FA).
4 Comments
403 Forbidden (not 401 Unauthorized) when I do this..."'${HUB_USERNAME}'" and "'${HUB_PASSWORD}'"? After changing this I was able to remove a tag.Use the Docker Hub API as documented in: https://docs.docker.com/v1.7/reference/api/docker-io_api/#delete-a-user-repository
I've just tested a delete of a test image with curl:
curl -X DELETE -u "$user:$pass" https://index.docker.io/v1/repositories/$namespace/$reponame/
Replace $user and $pass with your user and password on the Docker Hub, respectively; and replace $namespace (in my case it's the same as the $user) and $reponame with the image name (in my case was test).
3 Comments
Dockerhub has a REST backEnd, then you can use it... it is just skipping the FE...
For example:
export USERNAME=myuser
export PASSWORD=mypass
export ORGANIZATION=myorg # (if it's personal, then it's your username)
export REPOSITORY=myrepo
export TAG=latest
curl -u $USERNAME:$PASSWORD -X "DELETE" https://cloud.docker.com/v2/repositories/$ORGANIZATION/$REPOSITORY/tags/$TAG/
This will delete one tag...
In my case, I have microservices, then the REPOSITORY = the Microservice Name...
If I want to delete all the older images, I can iterate on this....
3 Comments
{"detail": "Invalid username/password"} as a response, though I've checked the username and password carefully.You can now use the new BETA (as of 2022-01) Docker Hub API
https://docs.docker.com/docker-hub/api/latest/
and the docker-hub CLI tool among a few other options.
hub-tool login
hub-tool tag rm myrepo/myimage:mytag
3 Comments
It is possible. For a shortcut, Open dev tools in Chrome, go to the network tab. Delete a tag manually from Docker Hub. You will see a request on the network tab in dev tools that goes to https://cloud.docker.com/v2/repositories//tags/. Just right click on that request, Copy, Copy as Curl. It should look something like this...
curl "https://cloud.docker.com/v2/repositories//tags//" -X DELETE -H 'Pragma: no-cache' -H 'Origin: https://cloud.docker.com' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.9' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36' -H 'Accept: application/json' -H 'Cache-Control: no-cache' -H 'Referer: https://cloud.docker.com/user/repository/registry-1.docker.io/reponame/tags' -H 'Cookie: ' --compressed
Comments
For any PowerShell friends.
$params = @{username='mickey';password='minnie'}
$response = Invoke-RestMethod -Uri https://hub.docker.com/v2/users/login/ -Method POST -Body $params
$token = $response.token;
$orgName = "mickey" #organization or user name
$repoName = "disney"
$Uri = $("https://hub.docker.com/v2/repositories/$orgName/$repoName/")
Invoke-WebRequest -Method Delete -Uri $Uri -Headers @{Authorization="JWT " + $token; Accept= 'application/json' }