How to Deploy Containers to a Kubernetes Cluster Using Docker


· ·

Setting up local development environments can be tiresome. Using infrastructures such as Docker and Kubernetes gives a developer an upper hand to quickly set up and deploy applications. This guide teaches you how to leverage Docker and Kubernetes to deploy Docker Containers to the Kubernetes cluster.

Containers are lightweight operating systems that run as a form of virtualization. They let you package your applications and run them in isolated containerized environments. This gives you the power to quickly run powerful applications from different computing environments. Kubernetes pushes containers a step further by orchestrating them across a broad domain. Let’s dive in and create one the learn how to deploy the container to a Kubernetes Cluster using Docker.

How to Deploy Containers to Kubernetes Cluster using Docker

To get a container up and running on Kubernetes, we will create a basic Node.js application. Using this application, we will run it through Docker and finally land it on Kubernetes Cluster.

Note: Node.js app is a demonstrative example. If you have any applications ready and running, you can use them along with this guide.

Step One: Requirements

To get a Kubernetes Cluster up and running, we will use the following tools:

  • Docker Engine: For creating, building, and deploying application images.
  • A Docker Hub Account for deploying Docker images to the Docker Hub registry.
  • Minikube: Minikube is Kubernetes tool. It allows you to run Kubernetes locally on your computer. It runs as a single-node Kubernetes cluster within your local computer, making it easy to develop the Kubernetes app.
  • Kubectl: Kubectl is a Kubernetes command-line (CLI) tool. It allows you to run commands related to deploying, inspecting, and managing cluster resources against Kubernetes.
  • Node.js runtime: For creating a Node.js app.

Ensure you have the above tools installed on your computer.

Step Two: Setting up Minikube and Kubectl

It is essential to ensure the above tools are installed tools and correctly configured on your computer before Deploying your container to the Kubernetes cluster using Docker.

You can check if Node.js is correctly installed using the following command:

To check if Docker is installed correctly, run the following:

docker version
# client
# version
# server Docker Desktop

At this point start you installed Docker App:

Also, ensure it is up and running:

Once you have installed Minikube on your computer, run the following command to get it up, and running within your installed Docker Engine:

minikube start

It’s time to get Kubectl. To run Kubectl, we need to execute its installed binaries from PATH environment variables. First, navigate to the directory where Kubectl got installed. Then copy the bin directory path. Finally, add the path to your computer’s PATH environment variables.

If you have any problems adding the installed Kubectl PATH binaries, check this guide to help you add the Kubectl binaries correctly.

Once you have the PATH ready, run the following command to check if your set Kubectl is ready to execute Kubernetes commands:

kubectl cluster-info

Step Three: Setting up a Basic app

Kubernetes allows you to manage large applications. Likewise, Docker can deploy this application to a Kubernetes cluster seamlessly. For this guide, we will just create a simple Node.js app to demonstrate how you can get your application running on a Kubernetes cluster.

If you have an application reader, you can skip this step and use the application alongside this guide.

First, navigate to your project directory and initialize Node.js using the following command:

npm init-y

Just like any application, you go ahead and add dependencies/libraries to run your application. In this case, we create a minimal Node.js app. Therefore, we will use the Express library to create a simple Node.js server. Install Express as follows:

npm install express

Then create an index.js file and create the server as follows:

const express = require('express');
const app = express();

const  port = 3000;

