Deploy NodeJS app to Kubernetes using docker-compose — part-1

GORAKH CHAVAN
3 min readDec 13, 2021

--

To deploy the NodeJS app you need a development environment setup.

So beginning with installing all the dependencies Docker, Minikube, Kompose. yep, that’s it but please make sure you install them in the sequence they are provided as they are dependent on each other. I assume you are familiar with these technologies a bit. Great! let's get started.

First of all, let's clone the repository so that we all are on the same page. here is the GitHub link for TrainingDemo.

This is our folder structure. we will deploy the server as a separate deployment and the client as a separate one. so we need to dockerize them separately.

create Dockerfile inside a server folder using the following command and paste the below content.

touch server/Dockerfile

here there are a few terms
FROM: this param defines which node package you want to build an image from.
ENV: this tag is used to set environment variables.
WORKDIR: this tag defines, inside which folder or directory should your app reside.
COPY: this command copies the dir mentioned in front of him.
RUN: it runs the particular command in the container
EXPOSE: this exposes the port to the external world from which we can access our application.
CMD: this tag runs after we run a container.

So, no need to worry it often stays the same for NodeJS particularly, and have to make some changes here and there. so basically this file tells docker that you are going to grab the node image of version 10 and set the environment variable of NODE_ENV to staging then it sets /app as a working directory inside the image for the application. after that, it copies the package.json file and runs the command npm install. then another copy command copies all the files and pastes them inside the image. at last, it exposes port 8080 and stores CMD value to execute later in the container.

Similarly, create Dockerfile in the client and paste the following content.

touch client/Dockerfile

As you might have noticed not much has changed. just the port number and start command. so now we are ready with the docker file, now let's create the docker-compose file.

create docker-compose.yml inside the root folder and paste the code given below. now the final folder structure looks like this.

touch docker-compose.yml

and our docker-compose files would look like this, to read more about docker-compose click here.

We have to take indentation seriously for docker-compose files. here we are just creating the services for client and server. build specifies the path to the DOCKER file of the client/server, thus providing the image name after the build is performed. and exposing the port again accordingly and our docker-compose file is ready.

Building Kubernetes manifest is tough and complex. so we are going to use a shorthand for it Kompose. this tool simplifies our work by creating a manifest file from the docker-compose file. with the help of simple commands.

kompose convert -f docker-compose.yml -o kubemanifest.yaml

Here, Kompose is taking the docker-compose.yml file and converting it to a manifest file inside a file kubemanifest.yaml. wasn't that simple!

After the conversion of the docker-compose file the kubemanifest.yaml would look like this. if you have any issues you can reach me out on Linkedin or Instagram

--

--