Hosting Nodejs App using your own Docker Image in Jelastic PaaS/ Yeti Cloud

Bipin Budhathoki
4 min readJan 10, 2022

Hello Everyone, welcome back. It’s me Bipin.

Today I will show you how to host your node js application using your own docker image. Hosting an application using docker image is really simple and can solve many problems that can occur in a production environment.

Intro Image

So let’s begin with the introduction of Docker

Before knowing what docker is lets see the problem statement that docker solves so that we can understand docker easily. In tech organizations there are different teams who handle different operations like frontend, backend, QA and other. Different people have different OS (Operating System) in their computers. So while exchanging codes or pushing it from dev environment to staging environment it won’t work as expected.

And the famous quotes comes:

“ IT WORKS ON MY COMPUTER ”

This is one of the famous quotes among programmers or developers. This problem can be solved using a docker container. Irrespective of your OS docker container runs same on all enviroment. So Docker uses OS level Virtualization to deliver software in packages called containers.

We can write and talk about docker all day long but this is not a blog all about Docker but about how to host the docker image to our jelastic Paas environment. If you want to learn more about docker you can learn from this link.

Now I will show you the step by step process to host an application using Docker Image.

Step 1: Setup a nodejs application

You need to setup a node js project or use your existing node js project.

Step 2: Setup Dockerfile

  • Create a file name Dockerfile and .dockerignore in the root of your project.
  • In .dockerignore add files that need to be ignored like .env, node_modules

So i will give you a sample Dockerfile common for node js app.

FROM node:14 as dependencies # Use node:14 image as base and name or tag it as dependencies# Create app directory
WORKDIR /root/ROOT # Specify the folder where project lies, it will be created for you if not exists
COPY package.json ./ # Copy a package.json file to the container
RUN npm install # Install dependency from package.json
FROM node:14 as runner # Use node:14 image as base and name or tag it as runner
WORKDIR /root/ROOT # Specify the folder where project lies, it will be created for you if not exists
COPY . . # Copy all the files anf folder from project to container
COPY --from=dependencies /root/ROOT/node_modules ./node_modules # Copy node module we have installed in dependencies
ENV NODE_ENV production #SET ENV VARIABLES
ENV PORT 3000
EXPOSE 3000
# Expose port so that outside world can acces the port of container
RUN chmod +x entrypoint.sh # Make script file readable if available otherwise remove these two lines
ENTRYPOINT ["./entrypoint.sh"] # Script File if something need to be done before application starts like database migration
CMD ["npm","start"] # Start Application Command

I have explained each line in the comment so you can understand what each line does.

Step 3: Build Dockerfile

  • You need to install docker on your local machine to follow these steps
  • To build Docker Image we run command from root of the project
docker build -t tag-name . 
  • If error occurs during build fix error and build again

Step 4: Push Docker Image

You need to create a docker hub account or use other services like AWS ECR for these steps.

docker login # It will ask username and passworddocker push your-username/image_name

Then the image will be uploaded to docker hub so we can access it from jelastic.

Step 5: Into Jelastic Dashboard

  • Goto Dashboard -> Click New Environment
  • Goto Custom
Create Enviroment Custom
  • Click Select Image
Select Custom Container
  • Click Add New Image
Add Docker Image
  • Enter your image-name or url, username and password of docker hub or any other repository you are using
  • Click Add
Select Docker Image

Select your image and click Next

You can change other settings, name your environment and click create to create your environment.

Waahla!! Now your server is ready.

If you want to learn more about hosting in Jelastic paas i have another blog too where it covers details and basics about jealastic and its environment configurations.

I am new in this writing world so your feedback is appreciated and any other doubts and questions are welcome.

--

--