- Starting with an operating system (e.g., Ubuntu)
- Updating package repositories via APT
- Installing required dependencies using APT
- Installing Python packages with pip
- Copying your application’s source code to a target directory (for example, /opt)
- Launching the Flask web server

When designing your Dockerfile, remember that each command represents a layer. Combining commands (using &&) minimizes the number of layers and reduces image size.
Breaking Down the Dockerfile
A Dockerfile is a plain text file defining a series of instructions and arguments that Docker interprets to create an image. Here is an explanation of each instruction used in our example:- FROM: Sets the base image—in this case, Ubuntu. Every Dockerfile begins with a FROM instruction referencing an existing image on Docker Hub.
- RUN: Executes commands in the container. In the Dockerfile, the first RUN command updates the package lists and installs necessary packages. Combining commands with && minimizes the image layers.
- COPY: Transfers files from your local system into the image. Here, it copies the source code to
/opt/source-code. - ENTRYPOINT: Specifies the command that runs when the container starts. In this example, it sets the environment variable
FLASK_APPand starts the Flask web server.
- The base Ubuntu OS.
- APT updates and the installation of required packages.
- Python package installation.
- Copying of the source code.
- Setting of the ENTRYPOINT.
docker history command.
Building Your Docker Image
When you build your Docker image using thedocker build command, Docker outputs each step along with its result. Docker caches each layer so that if a build step fails and you fix the issue, previous layers are reused, speeding up subsequent builds. Here’s an example build process:
Inspecting the build layers with
docker history mmunshad/my-custom-app can help optimize your Dockerfile by identifying unnecessary layers.Beyond Web Applications
Docker is not limited to containerizing web applications. It can encapsulate a wide range of software including databases, development tools, and even full operating systems. Popular applications containerized with Docker include web browsers like Chrome and Firefox, utilities like cURL, and applications like Spotify or Skype. In the future, containerization may become the norm, simplifying software deployment and maintenance.