A Beginner's Guide to Dockerfile: Start Creating Containers with Docker

Wed Nov 1, 2023


Docker has revolutionized the way we develop, package, and deploy applications. With Docker, you can encapsulate your application, its dependencies, and its runtime environment into a lightweight portable container.

Dockerfiles play a crucial role in this process. In this blog, we'll explore what a Dockerfile is, how to write one and go through the available instructions to create Docker containers.


Before diving into this comprehensive guide on Dockerfile, it's essential to ensure you have the following prerequisites:

  1. Basic Understanding of Docker: This blog assumes that you have a foundational knowledge of what Docker is and its core concepts.
  2. Docker Server: To create and build Dockerfile for creating Docker containers.
If you're new to Docker, it's recommended to begin with my earlier blog posts:
  1. Docker and Its Architecture: A Deep Dive
  2. How to Install Docker on Ubuntu 22.04 LTS in AWS Cloud
  3. Mastering Docker Commands: A Complete Hands-On Guide

What is a Dockerfile?

A Dockerfile is a text file that contains a set of instructions for building a Docker container. These instructions are used to define the base image, configure the environment, copy files, and run commands within the container. 

Dockerfiles are the blueprint for creating consistent, reproducible, and portable containers, making it easier to manage and scale applications.

Dockerfile Instructions

Dockerfile instructions are the set of commands and directives used in a Dockerfile to define how a Docker container image should be built. 

These instructions are used to specify the base image, set up the environment, copy files, install software, configure settings, expose ports, and define the startup commands for the container. 

Dockerfile instructions are executed in order when building a Docker image, ensuring that the resulting container is configured as intended.

let's explore some of the most commonly used Docker instructions:

  1. FROM: Specifies the base image for your container.
  2. WORKDIR: Sets the working directory for subsequent instructions.
  3. COPY: Copies files from the host into the container.
  4. ADD: Similar to COPY, but also allows for remote URLs and extraction of compressed files.
  5. RUN: Executes commands within the container during the image build process.
  6. EXPOSE: Informs Docker that the container will listen on the specified network ports.
  7. ENV: Sets environment variables for the container.
  8. ARG: Defines build-time arguments that can be overridden when building the image.
  9. CMD: Provides a default command to run when the container is started.
         Note: Only one CMD instruction is allowed in a Dockerfile.
  10. ENTRYPOINT: Configures the container to be an executable, allowing you to pass arguments when running the container.

How to Write a Dockerfile

Creating a Dockerfile is a straightforward process. Here are the basic steps to get you started:

  1. Choose a Base Image: Start by specifying a base image that serves as the foundation for your container. You can use official images from the Docker Hub or create your own.
  2. Set the Working Directory: Define the working directory within the container. This is where you will execute commands and place your application code
  3. Copy your application files: Copy your application files from your host machine to the container using the COPY instruction
  4. Install Dependencies: Use RUN to install any necessary software or packages within the container.
  5. Expose Ports: If your application requires network access, use the EXPOSE instruction to specify which ports to expose.
  6. Define Startup Command: Finally, specify the command to run when the container starts. Use the CMD instruction.

Hands-on Demo

Let us write a simple Dockerfile for a "Hello, World!" application using Python as an example.

1. Create a hello.py file with the following content

print("Hello, World!")
2. Create a Dockerfile with the following Docker instructions 

# Use an official Python runtime as the base image
FROM python:3.8-slim

# Set the working directory in the container

# Copy a simple Python script that prints "Hello, World!" to the container
COPY hello.py .

# Define the command to run the Python script
CMD ["python", "hello.py"]

Docker Command to Build a Docker Image from a Dockerfile

docker build -t my-image .
The above command will create a Docker image named "my-image" using the Dockerfile in the current directory.

After running the docker build command with the Dockerfile, you'll find that a Docker image named 'my-image' has been created.

Now, let's run a Docker container using the 'my-image' Docker image

docker run my-image

The demo is successfully completed, as evidenced by the Docker container producing the 'Hello, World!' output message on the terminal.


In this blog, we have explored what a Dockerfile is, how to write one and go through the available instructions to create Docker containers along with a hands-on demo.

I hope you enjoyed reading this blog and found it informative. If you have any questions or topics you'd like us to cover in future blogs, please don't hesitate to connect with me on LinkedIn. 

Thank you for joining us on this Docker journey.

Sampath Siva Kumar Boddeti
AWS & Terraform Certified.

Launch your GraphyLaunch your Graphy
100K+ creators trust Graphy to teach online
DevOpsHub 2023 Privacy policy Terms of use Contact us Refund policy