Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

docker

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?

like image 536
codefx Avatar asked May 26 '17 20:05

codefx


3 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)
echo $HUB_TOKEN
like image 181
mirekphd Avatar answered Oct 02 '22 01:10

mirekphd


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

like image 43
Marco Vargas Avatar answered Oct 02 '22 01:10

Marco Vargas


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

like image 28
Ricardo Branco Avatar answered Oct 02 '22 00:10

Ricardo Branco