Winter Sale - up to 36% OFF

How to Deploy a Docker Container on Kubernetes: Step-by-Step Guide

How to Deploy a Docker Container on Kubernetes: Step-by-Step Guide
Published on Jan 30, 2025 Updated on Jan 30, 2025

#Introduction

Software development today demands an efficient and reliable way of deploying applications, a demand that popular DevOps tools like Docker and Kubernetes can meet. Docker enables developers to wrap their applications and their dependencies into packages called containers. Kubernetes orchestrates deployment, scaling, and even management of these containers within a cluster of machines.

In this article, we will provide step-by-step instructions on getting the docker container up and running and then deploying it to the Kubernetes system.

#Prerequisites

Before you get down to deploying a Docker container on Kubernetes, make sure that you have the following:

  • Docker is required on your local machine to be able to build and manage container images.
  • You have the Kubernetes (kubectl) command-line tool installed to talk to your Kubernetes cluster.
  • You have Minikube (or any other Kubernetes cluster) installed if you are running Kubernetes locally.
  • A place to keep your Docker images when you create them, which can be a container registry (e.g. DockerHub).

Ready to supercharge your Docker infrastructure? Scale effortlessly and enjoy flexible storage with Cherry Servers bare metal or virtual servers. Eliminate infrastructure headaches with free 24/7 technical support, pay-as-you-go pricing, and global availability.

#What are Docker and Kubernetes?

#Docker

Docker is a platform that allows developers to create, deploy, and manage applications in containers. Containers are an example of a unit or piece of software that includes code, runtime, system tools, system libraries, and settings that guarantee the required operation environment for the application so it runs consistently on many environments, including development machine, testing server, and production host, among others. It is then possible to move these applications onto another server or even on the cloud without facing major problems related to compatibility.

The client-server architecture of Docker allows the Docker client to interact with the Docker daemon. With Docker, Docker clients interact with the Docker daemon to manage images, containers, and networks. Docker images represent a form of template used for creating containers. Docker images are reusable, can be shared across projects, and are used successfully.

Also read: How to Run Docker on Bare Metal Cloud

#Kubernetes

Kubernetes is an open-source platform that automates the deployment, scaling, and operation of containerized applications over clusters of containers. The orchestration and management of several containers simultaneously are allowed through creating an easy way in which complex applications will be deployed and managed.

Kubernetes uses the master node in a master-node architecture to control the cluster, and containers run on worker nodes. Among its most important features are self-healing, load balancing, and the discovery of services that aid in an application's reliability at runtime.

You can create a powerful and flexible platform for building and deploying containerized applications by combining Docker and Kubernetes. Docker provides the building blocks, while Kubernetes provides the orchestration and management capabilities.

Also read: How to Deploy Kubernetes on Bare Metal

#Deploying a Docker container on Kubernetes

The instructions below show you how to deploy a docker container on Kubernetes.

#Create a Docker image

We will create a Docker image, push it to a container registry, create a Kubernetes deployment, and reveal the application. In this guide, we will deploy a basic Nginx web server.

Firstly, create a directory and navigate into it.

mkdir nginx-k8s 

cd nginx-k8s

Create directory

Inside this directory, create a dockerfile.

nano dockerfile

Add the following lines of code.:

FROM nginx:latest
COPY ./index.html /usr/share/nginx/html/index.html

Edit dockerfile

Create a custom index.html file

nano index.html

Add the sample code as shown below

<!DOCTYPE html>
<html>
<head>
    <title>Hello Kubernetes</title>
</head>
<body>
    <h1>We have successfully deployed Nginx container on Kubernetes!</h1>
</body>
</html>

Edit html page

Next, you need to build the docker image. Use the below docker command to build the image:

docker build -t my-nginx-k8s:1.0 .

Build the docker image

#Push Docker image to DockerHub

To deploy your Docker image on Kubernetes, it needs to be accessible to Kubernetes nodes. You can push it to a container registry like Docker Hub.

If you are pushing to Docker Hub, tag the image using the below command. Note that you need to Replace “demo042” with your docker hub name.

docker tag my-nginx-k8s:1.0 demo042/my-nginx-k8s:1.0

