How to Deploy Anything to Kubernetes (Part 1 - Containerizing an Application)

Do we really mean anything? No. Because not everything is capable of being deployed with Kubernetes, and some things are not worth deploying on Kubernetes. Those things are beyond the scope of this writing, but in a future series, we will explore the options available for getting those applications onto a Kubernetes cluster.

Let's get right to it and talk about what you're here for – containerizing an application so you can deploy it on Kubernetes. You can think of a container as a virtual machine. It's more or less a wrapper for your application that contains all the dependencies your application needs to run.

For this step, we are going to put your application in a container. To do this, you will need to have Docker installed. Once Docker is installed, you will write a Dockerfile which tells Docker three things (among others):

  1. What dependencies your application needs.
  2. How to build your application.
  3. How to run your application.

Let's consider a Python application. Here's how we might containerize that application by creating a Dockerfile right beside our python code.

FROM python:latest


COPY requirements.txt ./

RUN pip install --no-cache-dir -r requirements.txt

COPY *.py ./

CMD [ "python3", "-u", "./" ]
Dockerfile for a Python Application

Now Here's that same Dockerfile with comments to explain what's happening.

# Use the latest python image from Dockerhub (
FROM python:latest

# Set the directory to /app (similar to using the `cd` command)

# Copy a file into the container
COPY requirements.txt ./

# Run `pip install` to install dependencies specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Copy all files that end in `.py` into the container
COPY *.py ./

# Set the command to be executed when the container runs
CMD [ "python3", "-u", "./" ]
Dockerfile for a Python Application – With Comments

The end result of running this Dockerfile will be a container image that has all of your dependencies and your code. If you are following along with a different application, you might need to add a command to the Dockerfile to actually build your code.

Great! Now you have a Dockerfile that defines your dependencies. Let's use that to actually create a container image that Kubernetes can use. To test this out locally, you can use the following command to have Docker run your Dockerfile steps:

docker build -t myapp:latest .

If everything ran successfully, you should have a container image that is able to be used. You can confirm that the image exists by running the docker images command, and you can use docker run myapp:latest run the container and docker ps to ensure that the container is running.

Coming Up

In the next article, we will expand on this work to place our container image in a registry which a Kubernetes cluster can pull the image from. Here are the links to the articles in this series:

  • Part 1: Containerizing an Application (This Article)
  • Part 2: Push Image to Registry
  • Part 3: Registry Pull Secrets
  • Part 4: Write a Kubernetes Manifest
  • Part 5: Deploy An Application
  • Part 6: VMs on Kubernetes

If you like this article and want to follow this series as we publish and expand it, subscribe for email alerts!