Dockerhub push images and delete with api

Sanjeev Rohila
4 min readMay 23, 2020

What you will learn -

  • Creating a docker image using a Dockerfile
  • Pushing image to Dockerhub
  • Creating containers using the image
  • Deleting the images from Dockerhub using api

First of all we need an account on dockerhub.com. Creating an account, its very simple, then you need to create a repository, in fact creating a repository is not required, when we push an image its create by default -

Download the Dockerfile, i have already created one for your ease

wget https://raw.githubusercontent.com/sanjeevrohila/docker-learning/master/dockerfiles/Dockerfile

Dockerhub login

$ sudo docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: justsanjeev
Password:
WARNING! Your password will be stored unencrypted in /home/ubuntu/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded

Create an image with the Dockerfile downloaded first, execute the command at same path as the Dockerfile is copied, i am creating an image nginx-oss with tag test

$ sudo docker build --tag nginx-oss:test .
Sending build context to Docker daemon 108.5kB
Step 1/6 : FROM debian:stretch-slim
---> fa41698012c7
Step 2/6 : LABEL maintainer="Sanjeev Rohila <justsanjeev@gmail.com>"
---> Using cache
---> 36d65d27754e
Step 3/6 : RUN set -x && apt-get update && apt-get install nginx -y
---> Using cache
---> a74fea1f36f9
Step 4/6 : EXPOSE 80
---> Using cache
---> 81179e935730
Step 5/6 : STOPSIGNAL SIGTERM
---> Using cache
---> a65cd1f8ef11
Step 6/6 : CMD ["nginx", "-g", "daemon off;"]
---> Using cache
---> 8b999250d611
Successfully built 8b999250d611
Successfully tagged nginx-oss:test

the output for you will be different when you will create it for the first time, as i already created it before and i am creating with a new tag so its fast.

Check the images, you will get the images on line 4 and 6 only, 6th is the debian image we have used to install the nginxopen source

$sudo docker images

Now add tag to your image, and check images again(line 3 is the image tagged just now)

$ sudo docker tag 8b999250d611 justsanjeev/nginx-oss:test
$ sudo docker images

The image created with tag test is available

Now push it

$ sudo docker push justsanjeev/nginx-oss:test
The push refers to repository [docker.io/justsanjeev/nginx-oss]
57aeb8471e4c: Layer already exists
83b43189420d: Layer already exists
test: digest: sha256:bd06696a874dbf9cb396fdf1c3b80d696af7270df958bf4df2d9bbf0cba19a4b size: 741

Check on Dockerhub, a public repo is created automatically with the name of your image, upon clicking repo it shows the images inside the repo with different tags

Delete the tags

$ sudo docker rmi nginx-oss:test
Untagged: nginx-oss:test
$
$ sudo docker rmi justsanjeev/nginx-oss:test
Untagged: justsanjeev/nginx-oss:1.0
Untagged: justsanjeev/nginx-oss@sha256:45ef1890acf65ab76d9780643346e882bee12297c4e68c2243096738a8a44649

Create a container using the image created above

Pull the image from dockerhub

$ sudo docker pull justsanjeev/nginx-oss:test
latest: Pulling from justsanjeev/nginx-oss
e62d08fa1eb1: Already exists
f91385bb97a8: Pull complete
Digest: sha256:bd06696a874dbf9cb396fdf1c3b80d696af7270df958bf4df2d9bbf0cba19a4b
Status: Downloaded newer image for justsanjeev/nginx-oss:test
$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
justsanjeev/nginx-oss test 8b999250d611 3 hours ago 129MB
debian stretch-slim fa41698012c7 8 days ago 55.3MB
ubuntu@dontdeletepoc:~$

Create a container

$ sudo docker run --name nginx-container --net bridge -d justsanjeev/nginx-oss:test
dc7c7aa8b3d07dccffe30335b3b08a05e5e9196306c576f7acfe751a86d40363
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dc7c7aa8b3d0 justsanjeev/nginx-oss:test "nginx -g 'daemon of…" 25 seconds ago Up 24 seconds 80/tcp nginx-container

Check the Ip address of the container

$ sudo docker inspect dc7c7aa8b3d0 | grep "IPAddress"
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",

Check if the Nginx is up and running, and per the below output its working :)