Tag the docker image

Now you need to login to Docker Hub.

docker login

Docker login

You need to press ENTER, and you will be directed to a page similar to the one below. Click on Confirm.

Docker login device confirmation

Login to docker with your credentials; I am using my Gmail to log in.

Docker sign in

You should get a congratulations message once the login is successful.

Docker login confirmation

Now go back to your terminal, and you should get the “Login Succeded” message.

Docker login successful

Finally, push the image to Docker Hub using the below command:

docker push demo042/my-nginx-k8s:1.0

Push the docker image to dockerhub

You can check your dockerhub repository to check if the docker image was created successfully.

Dockerhub repository

It’s time to deploy the docker container on Kubernetes using the docker image we just created.

Also read: How to install Docker on Ubuntu 24.04

#Configure Kubernetes YAML files

Follow the steps below to deploy a Docker container on Kubernetes using a YAML file.

Create a YAML file for the deployment. Use a text editor to create a YAML file that defines the deployment configuration. Specify the desired number of replicas, the image name, and any necessary environment variables or labels.

For example, name it nginx-deployment.yaml

nano nginx-deployment.yaml

and include both the deployment and the service definitions in the same file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: demo042/my-nginx-k8s:1.0
        ports:
        - containerPort: 80

Nginx deployment configuration

#Deploy the Docker container on Kubernetes

Deploy the application in your Kupernetes cluster using the kubectl command-line tool. Run the following command to apply the nginx-deployment.yaml file:

kubectl apply -f nginx-deployment.yaml

Nginx deployment

Check the status of the deployment to ensure that the desired number of pods are running:

kubectl get deployments

Get the deployments

You need to find the name of the Pod you want to access. Use the following command to list all running Pods:

kubectl get pods

List the pods

To access a Kubernetes pod on your browser, you can route traffic from your machine to a specific Pod in your Kubernetes cluster. This process is called port forwarding.

We will now use the kubectl port-forward command to connect a local port on our device with the port of the pod. The general format is:

kubectl port-forward <pod-name> <local-port>:<pod-port>

For example, if you want to forward port 8080 on your local machine to port 80 in the Nginx Pod, run:

kubectl port-forward nginx-deployment-6df5f6d8df-9pt9l 8080:80

Port forwarding

#Access the deployed application

Once the port is forwarded, you can open the web browser and go to https://localhost:8080 to access the pod.

Nginx container deployed on Kubernetes

You should see your Nginx welcome message that was configured in the YAML file.

#Conclusion

Congratulations! You’ve successfully deployed a Docker container on Kubernetes. Using Docker containers on Kubernetes gives you the benefits of container orchestration to manage apps in a way that is scalable, reliable, and productive.

Whether you are working with Minikub or running a production cluster in the cloud, Docker and Kubernetes together offer a strong answer for deploying modern apps. Keep learning about Kubernetes features and the best ways to work to get the most out of your containerized apps and make sure they do well in the fast-changing world of software development.

Cloud VPS - Cheaper Each Month

Start with $9.99 and pay $0.5 less until your price reaches $6 / month.

Share this article

Related Articles

Published on May 15, 2024 Updated on May 15, 2024

Portainer Tutorial: How to Update [and Restart] Portainer

This tutorial demonstrates how to manage Portainer: how to update Portainer, how to restart Portainer, and update existing containers.

Read More
Published on Apr 14, 2020 Updated on Feb 6, 2024

Docker Swarm vs. Kubernetes – What are the Subtle Differences?

Kubernetes and Docker Swarms are essential tools used to deploy containers inside a cluster. Learn how they differ and when to use them

Read More
Published on May 29, 2023 Updated on Aug 21, 2024

How to Start and Stop Docker Containers | Step-by-Step

This step-by-step tutorial will cover basic Docker commands, such as how to start and stop Docker containers and list containers with filters.

Read More
We use cookies to ensure seamless user experience for our website. Required cookies - technical, functional and analytical - are set automatically. Please accept the use of targeted cookies to ensure the best marketing experience for your user journey. You may revoke your consent at any time through our Cookie Policy.
build: f894329ec.894