app.get('/', (req, res) => {
  res.send('Hello Kubernetes!!!');

app.listen(port, () => {
  console.log(`Server started on port ${port}`);

Then test the application by running the following:

node index.js

Finally, open http://localhost:3000 on the browser to test the application:

We will use this example. At the end of this tutorial, you should have the above example running on a Kubernetes cluster. Let’s dive in and start deploying the application.

Step Four: Writing Dockerfile

To get the application ready, we will first write a few commands that will instruct Docker to build an image for this specific application.

Note: Each application Dockerfile differs. Therefore, always ensure you have the right Dockerfile for running your application. In this example, we will write the Dockerfile as follows:

Inside the project directory, create a Dockerfile file and add the following Docker commands.

# pull the Node.js from the Docker registry
FROM node:18-alpine3.15

# set default dir so that subsequent commands execute in /src/app dir
WORKDIR /src/app

# copy dependencies files
COPY package.json ./
COPY package-lock.json .

# will execute npm install in /src/app because of WORKDIR
RUN npm install

# copy application files (index.js)
COPY ./ ./

# Port t expose the application on Docker

# The command for running the application
CMD ["node", "index.js"]

In case you have any files and folders that you don’t your Docker to build, create a .dockerignore file and add such items. In our case, we installed Node.js packages to create a directory node_modules. We don’t need to copy such items as:

  • They are large.
  • Executing RUN npm install will automatically create this folder. There is no need to copy it over to Docker.

Therefore, go ahead and add this directory to your dockerignore file.


Step Five: Building the Docker Image

Once you have the Dockerfile ready, let’s dive in and run the application using Docker. Here we will:

  • Build the application image
  • Allows us to test if the application is correctly running on Docker.
  • Build a global registry image for Docker Hub

Let’s now dive in and learn how to build the container image using the Dockerfile we have created.

First, let’s build the image and test it locally. Inside your Dockerfile root directory, run the following command:

docker build -t node-app.

Now run a container from this image and see that it works as expected:

docker run -it -p 3000:3000 node-app

If you open http://localhost:3000, you should be served the same application we run locally. However, this time the application will be loaded from a Docker container.

To deploy this application to Kubernetes, we first need to push the image to a Docker Hub registry. To do so, login to your Docker Hub account using a terminal by running:

docker login

Provide your correct Docker Hub username and password. If the credentials are correct, a Login Succeeded message will be logged on your terminal.

Proceed and build an image using your username as follows:

docker build -t <Enter your Docker Hub username>/node-app

Here, ensure you have the correct username added to the above command. This will form the image name of your application. For example:

docker build -t kimafro/node-app

Once the image is built successfully, go ahead and push it to your Docker Hub registry as follows:

docker push <Enter your Docker Hub username>/node-app

Replace your username as such:

docker push kimafro/node-app

Step Six: How to Deploy the Application to a Kubernetes Cluster

We have Docker ready. Let’s now get it up and running on the cluster. First, we need to create the deployment command for Kubernetes using a .yaml file. o ahead and create a k8s.yaml at the root of your project directory.

Create the deployment configuration for Kubernetes as follows:

apiVersion: apps/v1
kind: Deployment
  name: nodeapp-deployment
    app: nodeapp
  replicas: 1
      app: nodeapp
        app: nodeapp 
      - name: nodeserver
        image: kimafro/node-app
        imagePullPolicy: Always
        - containerPort: 3000

This will create the pods where the cluster will run. Then pull the image from the Docker registry and expose it on port 3000. Replace image: kimafro/node-app with the image name used alongside the Docker Hub username. I.e., <Enter your Docker Hub username>/node-app.

Inside the same file, create a service to expose the application as follows:

apiVersion: v1
kind: Service
  name: nodeapp-service
    app: nodeapp 
  type: LoadBalancer
  - protocol: TCP
    port: 5000
    targetPort: 3000
    nodePort: 31110

This example will use a load balance. However, you can go ahead and choose a different approach to expose the application. Other options include NodePorts, LoadBalancers, Ingresses, and Cluster IP. Check this guide and learn more about them.

The application is ready. Let’s use Kubectl to deploy it. Go ahead and run the following command inside the application directory:

kubectl create -f k8s.yaml

This will create the application deployment and the service:

deployment.apps/nodeapp-deployment created
service/nodeapp-service created

To check if the application cluster is running, use the following command:

kubectl get pods

NAME                                 READY   STATUS    RESTARTS   AGE
nodeapp-deployment-b8876f487-cgrv8   1/1     Running   0          8s

At this point, the application status should be Running. To test the deployed cluster service run:

minikube service nodeapp-service

Where nodeapp-service is the service name added in the k8s.yaml file.

Running the above command will loach the application as follows:

And there you have the application running the Kubernetes cluster. The application will be launched on your browser. However, you can go ahead and use the and access the application. (Your URL might be different from this one. Copy the URL displayed in the above dashboard of your terminal)

If you are done using the cluster, you can delete it using:

kubectl delete -f k8s.yaml


deployment.apps "nodeapp-deployment" deleted
service "nodeapp-service" deleted


Kubernetes is an open-source technology for automating the deployment, scaling, and management of container orchestration. Docker lets you run applications in containers, and Kubernetes helps you manage those containers. I hope you found this guide helpful and have successfully learned how to deploy containers to Kubernetes cluster using Docker.

Joseph is fluent in Fullstack web development and has a lot of passion for DevOps engineering. He loves to write code and document them in blogs to share ideas.


Leave a Comment

Your email address will not be published. Required fields are marked *

Skip to toolbar