How to Build a Container Image Running on Docker Hub.

In this blog post, I will guide you through the process of building a container image that runs on Docker Hub.

Additionally, I will explain what containers are, allowing you to establish a strong foundation in understanding how container images work.

By the end of this post, you will have a practical understanding of containerization and be able to build your container images.

So, let's dive in and explore the world of container images

What is a Container?

In Docker, a container is a lightweight, standalone, and executable package of software that includes everything needed to run an application, like code, dependencies, and libraries, without relying on the host machine's environment.

Here's a more detailed explanation:

  • What it is:

    A container is a runnable instance of a Docker image, which is a read-only template containing the application's code and dependencies.

  • How it works:

    Docker containers share the host machine's operating system kernel, making them lightweight and efficient.

  • Why use it:

    Containers isolate applications and their dependencies, ensuring they run consistently across different environments, eliminating "it works on my machine" problems and facilitating faster software delivery.

  • Benefits:

    • Portability: Containers can run on any system that supports the container runtime, regardless of the underlying operating system.

    • Isolation: Each container runs in its isolated environment, preventing application conflicts and ensuring a secure environment.

    • Resource efficiency: Containers share the host's kernel, requiring fewer resources than virtual machines.

What is an Image?

An image can be defined as a software package, for instance, an operating system. These Images are stored in what we call an image repository.

To get a machine image into our environment, we use a "PULL" operation. For instance, if we want to download redis. We input the "command docker pull redis" To start an image, we use the "run" command.

For more information on how to install the Docker engine in your Linux terminal, see the steps here. To check if Docker has been successfully installed, you can check with the command "docker -v"

Let's Get Started

Prerequisite: Ensure you have installed Docker on your Linux terminal.

a) To begin, enter into root user with the command "sudo su" and press Enter

b) Thereafter, you are prompted to input your password, which doesn't reflect when you type it.

c) Having inputted your password, press Enter.

d) Input the "apt update" command on your system to have the latest information about available software updates before performing any package installations or upgrades.

e) The next step is to create your directory with the command "mkdir", leave a space, and give your directory a name.

f) To open the directory, input the command "cd", leave a space, and type the directory name. Press Enter.

g) To create our text file use the command "vi" for default or install "vim" for the latest version. Leave a space and type in the default text file.

h) Once you click on enter, the text file opens as shown in the capture below.

i) Click on the letter "I" on your desktop to enable you to type or insert your text in the space.

j) To build our container image, the first command is "FROM". It specifies the base image that will be used as the starting point for building our custom Docker image. Then we input the following commands:

i) FROM ubuntu: The "FROM" command and the Ubuntu base image name will be used to build our Docker image. This means our Docker image is housed in the Parent image, called "ubuntu".

ii) LABEL maintainer=umeokolivincent@gmail.com: The "LABEL" command is an instruction used to add metadata to a Docker image in the form of key-value pairs.

iii) CMD [ "echo", "Welcome"]: The "CMD" command is used to give an instruction. In this demo, we are telling Docker that when the container image starts, it should show the message "Hello World" by default.

iv) WORKDIR /app: The WORKDIR command sets the working directory (also called the "current directory") for any subsequent instructions like RUN, CMD, COPY, ADD, or ENTRYPOINT. It functions similarly to cd in Linux but persists for the rest of the Dockerfile instructions.

v) COPY . /app: The COPY command in a Dockerfile is used to copy files or directories from the host machine (your local system) into the Docker image during the build process. It is one of the most frequently used instructions for adding application code, configuration files, or dependencies into the container.

k) To exit the text space,

i) Press the ESC button on your desktop

ii) Press the space button on your desktop

iii) Input the :wq command. This command is used within the Vim text editor to save changes to the file and exit the editor.

L) Having inputted the :wq command above, the text file page will close. You will be returned to your directory as shown below.

m) The next step is to build our container image.

n) Input the command docker build -t utomimg . Once you press Enter, your container image starts to build.

o) Once it has finished building, you will see the "FINISHED" command.

p) Type in docker images command to display all the images created, including the recently created image “utomimg“. You can also use the ps or ps -a commands to view the running images or all images.

q) The next step is to log into Docker Hub and create a repository so that we can be able to push our image. Take note of the docker push, Docker Hub name, and the repository name.

r) We then log in to Docker Hub from the terminal, typing the following command: docker login —username=<name of the Docker Hub account>. It would prompt you for your Docker Hub password, input it and hit Enter, then you will be logged into the Docker Hub remotely.

s) We then put a new tag to our repository by running the command: docker tag <name of image> <name of your repository>:latest, hit Enter.

t) We then run docker inspect to check our image.

u) We push by running docker push <name of the repository>.

We go to the Docker Hub to check if the image has been pushed to the repository, as seen below.

v) We then use the image created to create a container. type docker run -d —name <name of container> -p 8080:80 <name of image>.

w) We then run docker ps to see the status of our container and image.

0
Subscribe to my newsletter

Read articles from umeokoli vincent tochukwu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

umeokoli vincent tochukwu
umeokoli vincent tochukwu