Skip to main content

Command Palette

Search for a command to run...

Creating My First Dockerfile

Updated
3 min read
Creating My First Dockerfile
P
👋 Hey there! Backend-focused developer building scalable systems with Node.js & FastAPI 🚀 Working with AWS, Docker, and modern backend architectures ☁️ Passionate about performance, system design, and continuous learning ⚔️💻 Sharing my journey of building real-world backend systems 💬

Until now, I was mostly pulling Docker images from Docker Hub and running them as containers.
That helped me understand how to use Docker.

But today’s goal was different:

I wanted to create my own Dockerfile and build my own Docker image.

To keep things simple and focused on learning Docker (not frameworks), I created a small Java application that prints the current date.

This blog explains:

  • Why I chose this project

  • How the Java app works

  • How a Dockerfile is written

  • How an image becomes a container


🔹 Why This Project?

Most beginners jump directly into:

  • Spring Boot

  • Big frameworks

  • Complex Dockerfiles

That hides the real learning.

So I intentionally chose:

  • Plain Java

  • No Maven / Gradle

  • One file

  • One responsibility

This helps me focus purely on Docker concepts.


🔹 Project Structure

java-docker-date/
│
├── index.java
├── Dockerfile
└── Readme.md

That’s it.
No extra files. No magic.


🔹 Java Application (index.java)

import java.time.LocalDate;

public class index {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        System.out.println("Today's date is: " + today);
    }
}

This program:

  • Uses standard Java

  • Prints the current date

  • Has zero external dependencies

Perfect for a first Docker image.


🔹 Writing the Dockerfile (Core Concept)

This is where real Docker learning happens.

# Use official OpenJDK image from Docker Hub
FROM eclipse-termurin:17-jdk-alpine

# Set working directory inside container
WORKDIR /app

# Copy Java source file into container
COPY index.java .

# Compile Java program during image build
RUN javac index.java

# Command to run when container starts
CMD ["java", "index"]

🔹 Understanding the Dockerfile Line by Line

  • FROM
    Pulls a base image (eclipse-termurin:17-jdk-slim) from Docker Hub
    This image already contains Linux + Java

  • WORKDIR
    Creates and switches to /app inside the container

  • COPY
    Copies only Main.java into the image
    Nothing else from my system is included

  • RUN
    Executes during image build
    Here, Java code is compiled once

  • CMD
    Runs when the container starts
    This is the default command

Important idea:

RUN = build time
CMD = run time


🔹 Building the Docker Image

From the project directory:

docker build -t java-date-app .

This command:

  • Reads the Dockerfile

  • Executes steps one by one

  • Creates a new Docker image

Check the image:

docker images

🔹 Running the Container

docker run java-date-app

Output:

Today's date is: 2025-01-05

This date is printed from inside the container, not from the host system.


🔹 What I Learned from This Small Project

  • Dockerfile is the recipe

  • Image is the built result

  • Container is the running instance

  • Base images come from Docker Hub

  • Containers are created and destroyed easily

Most importantly:

Docker is not about big apps.
It’s about consistency.


🔹 Why This Matters for Cloud & AWS

This same image can now be:

  • Pushed to Docker Hub

  • Pushed to AWS ECR

  • Pulled on EC2, ECS, or EKS

  • Run anywhere without changes

This is why Docker is the foundation of Cloud and DevOps.


🔹 Final Takeaway

Creating your own Dockerfile is the moment when Docker truly makes sense.

You stop being a Docker user
and start becoming a Docker engineer.