Skip to content

Docker Hub, What is it, what is it for?

Docker Hub is a Docker artifact repository. Just as Maven has different platforms to manage and distribute Maven-based software, the same happens with Docker artifacts, there are platforms for the distribution of Docker artifacts.

Docker Hub is the global and public platform where we can publish our Docker artifacts.

Publish image on Docker Hub

For this example we are going to base ourselves on creating our own image based on an existing image.

1. Create a .Dockerfile file

We specify our image and if it is based on some existing image, in this case on busybox. And we add an execution entry point (CMD) for when the image is executed.

FROM busybox:latest
CMD ["date"]

2. Build the image locally

We specify the name of the image we want to create, in this case demotest_image

$ docker build -t demotest_image .
Sending build context to Docker daemon  75.26kB
Step 1/5 : FROM busybox:latest
 ---> beae173ccac6
Step 2/5 : LABEL version="1.0"
 ---> Running in 19629bb10c2d
Removing intermediate container 19629bb10c2d
 ---> 336971ddf3ee
Step 3/5 : LABEL description="Demo test Image"
 ---> Running in afca3593e1f6
Removing intermediate container afca3593e1f6
 ---> 99366c37d301
Step 4/5 : LABEL author="Daniel Peña Perez | danipenaperez@gmail.com"
 ---> Running in 55e68ab00a78
Removing intermediate container 55e68ab00a78
 ---> c392bd51fe76
Step 5/5 : CMD ["date"]
 ---> Running in 81ccf0032deb
Removing intermediate container 81ccf0032deb
 ---> 4844e8e78976
Successfully built 4844e8e78976
Successfully tagged demotest_image:latest

3. Check the created image

We can see the definition that has been created by doing an inspect of the image:

$ docker inspect demotest_image
[
    {
        "Id": "sha256:4844e8e78976134efb5323b4225048922903b4af7643c41b9630b292503a3d7d",
        "RepoTags": [
            "demotest_image:latest"
        ],
        "RepoDigests": [],
...
...

4. Execute the image

We use docker run to test the image execution. If we remember, the CMD printed a Date

$  docker run -it demotest_image
Wed May 22 12:20:30 UTC 2024

5. Publishing on DockerHub

First we log in with the docker client to the target artifactory, in this case DockerHub

$ 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: danipenaperez
Password: xxxxxx
WARNING! Your password will be stored unencrypted in /home/dpena/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

We tag the image locally, we are going to indicate that it is the image:latest and that it is under our namespace on dockerhub (in my case danipenaperez)

docker tag demotest_image:latest danipenaperez/demotest_image:latest

We push the image (the corresponding repository will be automatically created on dockerhub, that is, it is not necessary to create the repo on the web)

$ docker push danipenaperez/demotest_image:latest
The push refers to repository [docker.io/danipenaperez/demotest_image]
01fd6df81c8e: Mounted from library/busybox
latest: digest: sha256:6c8722ea0cb9d15fca0ea15012989f3a0aba66fb85186b5484a03c8473c4b45f size: 527

We check the image in our dockerHub account dockerhub_image_published.png

Verification. We delete the local image and try to execute it so we will see how the image falls from dockerHub:

docker images
REPOSITORY                                                                      TAG                   IMAGE ID       CREATED          SIZE
danipenaperez/demotest_image                                                    latest                4844e8e78976   13 minutes ago   1.24MB
We delete the image

$ docker image rm -f danipenaperez/demotest_image:latest
Untagged: danipenaperez/demotest_image:latest
Untagged: danipenaperez/demotest_image@sha256:79d1dd0936899f5312136b51a2f41470aa880c02fdb941dccff044b857baaf9f
Deleted: sha256:4844e8e78976134efb5323b4225048922903b4af7643c41b9630b292503a3d7d
Deleted: sha256:c392bd51fe76c55f478c430be484ffa1d94c739c46dd209da616a58390829190
Deleted: sha256:99366c37d30181f487be17b8fbd743df46d8b56d53f3eb581f4c9985fe933c52
Deleted: sha256:336971ddf3ee0e44d3953028c6434793d77dbd67af248dd7a24f6fe871496813

We perform the execution and as it does not exist locally, it will try to bring the image from dockerhub:

$ docker run -it danipenaperez/demotest_image
Unable to find image 'danipenaperez/demotest_image:latest' locally
latest: Pulling from danipenaperez/demotest_image
5cc84ad355aa: Already exists
Digest: sha256:79d1dd0936899f5312136b51a2f41470aa880c02fdb941dccff044b857baaf9f
Status: Downloaded newer image for danipenaperez/demotest_image:latest
Wed May 22 12:33:17 UTC 2024

Source Code

You have the source code of this demo at https://github.com/deadveloper666/dockerhub-tutorial