It provides us with a platform to orchestrate different microservices or systems while deploying. It serves a load of features to manage our resources efficiently. To containerise an application, we need a base image which can be used while creating an instance of the container.
Docker Hub is the centralised container image registry where we can host our images. These images can then be used for development.
Scope of the Article:
Here, we will look at the following things:
- Package application in a Docker container image.
- Host the image on Docker Hub.
- Set up a Kubernetes Cluster locally using MiniKube.
- Deploy the image on a locally running Kubernetes cluster.
For the purpose of this article, let's quickly create a sample application in Node.js
Create a directory for the application where all the files will reside.
mkdir hello-world
Now, we will create package.json
file to describe the app and its dependencies.
// package.json
{
"name": "docker_sample_app",
"version": "1.0.0",
"description": "Node.js on Docker",
"author": "akanksha priyadarshini",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.16.1"
}
}
Run npm install
to install all the dependencies for our app.
Create a server.js
file that will define our application.
// server.js
'use strict';
const express = require('express');
// Constants
const PORT = 8080;
const HOST = '0.0.0.0';
// App
const app = express();
app.get('/', (req, res) => {
res.send('Hello world');
});
app.listen(PORT, HOST);
console.log(`App is running on http://${HOST}:${PORT}`);
Now, let's create a file called Dockerfile.
A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
touch Dockerfile
In this file, first we will define from what we want to build the image from. Here, we will use node version 10 available from the Docker Hub.
FROM node:10
Next, we will specify the directory where our application code will reside inside the image. If the directory doesn't exist it will be created.
WORKDIR /home/node/app
Copy package.json and package-lock.json and install the dependencies.
COPY package*.json ./
RUN npm install
To bundle the app's source code inside the docker image, we can use COPY command.
COPY . .
Since our app binds to port 8080. We will expose this port to be mapped by docker.
EXPOSE 8080
Lastly, specify the command to run the application.
CMD [ "node", "server.js" ]
Final Dockerfile:
// Dockerfile
FROM node:10
WORKDIR /home/node/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "node", "server.js" ]
Let's create .dockerignore file.
// .dockerignore
node_modules
npm-debug.log
Before we move further, head on to Docker Hub, create an account if you don't already have one.
Let's create a repository named hello-world
here to keep our application's images to be used and shared.
Build the docker image.
// sample app directory where Dockerfile is present
cd hello-world
docker build -t <your username>/hello-world .
The -t
flag is used to tag the image so that it's easier to find.
Run the command docker images
to see the list of images. It should contain the image we built above.
Run the image locally and verify that everything is working fine.
docker run -p 8000:8080 -d <your username>/hello-world
-p
flag redirects a public port to a private port inside the container.
docker ps
will give the list of running containers images.
In this example docker mapped the 8080
port inside of the container to the port 8000
on our machine.
Test:
curl -i localhost:8000
We have successfully built and tested our docker image.
Let's push the locally created image to the repo we created on Docker Hub using the following commands.
// docker tag <local-image>:<tagname> <new-repo>:<tagname>
docker tag <your-username>/hello-world:latest <your-username>/hello-world:latest
// docker push <new-repo>:<tagname>
docker push <your-username>/hello-world:latest
We can check our repo on Docker Hub to verify the container image upload. In the tags section, we can see the recently pushed tag.
We can use these images for development and sharing.
Now, we will look at how to set up a Kubernetes cluster locally and deploy our application's docker image.
We will use Minikube to run Kubernetes cluster locally. You can check out how to install Minikube here.
Start minikube and create a cluster.
minikube start
You can check out more info about starting the cluster here.
To interact with the cluster, we need a command-line tool called kubectl . Install and set up kubectl from here.
Create a Kubernetes Deployment and use our application's docker image with it.
kubectl create deployment hello-kubernetes --image=<your-username>/hello-world
Output:
deployment.apps/hello-kubernetes created
To access the hello-kubernetes Deployment, we will expose it as a Service.
kubectl expose deployment hello-kubernetes --type=NodePort --port=8080
Output:
service/hello-kubernetes exposed
The pod hello-kubernetes
is now launched. It takes a few seconds for the pod to get to the running state.
You can run the following command to get the list of pods.
kubectl get pods
Get the url of the exposed service.
minikube service hello-kubernetes --url
Now, you can curl to the url and verify the deployment.
And we are done!
But you don't have to stop here. Continue to learn and test out different features supported by Kubernetes 😊
Originally Published on my website.
Top comments (0)