Docker Images
What is a Docker Image?
A Docker Image is a read-only template that defines the environment and configuration required to run an application. It contains everything needed to run an application, including the code, runtime, libraries, environment variables, and configuration files.
How to Create a Docker Image
Docker Images are created using a Dockerfile, which is a text file that contains a series of instructions for building the image. These instructions define the base image, environment variables, dependencies, and commands needed to set up the application environment.
Here is an example of a Dockerfile that builds an image for a simple Python application.
First, create a directory for your application and add the following files:
myapp/
├── app.py
├── dependencies.txt
└── Dockerfile
Execute the following commands to create the files:
mkdir myapp
cd myapp
touch app.py dependencies.txt Dockerfile
The app.py file contains a simple Python application, this exemple is a simple web server that listens on port 8080:
from http.server import HTTPServer, BaseHTTPRequestHandler
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(b'Hello, World!\n')
# Specify the server address and port
server_address = ('', 8080) # The empty string '' means the server will listen on all available network interfaces
# Create an instance of HTTPServer
httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)
# Start the server to listen continuously for HTTP requests
print("Server started at http://localhost:8080")
httpd.serve_forever()
The dependencies.txt file contains the dependencies required by the application that will be installed using pip during the build process.
flask==2.0.1
requests==2.26.0
| Library | Version | Description |
|---|---|---|
| flask | 2.0.1 | A web framework for Python that we will use to create a simple web server. |
| requests | 2.26.0 | An HTTP library for Python that we will use to make HTTP requests. |
The Dockerfile contains the instructions to build the Docker image.
Each instruction in the Dockerfile creates a new layer in the image, and Docker caches these layers to speed up the build process.
Every layer is explained in the following code:
# Use the official Python image as the base image, version 3.12
FROM python:3.12
# Set the working directory in the container to /app
WORKDIR /app
# Copy the dependencies file into the container at /app
COPY dependencies.txt .
# Upgrade pip to the latest version
RUN pip install --no-cache-dir --upgrade pip
# Install the dependencies that we just copied into the container using pip
RUN pip install --no-cache-dir -r dependencies.txt
# Copy the rest of the application code into the container
COPY . /app
# Expose port 8080 to allow outside connections to the application
EXPOSE 8080
# Set the default command to run when the container starts
CMD ["python", "app.py"]
To build the image, you can run the following command in the same directory as the Dockerfile:
docker build -t myapp .
-t flag is used to tag the image with a name (e.g., myapp).
. specifies the build context, which is the directory containing the Dockerfile and any files needed to build the image.
The build process will start, and you will see the output of each instruction in the Dockerfile being executed.

You can specify a file other than Dockerfile by using the -f flag, for example let's say you have a Dockerfile.prod file:
docker build -t myapp -f Dockerfile.prod .
This command will build the image using the Dockerfile.prod file.
Image Tags
Images can be tagged with version numbers or labels to identify different versions of the same image. Tags are used to reference specific versions of an image when running containers.
docker build -t myapp:1.0 .
docker build -t myapp:latest .
The -t flag is used to tag the image with a name and optionally a version number or label. (e.g., myapp:1.0).
If no tag is specified, Docker will use the latest tag by default.
Docker Image Layers
Images are built in layers, with each layer representing a specific instruction in the Dockerfile. These layers are cached and reused to speed up the build process.
In the example above, the Dockerfile instructions are split into multiple layers, each representing a different step in the build process.
Layers a represented in the following way:
| Layer | Description | Dockerfile Instruction |
|---|---|---|
| 1 | Base Image | FROM python:3.12 |
| 2 | Set Working Directory | WORKDIR /app |
| 3 | Copy Dependencies | COPY dependencies.txt . |
| 4 | Upgrade pip | RUN pip install --no-cache-dir --upgrade pip |
| 5 | Install Dependencies | RUN pip install --no-cache-dir -r dependencies.txt |
| 6 | Copy Application Code | COPY . /app |
| 7 | Expose Port 8080 | EXPOSE 8080 |
| 8 | Set Default Command | CMD ["python", "app.py"] |
When you make changes to the Dockerfile and rebuild the image, Docker will only rebuild the layers that have changed, using the cached layers for the unchanged instructions. If a layer fails to build, Docker will stop the build process and display an error message.
Images Information
Images have metadata that provides information about the image, such as:
| REPOSITORY | TAG | IMAGE ID | CREATED | SIZE |
|---|---|---|---|---|
| myapp | latest | 94e6752aa84e | 7 minutes ago | 1.04GB |
You can view the metadata of your images using the docker images or docker image ls command:
docker images
This command will display a list of all the images on your system, along with their repository, tag, image ID, creation date, and size.

If you want to see the information about a specific image, you can use the docker images command with the image name:
docker images myapp

Image Inspection
You can view detailed information about an image using the docker inspect command:
docker inspect myapp
This command will display a JSON object containing information about the image, such as the layers, environment variables, and labels.
Image Layers
You can view the layers that make up an image using the docker history command:
docker history myapp
This command will display a list of the layers in the image, along with the commands used to create each layer.

Next Step
Now that you have created a Docker image, you can run it as a container. Continue to Docker Containers to learn about running and managing Docker containers.