Docker Hub

Github is probably familiar to you, so what is Docker Hub? Are the Hub in "Docker Hub" and the "hub" in "github" the same? Let's find out together

1. Docker Hub

Docker Hub is a service provided by Docker that allows searching and sharing container images. The main features of Docker Hub are:

  • Repositories: Push và pull container images.

  • Teams & Organizations : Manage access to private repositories of container images.

  • Official Images : Pull uses high quality Docker container images.

  • Publisher Images : Pull and use container images provided by other vendors.

  • Builds : Automatically create container images from GitHub and Bitbucket. Push them to Docker Hub.

  • Webhooks : Activate actions after successfully pushing a repository to Docker Hub with other services.

To use Docker Hub, sign up for an account here .

If your computer does not have Docker installed, you can install it by following the instructions here , or read this article.

If your computer has docker installed, open the terminal and run the command:

docker login

then enter username and password. If the result you get is

Login Succeeded

then that's ok. Now we can create docker images.

2. Create a Docker Image

First, we need to create a repository on Docker Hub, this will be the place to store our images.

To create a repository, log in to Docker Hub and visit the page: https://hub.docker.com/repository/create

For example, here I have created a repository called 2020-09-test

Now I will show you how to create an image locally and push it to the repository. Extremely simple.

First of all, we will create a Dockerfile

cat > Dockerfile <<EOF
FROM busybox
CMD echo "Hello world! This is 2020-09-test."
EOF

A Dockerfile file will be created like so

-rw-r--r--  1 user   68 Th09 11 14:29 Dockerfile

Let's try this build

$ docker build - < Dockerfile
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM busybox
latest: Pulling from library/busybox
df8698476c65: Pull complete 
Digest: sha256:d366a4665ab44f0648d7a00ae3fae139d55e32f9712c67accd604bb55df9d05a
Status: Downloaded newer image for busybox:latest
 ---> 6858809bf669
Step 2/2 : CMD echo "Hello world! This is 2020-09-test."
 ---> Running in 9df65757d429
Removing intermediate container 9df65757d429
 ---> 9736d7eb35b9
Successfully built 9736d7eb35b9

So we have created an image locally with id 9736d7eb35b9. Try running the image:

$ docker run 9736d7eb35b9
Hello world! This is 2020-09-test.

So our image is running with a CONTAINER ID of c2c4f119039a.

docker ps -a.
CONTAINER ID        IMAGE               COMMAND                   CREATED             STATUS                     PORTS               NAMES
c2c4f119039a        9736d7eb35b9        "/bin/sh -c 'echo \"H…"   2 minutes ago       Exited (0) 2 minutes ago                       epic_dhawan

Now let's commit and push the image to Docker Hub

$ docker commit c2c4f119039a latest
$ docker tag latest:latest haihachan/2020-09-test
$ docker push haihachan/2020-09-test:latest

As a result, after the push is completed, we have an image with the tagname latest

Now download it and try it out

$ docker pull haihachan/2020-09-test:latest 
latest: Pulling from haihachan/2020-09-test
Digest: sha256:4a6173ad3154afd5eeb54916173acbb8d1dad04296a21d7d44f5f4b077c60f12
Status: Image is up to date for haihachan/2020-09-test:latest
$ docker images
REPOSITORY                            TAG                 IMAGE ID            CREATED             SIZE
haihachan/2020-09-test                latest              721106855c17        20 minutes ago      1.23MB

It's delicious, let's try running the image:

$ docker run haihachan/2020-09-test
Hello world! This is 2020-09-test.

It seems simple. But to learn more deeply, please find out here . Docker Hub's documentation is really misleading to users

3. Create a Docker Image with the Github repo

In addition to pushing as in section 2, we can also connect Docker Hub's repository to Github. Then, instead of pushing the image to Docker Hub, we can push the code to github, and build the image on the Docker Hub interface.

First, you need to connect your Github account on Docker Hub here: https://hub.docker.com/settings/linked-accounts

Once connected, go to the repository and select the Builds tag:

After clicking on the Github icon, select the repo you want to save the source code for your image:

Click Save and Build, so your source code on Github will be built into an image. Too easy, right?

Last updated