$ curl -X GET http://172.17.0.2 -I
HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Sat, 23 May 2020 11:37:20 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sat, 23 May 2020 08:37:54 GMT
Connection: keep-alive
ETag: "5ec8e0e2-264"
Accept-Ranges: bytes

Now lets look at the deletion of the images from Dockerhub

Authenticate

$ curl -s -H “Content-Type: application/json” -X POST -H “Content-Type: application/json” -d ‘{“username”:”your_username”, “password”:”your_password"}’ “https://hub.docker.com/v2/users/login/"{"token": "eyJ4NWMiOlsiTUlJQytUcCtnQXdJQkFnSUJBREFLQmdncWhrak9QUVFEQWpCR01VUXdRZ1lEVlFRREV6c3lWMDVaT2xWTFMxSTZSRTFFVWpwU1NVOUZPa3hITmtFNlExVllWRHBOUmxWTU9rWXpTRVU2TlZBeVZUcExTak5HT2tOQk5sazZTa2xFVVRBZUZ3MHlNREF4TURZeU1EVXhNVFJhRncweU1UQXhNalV5TURVeE1UUmFNRVl4UkRCQ0JnTlZCQU1UTzFWQlVUYzZXRk5PVXpwVVIxUXpPa0UwV2xVNlNUVkhTenBTTmpSWk9sZERTRU02VjFaU1N6cE5TVE5TT2t0WVRUYzZTRmRUTXpwQ1ZscGFNSUlCSWpBTkJna3Foa2lHOXcwQkFRRUZBQU9DQVE4QU1JSUJDZ0tDQVFFQXJ4eTJvbkgwU1h4eGtSQmRvcFgxVnNVbkFaXC85SllHck1JeWt6Um4xNGx3UDVKRWYrWE01QU5FbU1cLzBYOFJyNUlIN2VTWFV6K1lWaFVucVNKc3lPUlwvcXdwU3hXS1lMcVZwdW5RTlk4SGs3ZkZ5bzdJdG14MWo2dXZ0bVZhYmxRT0xGSTFCTVZ2NmNyMVY1d0ZWUWFnN0p4ZEVBUmVrWkdTOXg5SHJzNTVXcW9JUitvRkRsMVVUYzZQRUo2VVhncGJoV1h2aFNET2lwT3FJWHR2ZHRyaFhRaXdtOGNxMnMxdExEM2c4NlpkWFRYN1AxRWZMRThtYzBIeGpwRjZHYjVsR2RYY3Y1OXBcL1IzMVwva0xlXC9PcERzZ1VicTBBb3dwbHNZS29HZUpnVTQyWmRuOUhWRlFRUXBLRkxQTStaUDdEdmZlRjFjSFhYRm5SNU5KRVNWdWx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-1G_ou6Js5tlRr5iD3CFBp2-rBANdAn73MnTVNdPQgaTzErubtWVRk2x1R1CUFBY1lOkjaH7eCA2EWERtiLn_wdCgDdN6k4Gpg3EKT855jkkiiE9MXpZp41YHjWcOoXD_6ZFQ0u-59Rmt3OJijbmMUZw"}$ TOKEN=<token generated above>or$ TOKEN=`curl -s -H "Content-Type: application/json" -X POST -H "Content-Type: application/json" -d '{"username":"your_username", "password":"your_password"}' "https://hub.docker.com/v2/users/login/" | jq -r .token`

Get the tags present

$ curl “https://hub.docker.com/v2/repositories/justsanjeev/nginx-oss/tags" -X GET -H “Authorization: JWT ${TOKEN}”{“count”:1,”next”:null,”previous”:null,”results”:[{“creator”:517915,”id”:101720386,”image_id”:null,”images”:[{“architecture”:”amd64",”features”:””,”variant”:null,”digest”:”sha256:bd06696a874dbf9cb396fdf1c3b80d696af7270df958bf4df2d9bbf0cba19a4b”,”os”:”linux”,”os_features”:””,”os_version”:null,”size”:59101016}],”last_updated”:”2020–05–23T08:40:51.600672Z”,”last_updater”:517915,”last_updater_username”:”justsanjeev”,”name”:”test”,”repository”:9101107,”full_size”:59101016,”v2":true}]}

Delete

curl "https://hub.docker.com/v2/repositories/justsanjeev/nginx-oss/tags/test/" -X DELETE -H "Authorization: JWT ${TOKEN}"

--

--