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
Watch this tutorial on What is a Dockerfile.
Practice 01_01: "Hello World" Docker Application
You will find this example in our GitHub: 01_01 Hello World Docker Application.
Step 1: Create a Working Directory
- Open your terminal and create a directory for your project:
mkdir 01_01_hello-world-docker
cd 01_01_hello-world-docker
- 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:
FROM python:3.9-slim: Specifies the base image (Python 3.9 slim version).WORKDIR /app: Sets the working directory inside the container.COPY hello.py /app: Copies your hello.py script into the container.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
- In the terminal, ensure you are in the
hello-dockerdirectory. - Run the following command to build the image:
docker build -t hello-docker .
-t hello-docker: Tags the image with the namehello-docker..: Specifies the current directory as the build context.
- You should see Docker processing the instructions in the Dockerfile and building the image.
- Verify the image is created (look for the
hello-dockerimage in the list):
docker images
Step 5: Run the Docker Container
- Run the container from the image:
docker run hello-docker
- You should see the output:
Hello, Docker World!
Step 6: Clean Up (Optional)
To remove the image and container when no longer needed:
- List all containers (even stopped ones):
docker ps -a
- Remove the container by its ID:
docker rm <container_id>
- 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!