codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐳

Docker

6 / 60 topics
6Dockerfile Basics7Multi-Stage Builds
Tutorials/Docker/Dockerfile Basics
🐳Docker

Dockerfile Basics

Updated 2026-05-15
10 min read

Dockerfile Basics

Introduction

In the previous section, we learned about what Docker is and how it can be used to containerize applications. Now, let's dive into creating a simple Dockerfile to build an image. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build, users can create an automated build that executes several command-line instructions in succession.

Concept

A Dockerfile consists of a series of instructions, each one creating a new layer in the image. The basic syntax for a Dockerfile is as follows:

  • FROM: Specifies the base image to use.
  • RUN: Executes any commands in a new layer on top of the current image and commits the results.
  • CMD: Provides defaults for executing containers.
  • EXPOSE: Informs Docker that the container listens on the specified network ports at runtime.
  • WORKDIR: Sets the working directory inside the container.

Examples

Let's create a simple Dockerfile to build an image that runs a basic "Hello, World!" application using Python.

Step 1: Create a Directory for Your Project

First, create a new directory for your project and navigate into it:

Terminal
mkdir hello-world-docker
cd hello-world-docker

Step 2: Create a Python Script

Create a simple Python script that prints "Hello, World!".

Python
1# app.py
2print("Hello, World!")

Step 3: Create a Dockerfile

Next, create a Dockerfile in the same directory. This file will contain instructions to build the Docker image.

dockerfile
1# Dockerfile
2FROM python:3.9-slim
3
4WORKDIR /app
5
6COPY app.py .
7
8CMD ["python", "app.py"]
  • FROM python:3.9-slim: This line specifies that we are using the official Python 3.9 slim image as our base.
  • WORKDIR /app: Sets the working directory inside the container to /app.
  • COPY app.py .: Copies the app.py file from your host machine into the /app directory in the container.
  • CMD ["python", "app.py"]: Specifies the command to run when a container is started, which in this case is running the Python script.

Step 4: Build the Docker Image

Now that you have your Dockerfile and application ready, you can build the Docker image. Run the following command:

Terminal

You should see the output:

Output
Hello, World!

What's Next?

In this tutorial, we learned how to create a simple Dockerfile and build an image. In the next section, we will explore Multi-Stage Builds, which allow you to optimize your images by reducing their size and improving security.

Stay tuned for more advanced topics in our Docker curriculum!


PreviousBasic Docker CommandsNext Multi-Stage Builds

Recommended Gear

Basic Docker CommandsMulti-Stage Builds