DEV Community

Cover image for How to Dockerize and Deploy Express.js APIs

How to Dockerize and Deploy Express.js APIs

Jonas Scholz on October 24, 2024

If you're just here to copy and paste, here's the final Dockerfile that will produce an image for your Express.js app: FROM node:22.10.0-alpine....
Collapse
 
tinkermakar profile image
Makar

it's also a good practice to switch add USER node in the Dockerfile, so as your in-docker runtime was not the root user. If I am not mistaken, you also need to run RUN chown -R node /app so as that node user had write access to the app.

Collapse
 
code42cate profile image
Jonas Scholz

Yes! 100% true

Collapse
 
arindam_1729 profile image
Arindam Majumder

Great One Jonas!

Collapse
 
code42cate profile image
Jonas Scholz

Thanks:)

Collapse
 
wimadev profile image
Lukas Mauser

Good one! :-) Any thoughts on express vs h3? 👀

Collapse
 
code42cate profile image
Jonas Scholz

I don't think I've ever used h3, but the website is sleek so it must be good xD

Collapse
 
saqib_diar profile image
Saqib Diar

One basic question always bother me.

Let's say I want to deploy my NodeJs project on hostinger vps, so without using docker approach I will install NodeJs MySQL express etc and then I will pull code from my repo and install dependency and then start pm2.

But Now let's say my manager asked me to implement docker, I will create the docker image then container and then let say I deployed that container, now what?

Now that container contain my entire code?

Like I won't need pull from the GitHub to bring my whole code into my VPS?

Or I would just run docker there?

Means I won't be having code on server? Just docker file or what?

Collapse
 
inspiraller profile image
steve • Edited

I had the same question when I first was introduced to docker. What is it good for? As a front end developer doing react its not really important. As a full stack developer it opens doors.

Question:
"Now that container contain my entire code?"

Answer:
Yes and no.

Once you build your docker image, think of that like your build of a computer operating system with all its dependencies. You aren't going to build a whole windows operating system with hardware drivers, microsoft office, email etc. You are building the equivalent of a node dist folder ( but - for full stack, not just front end code ). The bare minimum of production ready dependencies.

1 Dockerfile = build image
You might build nginx and react app in one Dockerfile because nginx is going to serve the static build content of your react production code.

2 Dockerfiles = 2 build images, that will communicate with each other over docker network.

You might build nextjs server which runs on port 3000 and nginx which runs on port 80. Nginx is reverse proxy to nextjs server because they run on separate ports. Here you have two separate Dockerfiles images built but a docker compose file which works them together, or if you were deploying these to a cloud service like aws, intead of a docker compose file you have a task definition file respectively.

Answer yes:
Yes it contains your build image.

Yes it does once you built your image:
docker build myproject . -t myproject:versionX

myproject:versionX contains all your "BUILD" code per Dockerfile. That can be deployed to the docker registry.

Answer no:
That being said, all that above is your deployed code. You still need to store your development code.

Question:
"Like I won't need pull from the GitHub to bring my whole code into my VPS?"

No. You may have built your production code and stored that in docker registry or other cloud hosting space like amazon ecr, but you have not stored your development code. Ie your Dockerfile and all your development code. That all needs to be under revision control.

Question:
So why use docker at all?

Dockerfile does a few things:

  1. It is instruction file for any os (linux, windows whatever) in whole os dependencies for your application to work. Don't assume your implementation on windows will work on linux based environment and vice versa.
  2. Much like nodejs and npm can start up a script by: npm run myscript -- --someEnvVar value, docker does similar docker run myImage env someEnvVar myImg:version
  3. Much like you can deploy your optimised build nodejs dist/ folder to a website host, you can deploy your entire full stack infrastructure to a cloud host like aws. Ie that's your postgres db, nginx reverse proxy to nodejs, react app and any other service built from multiple Dockerfiles into production ready, versioned builds but all integrated.

I hope I answered the question.

Collapse
 
code42cate profile image
Jonas Scholz

nice reply! thanks for the long explanation, i hope that helps Saqib:)

Collapse
 
code42cate profile image
Jonas Scholz

Hi! Your Dockerfile will basically be the description of the steps you did without docker. You would install Nodejs (using a baseimage), copy the code, install dependencies etc the same, but Docker would do that when you build your image. Your image will then contain the final state of your files basically, which you can use to then run a container. Does that make sense?

Collapse
 
marco45palomo profile image
Marco Palamede

Nicely done Jonas, but can you deploy some docker-compose file on sliplane?

Collapse
 
code42cate profile image
Jonas Scholz

Thank you, no not yet!

Collapse
 
karthick_rajam_43e22ca31 profile image
karthick Raja M

Nice content

Collapse
 
code42cate profile image
Jonas Scholz

Thanks:)

Collapse
 
thecodingthesi profile image
Thesi

hono > express

Collapse
 
code42cate profile image
Jonas Scholz

fair