How To Build A Container Image And Push To DockerHub
In this blogpost i will walk you through the process of building a container image and publishing it to DockerHub.
What Is a Container?
Containers are isolated execution environments with an (operating system)OS-level virtualization, Isolated libraries & applications and fast to deploy & use fewer resources.
Containers have revolutionized the way we develop, deploy, and manage applications. Docker is the leading containerization platform, allows developers to package applications along with their dependencies into a standardized unit known as a container image.
DockerHub is a cloud-based registry that houses and makes your Image available/accessible from anywhere in the world. Examples of cloud-based registry is DockerHub registry, Azure container registry and Google container registry
CONTAINER USE CASES
a. Solving Development/production parity b. Migrating Infrastructure c. Microservice-Based Architecture d. Elastic Architectures e. Self-Healing Architecture f. CI/CD Pipelines
What Is An Image ?
A container Image is a standardized package containing all the necessary files, binaries, libraries and configurations to run a container. It is a self-contained bundle that encapsulates an application and its dependencies.
Containers virtualized the OS, not the hardware, making them more efficient and smaller than VMs, and allowing multiple applications to share the same OS kernel.
Containers are Isolated, lightweight and standalone
Examples of parent Images are Docker Image, Nginx, Redis etc
Prerequisites
Before we begin, make sure you have the following prerequisites:
Install docker on your Linux Terminal https://docs.docker.com/engine/install/ubuntu/
Signup on DockerHub and create an account https://hub.docker.com/
Let's Begin
Step 1: Run command sudo su
to enter root
- Input password NOTE: when inputting your password it won't be visible so type carefully and "Click' "Enter"
Step 2: Update your package using the command apt update
Step 3: Create A Directory - Using Command mkdir <name your directory
- Run command cd <name of directory>
to open up the directory
Step 4: Open Text File - vi Dockerfile
A Dockerfile is a text file that contains a set of instructions that Docker uses to build a container Image. It specifies the base image to use, any additional software packages to install, and any configuration settings to apply
Step 4a: Empty Text File
NOTE: To type or insert text into the designated space, click the letter "I" on your desktop
Step 4b: INPUT DOCKERFILE INSTRAUCTIONS 'FROM' is the bas image used for building a new image. The base image are available on DockerHub or any other image registry . FROM ubuntu
'MAINTAINER' Specifies the name and email address of the person or team responsible for maintaining the image . MAINTAINER <email>
'CMD' specifies the default command that should be executed when the container starts . CMD ["echo, "Hello World"]
Step 4c: SAVE AND EXIT - hit the desktop's "ESC" button
- Enter the command ":wq"
Step 5: Build Container Image - Run command docker build -t mydocker .
"docker build -t mydocker ." - means Build a docker Image named "my docker" from dockerfile in the current directory "techclass".
- 'docker build' command is what you run to build a Container
'-t' stands for tagname used with your DockerHub username, desired image name etc
- '.' refers to your present location
Step 6: Push Image To DockerHub
Log into DockerHub
On the Homepage "Click On" "Create a Repository"
Step 6a: Fill In Required Details - -- Give your repo a name "ladyintechrepo" - Give a short description of what your repo is about - Select Visibility "Public" -"Click On" the "Create"
Step 6b: Successfully Created A Repository
Step 7: Log Into DockerHub Remotely From Terminal - - Run command docker login --username=euniceakeh
- You will be prompted to enter your DockerHub password - We have successfully Logged in
Step 8: Push A New Tag To Your Repository docker tag <image name> <username/repo name>:version
Step 9: Inspect Your Docker Image docker inspect <Image name>
This is to inspect your image Repo Tags, name of Image, Version , Time & year created etc
Step 10: Push Image To Repo In DockerHub - Use command docker push <username/reponame>
as highlighted below
Step 11: Successfully Pushed To DockerHub
Conclusion
Building and pushing a Docker container image to DockerHub is a straightforward process that can greatly enhance the portability and deployment of your applications. By following this step-by-step guide, you can create and share container images efficiently, enabling consistent environments across development, testing, and production
Subscribe to my newsletter
Read articles from AKEH EUNICE directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by