TechyPilots Info
TechyPilots.INFO
Home/Tutorials/Docker/Create Your First Dockerfile
Dockerfile Tutorial

Create Your First Dockerfile

Learn how to package an application into a Docker image by creating your first Dockerfile. Understand the essential instructions, build your image, and run your application inside a container.

Level: Beginner
Topics: 10 Core Areas

Build Workflow

Dockerfile → Image → Container

📦
1Create Dockerfile
2Write Instructions
3Build Docker Image
4Run Container
5Test Application

Introduction

Turn your application into a Docker image

A Dockerfile is a text-based file containing instructions that Docker uses to build an image. These instructions can define the base image, working directory, files to copy, commands to run, and the default command for the container.

Once your Dockerfile is ready, you can use the docker build command to create an image. That image can then be used to create and run one or more containers.

Learning to write Dockerfiles is an important step toward containerizing applications and working with modern DevOps and cloud-native environments.

Your First Dockerfile

A simple example

This example demonstrates a basic Dockerfile structure for a Node.js application.

Dockerfile
FROM node:22-alpine

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["npm", "start"]

Why Learn Dockerfiles

Build applications consistently

Dockerfiles provide a repeatable way to define how your application environment is created.

Repeatable Builds

A Dockerfile provides a repeatable set of instructions for creating the same application image.

Portable Applications

Package your application and its required environment so it can run consistently across different systems.

Automation

Dockerfiles make application image creation easier to automate in development and CI/CD workflows.

Foundation for DevOps

Dockerfile skills are important for containers, CI/CD, Kubernetes, cloud-native applications, and DevOps.

Core Concepts

Learn Dockerfile step by step

These concepts will help you understand how Docker builds an image from a Dockerfile and runs it as a container.

01

What is a Dockerfile?

Learn how a Dockerfile defines the instructions Docker uses to automatically build a container image.

02

Choose a Base Image

Use the FROM instruction to select the base image your application will build upon.

03

Set the Working Directory

Use WORKDIR to define where future commands and application files will be handled inside the image.

04

Copy Application Files

Use COPY to add your application files and dependencies to the container image.

05

Install Dependencies

Use RUN to execute build-time commands such as installing application dependencies.

06

Define the Startup Command

Use CMD to define the default command that runs when a container starts.

07

Build Your Docker Image

Use docker build to create an image from the instructions in your Dockerfile.

08

Run Your Container

Use docker run to create and start a container from the image you built.

09

Understand Build Context

Learn why the directory used with docker build determines which files are available to COPY instructions.

10

Improve Your Dockerfile

Learn the basics of image layers, build caching, .dockerignore, smaller images, and better Dockerfile organization.

Practice Commands

Build and run your first image

Use these Docker commands after creating your Dockerfile.

docker build -t my-app .

Build a Docker image named my-app using the Dockerfile and current directory as the build context.

docker images

List the Docker images available on your local system.

docker run my-app

Create and start a container from the my-app image.

docker ps

Display currently running containers.

docker ps -a

Display all containers, including containers that have stopped.

docker run -p 3000:3000 my-app

Run the application and publish container port 3000 to port 3000 on the host.

Skills You Will Learn

Dockerfile fundamentals toolkit

These instructions and commands form the foundation for building Docker images.

Dockerfile
FROM
WORKDIR
COPY
RUN
CMD
EXPOSE
docker build
docker images
docker run
Build Context
.dockerignore

Learning Roadmap

Create your Dockerfile step by step

Follow this simple process to package and run your first containerized application.

Step 1

Create Your Application

Start with a simple application that you want to package into a Docker container.

Step 2

Create a Dockerfile

Create a file named Dockerfile in your project directory without a file extension.

Step 3

Choose a Base Image

Use the FROM instruction to select an appropriate base image for your application.

Step 4

Copy and Build

Copy application files into the image and run the commands required to install dependencies.

Step 5

Define the Startup Command

Use CMD to specify the default command that should run when the container starts.

Step 6

Build and Run

Build your Docker image and run it as a container to test your application.

What's Next?

Continue your Docker journey

After creating your first Dockerfile, continue learning about images, containers, storage, networking, and multi-container applications.

Start Building

Ready to create your first Dockerfile?

Create a simple Dockerfile, build your first image, and run your application inside a container.