Hands-On Lab: Deploying a Docker Container to Kubernetes

Hello there, fellow tech enthusiasts! Today, we're going to explore a hands-on lab that's sure to get your coding juices flowing. In this tutorial, we’ll learn about deploying a Docker container to Kubernetes - one of the most popular orchestration tools for containerization. You'll get hands-on experience that will help you better understand how to get started with Docker containers on Kubernetes.

Why Kubernetes?

Before jumping into the lab itself, let's discuss why Kubernetes is such a popular orchestration tool. Kubernetes (also known as K8s) is an open-source platform that automates container operations. It helps you manage, scale, and deploy containerized applications in a reliable, scalable, and fault-tolerant way. Kubernetes is built on top of Docker - a leading containerization platform that automates the deployment of applications in containers without changing the code.

Kubernetes became so popular because it solves many problems related to container orchestration. Containers are lightweight, fast, and efficient, but they can be difficult to manage and scale without the right tools. Kubernetes helps you solve those problems through its ability to automate container management, auto-scale, and load balancing.

What is Docker Containerization?

Docker is a popular containerization technology that is used to build and deploy applications in containers. It allows you to package an application and all of its dependencies, libraries, and runtime tools into a single container. The container is then run on top of a host operating system, creating an isolated environment that runs the application. This makes it easier to move the application from one environment to another, without worrying about dependencies and compatibility issues.

Docker also provides a platform-agnostic way of building and managing applications. You can build Docker images that can run on any platform- whether it's Windows or Linux. Docker also allows for easy scaling of applications, since each container can run its own copy of the application.

Deploying a Docker Container to Kubernetes

Now that we have a basic understanding of Docker and Kubernetes, let's get started with our hands-on lab. We'll deploy a simple "Hello World" application in a Docker container and deploy it on Kubernetes.


Before getting started, make sure you have the following prerequisites:

For this tutorial, we'll be using Minikube, a lightweight Kubernetes implementation that runs in a virtual machine on your local computer.

Step 1: Create a Simple "Hello World" Application

The first step is to create a simple "Hello World" application that we'll deploy in a Docker container. We'll use a popular Python web framework called Flask to create a simple web application.

Create a new directory for our project and navigate to it using the terminal.

mkdir hello-world
cd hello-world

Create a new file named app.py and add the following code:

from flask import Flask
app = Flask(__name__)

def hello_world():
    return 'Hello, World!'

Run the application using Flask with the following command:

export FLASK_APP=app.py
flask run

If everything is set up correctly, you should see output like the following:

 * Serving Flask app "app.py"
 * Running on (Press CTRL+C to quit)

Open up your browser, navigate to http://localhost:5000/ and you should see the famous phrase "Hello, World!" displayed.

Step 2: Build a Docker Image

Now that we have a simple web application running, let's put it into a Docker container. Create a new file named Dockerfile in your project directory.

Add the following code to your Dockerfile:

FROM python:3.9-alpine
COPY . /app
RUN pip install flask
CMD ["python", "app.py"]

This Dockerfile starts with a base image (Python 3.9 Alpine). It then copies the contents of your project directory into the container under the /app directory. It sets the working directory to /app, installs Flask, defines the command to start your application, and finally exposes port 5000 for your application.

Build your Docker image with the following command:

docker build -t hello-world:latest .

This will build a Docker image named hello-world:latest using the Dockerfile in your current directory. The . at the end of the command specifies the current directory as the build context.

Step 3: Test Your Docker Image

You've built a Docker image, but how do you know it works? Let's test it out locally.

Run the following command:

docker run -p 5000:5000 hello-world

This command starts a new container based on your Docker image and maps port 5000 in the container to port 5000 on your local machine. You should now be able to navigate to http://localhost:5000/ and see the "Hello World" message again.

Step 4: Deploy Your Docker Image to Kubernetes

Now that we have a working Docker image, let's deploy it to Kubernetes. We'll use Kubectl (the Kubernetes command-line tool) to create a new deployment of our application.

Start by opening up your terminal and running the following command:

minikube start

This command starts up Minikube, which creates a new local Kubernetes cluster on your machine.

Before creating a deployment, make sure your Docker image is stored in a container registry. In this tutorial, we'll use Docker Hub as our container registry.

First, login to Docker Hub:

docker login

This command will prompt you to enter your Docker Hub credentials.

Next, tag your Docker image with your Docker Hub username and the image name:

docker tag hello-world:latest [DOCKERHUB_USERNAME]/hello-world:latest

Then, push the image to Docker Hub:

docker push [DOCKERHUB_USERNAME]/hello-world:latest

Your Docker image is now stored in Docker Hub and can be accessed by Kubernetes.

Next, create a new deployment using the following command:

kubectl create deployment hello-world --image=[DOCKERHUB_USERNAME]/hello-world:latest

This command creates a new deployment named hello-world and tells Kubernetes to use your Docker image from Docker Hub.

Check the status of your deployment with the following command:

kubectl get deployments

You should see output like the following:

hello-world   1/1     1            1           5m

Your deployment is ready and running! But how can you access it? Kubernetes creates a Kubernetes Service to expose your deployment to the outside world.

Create a new Kubernetes Service with the following command:

kubectl expose deployment hello-world --type=LoadBalancer --port=5000 --target-port=5000

This command creates a new Kubernetes Service named hello-world of type LoadBalancer, which exposes port 5000 and targets port 5000 (the same port we exposed earlier in our Dockerfile).

Check the status of your service with the following command:

kubectl get services

You should see output like the following:

NAME          TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
hello-world   LoadBalancer    <pending>     5000:30827/TCP   2m

Note the EXTERNAL-IP field may take a few moments to populate.

Step 5: Test Your Deployment

Your Kubernetes deployment and service are up and running! Let's test it out:

minikube service hello-world

This command opens up the default browser and navigates to your Kubernetes Service. You should now see the "Hello, World!" message displayed in your browser.

Congratulations! You've successfully deployed a Docker container to Kubernetes.


In this tutorial, we've explored the basics of deploying a Docker container to Kubernetes. We've learned what Docker and Kubernetes are, created a simple "Hello World" web application, built a Docker image, and deployed it to Kubernetes. We've also learned how to create a Kubernetes Service to expose our deployment to the outside world.

I hope you found this lab informative and useful for your next project. Stay tuned for more hands-on labs that will help you get started with cutting-edge technologies in the world of software engineering, cloud deployment, networking and crypto.

Editor Recommended Sites

AI and Tech News
Best Online AI Courses
Classic Writing Analysis
Tears of the Kingdom Roleplay
Optimization Community: Network and graph optimization using: OR-tools, gurobi, cplex, eclipse, minizinc
Cloud Governance - GCP Cloud Covernance Frameworks & Cloud Governance Software: Best practice and tooling around Cloud Governance
Developer Recipes: The best code snippets for completing common tasks across programming frameworks and languages
Cloud Blueprints - Terraform Templates & Multi Cloud CDK AIC: Learn the best multi cloud terraform and IAC techniques
New Friends App: A social network for finding new friends