Skip to main content

What is a Dockerfile?

Introduction

In this tutorial, we’ll cover the basics of Docker and how to create a simple Docker application using a Dockerfile. By the end, you’ll understand what a Dockerfile is, how to build a Docker image, and how to run it locally. This is an important steps before deploying your own docker images and running them in an edge node.

What is a Dockerfile?

A Dockerfile is a text file containing a set of instructions to assemble a Docker image. It specifies everything needed to build and run a container, such as:

  • The base image to use (e.g., Ubuntu, Alpine).
  • Application dependencies.
  • Environment configuration.
  • Commands to execute when the container runs.

Think of it as a blueprint for your Docker container.

Prerequisites

Before starting, ensure you have the following installed:

  • Docker: Download and install Docker for your operating system.
  • Text Editor: Any text editor like VS Code, Nano, or Notepad++.
  • Terminal/Command Line: Access to a terminal (Linux/macOS) or PowerShell/Command Prompt (Windows).

Videotutorial

See also...

Watch this tutorial on What is a Dockerfile.


Practice 01_01: "Hello World" Docker Application

GitHub

You will find this example in our GitHub: 01_01 Hello World Docker Application.

Step 1: Create a Working Directory

  1. Open your terminal and create a directory for your project:
mkdir 01_01_hello-world-docker
cd 01_01_hello-world-docker
  1. Inside this directory, crate a file named Dockerfile(no file extension):
touch Dockerfile

Step 2: Write Your Dockerfile

Open the Dockerfile in your text editor and add the following lines:

# Use an official lightweight Python image as the base
FROM python:3.9-slim

# Set the working directory inside the container
WORKDIR /app

# Copy the application code to the container
COPY hello.py /app

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

Explanation of Instructions:

  1. FROM python:3.9-slim: Specifies the base image (Python 3.9 slim version).
  2. WORKDIR /app: Sets the working directory inside the container.
  3. COPY hello.py /app: Copies your hello.py script into the container.
  4. CMD ["python", "hello.py"]: Defines the command to run when the container starts.

Step 3: Write the Application Code

Create a file named hello.py in the same directory with the following content:

print("Hello, Docker World!!")

Step 4: Build the Docker Image

  1. In the terminal, ensure you are in the hello-docker directory.
  2. Run the following command to build the image:
docker build -t hello-docker .
  • -t hello-docker: Tags the image with the name hello-docker.
  • .: Specifies the current directory as the build context.
  1. You should see Docker processing the instructions in the Dockerfile and building the image.
  2. Verify the image is created (look for the hello-dockerimage in the list):
docker images

Step 5: Run the Docker Container

  1. Run the container from the image:
docker run hello-docker
  1. You should see the output:
Hello, Docker World!

Step 6: Clean Up (Optional)

To remove the image and container when no longer needed:

  1. List all containers (even stopped ones):
docker ps -a
  1. Remove the container by its ID:
docker rm <container_id>
  1. Remove the image
docker rmi hello-docker

Summary

Congratulations! You have successfully created a Docker image using a Dockerfile, built the image locally, and ran a containerized application. This basic practice is a foundational step toward deploying Docker applications on edge nodes. From here, you can expand to more complex applications, adding networking, volumes, or multi-stage builds.

For edge deployments, ensure your Docker images are lightweight and optimized for resource-constrained environments.

Happy Dockerizing!