DEV Community

Cover image for Understanding Docker Using a Tiny Python Application (A Beginner-Friendly Walkthrough)
Sanjay Ghosh
Sanjay Ghosh

Posted on

Understanding Docker Using a Tiny Python Application (A Beginner-Friendly Walkthrough)

Many Docker tutorials immediately introduce Docker Compose, Kubernetes, networking, volumes, and multi-container applications.

For beginners, that can feel overwhelming.

In this article, we'll build a very small Docker image that runs a simple Python script. The goal is to understand the core Docker concepts before moving on to more advanced containerization topics.

By the end of this tutorial, you'll understand:

  • What Docker is
  • What a container is
  • How Docker images work
  • How to build an image
  • How to run a container
  • The difference between an image and a container

Why Docker?

One common problem in software development is:

"It works on my machine."

An application may work perfectly on a developer's computer but fail on another machine because of missing libraries, different operating systems, or incompatible software versions.

Docker solves this problem by packaging:

  • Application code
  • Runtime
  • Libraries
  • Dependencies
  • Configuration

into a single portable unit called a container.

As long as Docker is installed, the application can run consistently across environments.


Docker in Simple Terms

A useful analogy is:

Docker Image  = Blueprint
Docker Container = House Built From The Blueprint
Enter fullscreen mode Exit fullscreen mode

An image is a template.

A container is a running instance created from that image.

You can create multiple containers from the same image.


Installing Docker on Ubuntu

Update package information:

sudo apt update
Enter fullscreen mode Exit fullscreen mode

Install Docker:

sudo apt install docker.io
Enter fullscreen mode Exit fullscreen mode

Verify the installation:

sudo docker run hello-world
Enter fullscreen mode Exit fullscreen mode

You should see a welcome message from Docker.


Running Docker Without sudo

Add your user to the Docker group:

sudo usermod -aG docker $USER
Enter fullscreen mode Exit fullscreen mode

Log out and log back in.

Now Docker commands can be executed without sudo.


Environment

The following environment was used:

Ubuntu 24.04 LTS
Docker 29.1.3
Python 3.x

Project Structure

docker-python-app/

├── test.py
└── Dockerfile

Our Tiny Python Application

Create:

test.py
Enter fullscreen mode Exit fullscreen mode

Contents:

print("This is a test")
Enter fullscreen mode Exit fullscreen mode

Normally, the script can be executed using:

python3 test.py
Enter fullscreen mode Exit fullscreen mode

Now we'll run the same application inside a Docker container.


Creating a Dockerfile

Create a file named:

Dockerfile
Enter fullscreen mode Exit fullscreen mode

Contents:

FROM ubuntu:latest

WORKDIR /docker-apps

COPY ./test.py .

RUN apt update && apt install python3 -y

CMD ["python3", "./test.py"]
Enter fullscreen mode Exit fullscreen mode

Docker builds images layer by layer by processing each instruction in the Dockerfile from top to bottom.

Let's understand each instruction.

FROM

FROM ubuntu:latest
Enter fullscreen mode Exit fullscreen mode

Uses Ubuntu as the base image.

WORKDIR

WORKDIR /docker-apps
Enter fullscreen mode Exit fullscreen mode

Sets the working directory inside the container.

COPY

COPY ./test.py .
Enter fullscreen mode Exit fullscreen mode

Copies the Python script into the container.

RUN

RUN apt update && apt install python3 -y
Enter fullscreen mode Exit fullscreen mode

Installs Python inside the image.

CMD

CMD ["python3", "./test.py"]
Enter fullscreen mode Exit fullscreen mode

Specifies the command executed when the container starts.


Building the Docker Image

Build the image:

docker build -t ubuntu-python .
Enter fullscreen mode Exit fullscreen mode

Docker reads the Dockerfile and creates an image named:

ubuntu-python
Enter fullscreen mode Exit fullscreen mode

Verify:

docker images
Enter fullscreen mode Exit fullscreen mode

You should see the newly created image.


Running the Container

Create and start a container:

docker run -it -d --name test-container ubuntu-python
Enter fullscreen mode Exit fullscreen mode

Verify:

docker ps -a
Enter fullscreen mode Exit fullscreen mode

View container logs:

docker logs test-container
Enter fullscreen mode Exit fullscreen mode

Expected output:

This is a test
Enter fullscreen mode Exit fullscreen mode

Congratulations! You have successfully executed a Python application inside a Docker container.

Screenshot

Docker Workflow

test.py
      ↓
Dockerfile
      ↓
docker build
      ↓
Docker Image
      ↓
docker run
      ↓
Docker Container
      ↓
This is a test
Enter fullscreen mode Exit fullscreen mode

This same workflow is used for much larger applications.


Image vs Container

One of the most important Docker concepts is understanding the difference between an image and a container.

Image

An image is:

  • Read-only
  • A template
  • Used to create containers

Example:

ubuntu-python
Enter fullscreen mode Exit fullscreen mode

Container

A container is:

  • Running or stopped
  • Created from an image
  • The actual execution environment

Example:

test-container
Enter fullscreen mode Exit fullscreen mode

A single image can create many containers.


Useful Docker Commands

Stop a container:

docker stop <container_name_or_id>
Enter fullscreen mode Exit fullscreen mode

Remove a container:

docker rm <container_name_or_id>
Enter fullscreen mode Exit fullscreen mode

Force remove a container:

docker rm -f <container_name_or_id>
Enter fullscreen mode Exit fullscreen mode

Remove all stopped containers:

docker container prune
Enter fullscreen mode Exit fullscreen mode

List images:

docker images
Enter fullscreen mode Exit fullscreen mode

List containers:

docker ps -a
Enter fullscreen mode Exit fullscreen mode

The Most Important Lesson

One of the biggest misconceptions among beginners is that Docker is a virtual machine.

It is not.

Containers share the host operating system kernel, making them much lighter and faster than traditional virtual machines.

Docker packages applications and their dependencies in a portable and isolated way without requiring a full guest operating system.

Understanding this distinction is one of the key concepts in containerization.


💡 Key Takeaway
Docker does not replace your application.

Docker packages your application together with its runtime and dependencies so it can run consistently across different environments.

Understanding the difference between an image and a container is one of the most important Docker concepts.

Future Enhancements

Once comfortable with this example, you can explore:

  • Docker volumes
  • Docker networking
  • Docker Compose
  • Multi-container applications
  • Docker Hub
  • Jenkins + Docker integration
  • Kubernetes

The concepts learned in this article form the foundation for all of them.


Final Thoughts

Docker can seem intimidating when first encountered through large production deployments.

Starting with a tiny application makes the learning process much easier.

A simple Python script is enough to understand:

  • Images
  • Containers
  • Dockerfiles
  • Build process
  • Container execution

Once these fundamentals become clear, moving toward real-world containerized applications becomes much more natural.

If you're new to Docker, try building this example yourself before moving on to Docker Compose, Kubernetes, or cloud deployments. The concepts learned here will transfer directly to larger containerized systems.

Top comments (0)