How do I delete a docker image from docker hub via command line?

Viewed 14583

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?

6 Answers

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....

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).

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' } 

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

Related