0Pricing
Docker & DevOps Fundamentals · Lesson

Creating Custom Docker Images

Build your first custom Docker image for a simple application, tagging and versioning it correctly.

Creating Custom Docker Images is a free Docker & DevOps Fundamentals lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Docker & DevOps Fundamentals learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Build Your First Docker Image

Why build custom images? They let you package your specific application, its dependencies, and configuration into a single, portable unit. This is key for consistency and easy deployment.

You define how to build this image using a file called a Dockerfile.

Creating Custom Docker Images — illustration 1

Our Simple Web App

We'll containerize a super simple Python Flask web app. It just returns "Hello from Docker!" when you visit its web address.

Imagine we have these files in a directory:

  • app.py: The Python application code
  • requirements.txt: Lists Python dependencies
  • Dockerfile: Instructions for building the image

The Flask Application Code

This is our basic Python Flask app. It listens on port 5000 and responds with "Hello from Docker!".

You can run this locally (after installing Flask) to see it in action!

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello from Docker!'

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

Specifying Dependencies

Our app needs the Flask library. We list it in requirements.txt. This file tells Docker (and Python's package manager, pip) what to install.

This ensures our image has all necessary libraries for the app to run.

Flask==2.3.2

Anatomy of Our Dockerfile

A Dockerfile is a text file with instructions to build an image. Each instruction creates a layer. Here's what we'll use:

  • FROM: Specifies the base image (e.g., Python).
  • WORKDIR: Sets the working directory inside the container.
  • COPY: Copies files from your local machine into the image.
  • RUN: Executes commands (e.g., install dependencies).
  • EXPOSE: Informs Docker about ports the container listens on.
  • CMD: Defines the default command to run when the container starts.

Crafting the Dockerfile

Here's the complete Dockerfile for our Flask app. Place this file in the same directory as app.py and requirements.txt.

# Use an official Python runtime as a parent image
FROM python:3.9-slim-buster

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY requirements.txt .
COPY app.py .

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 5000 available to the world outside this container
EXPOSE 5000

# Run app.py when the container launches
CMD ["python", "app.py"]

The `docker build` Command

Now, let's build our image! Open your terminal in the directory where your Dockerfile and app files are located.

The -t flag tags our image with a name (my-flask-app) and a version (1.0). The . tells Docker to use the current directory as the build context.

docker build -t my-flask-app:1.0 .

Verifying Your New Image

After the build completes, you can verify that your new image exists by listing all local Docker images.

Look for my-flask-app with the tag 1.0 in the output.

docker images

Launching Your App Container

Time to see your app in action! Use docker run to create and start a container from your image.

The -p 5000:5000 maps port 5000 on your host machine to port 5000 inside the container. -d runs it in detached mode (in the background).

docker run -d -p 5000:5000 my-flask-app:1.0

Visit http://localhost:5000 in your browser to see "Hello from Docker!".

Image Tagging Best Practices

Tags are crucial for managing different versions of your images. A common practice is to use semantic versioning (e.g., 1.0.0, 2.1.5) and also a latest tag.

You can add multiple tags to an image using docker tag:

docker tag my-flask-app:1.0 my-flask-app:latest

This allows you to refer to the same image by different names, making it flexible for deployments.

Quick Check

Consider the following Dockerfile and build command:

# Dockerfile content
FROM alpine:latest
WORKDIR /app
COPY . .
CMD ["echo", "Hello"]

docker build -t my-app:v1 .

Which of the following statements is TRUE about the docker build -t my-app:v1 . command?

Recap: Custom Images

Great job! You've learned how to:

  • Prepare a simple application for containerization.
  • Write a basic Dockerfile using key instructions (FROM, WORKDIR, COPY, RUN, EXPOSE, CMD).
  • Build a custom Docker image using docker build -t.
  • Verify your image with docker images.
  • Run a container from your custom image using docker run -p.
  • Understand the importance of image tagging and versioning.

Next, we'll dive deeper into how Docker images are structured with layers and techniques to optimize their size and build times!

Frequently asked questions

Is the “Creating Custom Docker Images” lesson free?

Yes — the full text of “Creating Custom Docker Images” is free to read here on the web, and the Docker & DevOps Fundamentals course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Docker & DevOps Fundamentals course, upgrade to CoddyKit PRO.

What will I learn in “Creating Custom Docker Images”?

Build your first custom Docker image for a simple application, tagging and versioning it correctly. You practise Docker & DevOps Fundamentals with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Docker & DevOps Fundamentals?

No prior experience is required. Docker & DevOps Fundamentals on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Creating Custom Docker Images” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Docker & DevOps Fundamentals lesson?

Yes. Every Docker & DevOps Fundamentals lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Understanding Dockerfiles
  2. Creating Custom Docker Images
  3. Image Layers and Optimization
  4. Multi-Stage Builds & Smaller Images
← Back to Docker & DevOps Fundamentals