Day 29: Hands-On with Docker – Build Your First Java Project Container from Scratch Using Dockerfile.

Today, we dive into one of the core concepts of Docker: understanding what a Dockerfile, Docker Image, and Container are, along with a real-life analogy (Maggi 😋) and practical demo to make things super easy to follow.
🔹 What is a Dockerfile, Image, and Container?
Let’s break it down:
Dockerfile: Think of it as a set of instructions written in a script format. It defines the environment, tools, dependencies, and everything needed to run your application.
(Like a recipe to cook something.)Docker Image: Once a Dockerfile is executed, it generates an image. This image is like a blueprint of your environment and application.
(Like the Maggi masala packet, already prepped and ready to use.)Docker Container: When you run a Docker image, you create a container — a live, running instance of the image.
(Like actually cooking the Maggi and serving it in a bowl 🍜.)
🔸 Real-Life Example: Let’s Understand with Maggi 🍲
Just like we cook Maggi:
We take a pot (this is our runtime environment).
Follow the recipe — first add water, then noodles, then masala, etc. (This is the Dockerfile.)
The complete cooked Maggi is like our Docker Image.
Now every time you use this masala and recipe, you can create multiple bowls of Maggi — which are the containers.
🔹 Practical Demo: Let's Get Started
1️⃣ Step 1: Login to Docker Hub
Before we can pull or push any Docker images, we need to log into Docker Hub.
bashCopyEditdocker login
🔐 Note: Sometimes the password doesn’t work. In that case, go to your Docker Hub account settings and create a personal access token to use instead of your password.

2️⃣ Step 2: Pull the hello-world Image
Let’s start with a very basic Docker image:
bashCopyEditdocker pull hello-world
Now check if the image was pulled successfully:
bashCopyEditdocker images

3️⃣ Step 3: Run the Image to Create a Container
Now run the pulled image:
bashCopyEditdocker run hello-world
This will create and run a container based on the hello-world image.
// Screenshot: Output of docker run hello-world
4️⃣ Step 4: Try a Server Image - MySQL
Let’s now pull a server-based image like MySQL:
bashCopyEditdocker pull mysql
Again, confirm it:
bashCopyEditdocker images
To run the MySQL image (which requires a root password), use:
bashCopyEditdocker run -d -e MYSQL_ROOT_PASSWORD=root mysql
Then check if the container is running:
bashCopyEditdocker ps
// Screenshot: Output of docker ps
🛠️ Now Let’s Create Our Own Dockerfile and Build a Container
We’ll now take a sample Java project from GitHub and create a Docker container for it.
5️⃣ Step 5: Setup Project Folder
First, create a new directory:
bashCopyEditmkdir java-project
cd java-project
Clone a Java project:
bashCopyEditgit clone <your-java-project-link>
// Screenshot: Project cloned from GitHub
6️⃣ Step 6: Create a Dockerfile
Inside the project folder, create a Dockerfile:
bashCopyEditvim Dockerfile
Example Dockerfile content:
DockerfileCopyEditFROM openjdk:17
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
RUN javac Main.java
CMD ["java", "Main"]
This Dockerfile sets up a basic Java environment and tells Docker how to run your project.
7️⃣ Step 7: Build the Docker Image
Now let’s build the Docker image using our Dockerfile:
bashCopyEditdocker build -t java-app .
// Screenshot: Docker image built
8️⃣ Step 8: Run the Container
Once the image is built, run it:
bashCopyEditdocker run java-app
This will execute your Java project inside a Docker container.
// Screenshot: Output of docker run java-app



