How to create a Docker image using Docker File and push it on Docker Hub.

Nipulpatel
5 min readJan 20, 2024

--

What is Docker ?

Docker is a containerization platform that provides easy way to containerize your applications, which means, using Docker you can build container images, run the images to create containers and also push these containers to container regestries such as DockerHub, Quay.io and so on.

In simple words, you can understand as containerization is a concept or technology and Docker Implements Containerization.

Docker LifeCycle

There are three important things,

  1. docker build -> builds docker images from Dockerfile
  2. docker run -> runs container from docker images
  3. docker push -> push the container image to public/private regestries to share the docker images.

Understanding the terminology

Docker daemon

The Docker daemon (dockerd) listens for Docker API requests and manages Docker objects such as images, containers, networks, and volumes. A daemon can also communicate with other daemons to manage Docker services. Docker Deamon is brain of Docker. If Docker Deamon is killed, stops working for some reasons, Docker is brain dead

Docker client

The Docker client (docker) is the primary way that many Docker users interact with Docker. When you use commands such as docker run, the client sends these commands to dockerd, which carries them out. The docker command uses the Docker API. The Docker client can communicate with more than one daemon.

Docker Desktop

Docker Desktop is an easy-to-install application for your Mac, Windows or Linux environment that enables you to build and share containerized applications and microservices. Docker Desktop includes the Docker daemon (dockerd), the Docker client (docker), Docker Compose, Docker Content Trust, Kubernetes, and Credential Helper. For more information, see Docker Desktop.

Docker registries

A Docker registry stores Docker images. Docker Hub is a public registry that anyone can use, and Docker is configured to look for images on Docker Hub by default. You can even run your own private registry.

When you use the docker pull or docker run commands, the required images are pulled from your configured registry. When you use the docker push command, your image is pushed to your configured registry. Docker objects

When you use Docker, you are creating and using images, containers, networks, volumes, plugins, and other objects. This section is a brief overview of some of those objects.

Dockerfile

Dockerfile is a file where you provide the steps to build your Docker Image.

Images

An image is a read-only template with instructions for creating a Docker container. Often, an image is based on another image, with some additional customization. For example, you may build an image which is based on the ubuntu image, but installs the Apache web server and your application, as well as the configuration details needed to make your application run.

You might create your own images or you might only use those created by others and published in a registry. To build your own image, you create a Dockerfile with a simple syntax for defining the steps needed to create the image and run it. Each instruction in a Dockerfile creates a layer in the image. When you change the Dockerfile and rebuild the image, only those layers which have changed are rebuilt. This is part of what makes images so lightweight, small, and fast, when compared to other virtualization technologies.

INSTALL DOCKER

create an Ubuntu EC2 Instance on AWS and run the below commands to install docker.

sudo apt update
sudo apt install docker.io -y

Start Docker and Grant Access

dont forget to Start the Docker daemon and grant acess to the user they want to use to interact with docker and run docker commands.Always ensure the docker daemon is up and running.

verify your Docker installation

docker run hello-world

If the output says:

This can mean two things,

  1. Docker deamon is not running.
  2. Your user does not have access to run docker commands.

Start Docker daemon

You use the below command to verify if the docker daemon is actually started and Active

sudo systemctl status docker

If you notice that the docker daemon is not running, you can start the daemon using the below command

sudo systemctl start docker

Grant Access to your user to run docker commands

To grant access to your user to run the docker command, you should add the user to the Docker Linux group. Docker group is create by default when docker is installed.

sudo usermod -aG docker ubuntu

NOTE: : You need to logout and login back for the changes to be reflected.

Use the same command again, to verify that docker is up and running.

docker run hello-world

Now create a Docker file by using the following command

vi dockerfile 

Now write the following content into the file

FROM ubuntu:latest

# Set the working directory in the image
WORKDIR /app

# Copy the files from the host file system to the image file system
COPY . /app

# Install the necessary packages
RUN apt-get update && apt-get install -y python3 python3-pip

# Set environment variables
ENV NAME World

# Run a command to start the application
CMD ["python3", "app.py"]

Login to Docker

docker login

Build your first Docker Image

You need to change the username accoringly in the below command

docker build -t nipul07/boon:v1 .

Output:

Verify Docker Image is created

docker images

Output

Push the Image to DockerHub and share it with the world

To push the image you need to first have an account on Docker Hub. If you have an account already you can skip this step.

docker push nipul07/boon:v1

Output

The docker image has been successfully uploaded to you Docker Hub account.

thank you !!!!

--

--

No responses yet