DEV Community

Cover image for How to create your first Docker image and push to Docker hub.
Ashutosh Sharma
Ashutosh Sharma

Posted on

How to create your first Docker image and push to Docker hub.

Prerequisite

  1. Docker hub account (https://hub.docker.com/)

  2. Any Terminal like bash or cmd

  3. Docker Installed: You can check if Docker is installed by running “docker — version”

    root@osboxes:~# docker --version
    Docker version 19.03.10, build 9424aeaee9

    1. Any Text Editor

Objective

In this example, we will be creating a docker image that will be using the base image as busybox.

BusyBox combines tiny versions of many common UNIX utilities into a single small executable. It is very small in size too (somewhere between 1 and 5 Mb in on-disk size (depending on the variant).

Steps

  1. Open the terminal and create a new folder named “Hello-World”.

    root@osboxes:~# mkdir hello-world

    1. Change the current directory to newly created “hello-world”

    root@osboxes:~# cd hello-world
    root@osboxes:~/hello-world#

    1. Create a new file name it “Dockerfile”. The contents of file are:

    FROM busybox
    CMD ["echo","Hello World from Docker"]

    1. Now we have to build this as an image, so we will run the **docker build **command.

    root@osboxes:~/hello-world# docker build -t hello-world:1.0 .
    Sending build context to Docker daemon 2.048kB
    Step 1/2 : FROM busybox
    ---> 1c35c4412082
    Step 2/2 : CMD ["echo","Hello World from Docker"]
    ---> Running in 87a79a673174
    Removing intermediate container 87a79a673174
    ---> 550111c1d0aa
    Successfully built 550111c1d0aa
    Successfully tagged hello-world:1.0

Success command means that our image is ready in local docker images.

  1. Check the image in the local docker repository docker images

    root@osboxes:~/hello-world# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    hello-world 1.0 550111c1d0aa About a minute ago 1.22MB

You will see the newly created image listed.

  1. Let’s run the image now using docker run

    root@osboxes:~/hello-world# docker run hello-world:1.0
    Hello World from Docker

  2. Our image is running and performing the task of printing. This is what we wanted to do. Now it’s time to push this to docker hub.
    Login to docker from CLI using docker login

    root@osboxes:~/hello-world# 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: ashusharma
    Password:
    WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
    Configure a credential helper to remove this warning. See
    https://docs.docker.com/engine/reference/commandline/login/#credentials-store

  3. Tag the local image with a name matching to your profile name with command docker tag hello-world:1.0 / hello-world:1.0

    root@osboxes:~/hello-world# docker tag hello-world:1.0 \
    ashusharma/hello-world:latest

  4. If you will check the local docker images again there will be two images available

    root@osboxes:~/hello-world# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    hello-world 1.0 550111c1d0aa 9 minutes ago 1.22MB
    ashusharma/hello-world 1.0 550111c1d0aa 9 minutes ago 1.22MB

  5. Let’s push the docker image now to docker hub using docker push / image:tag

    docker push ashusharma/ hello-world:1.0
    root@osboxes:~/hello-world# docker push ashusharma/hello-world:1.0
    The push refers to repository [docker.io/ashusharma/hello-world]
    1be74353c3d0: Mounted from ashusharma/busybox-hello-world
    1.0: digest: sha256:56dc0e710ab4cebfe5d75283b167c6839dae9429657e91798aa305662942098c size: 527

  6. Open the dockerhub page and log in. You will see the image available there.

Top comments (0)