DEV Community

Cover image for How to Dockerize and Deploy Fastify APIs
Jonas Scholz
Jonas Scholz Subscriber

Posted on

How to Dockerize and Deploy Fastify APIs

If you're just here to copy and paste, here's the final Dockerfile that will produce an image for your Fastify app:

FROM node:20-alpine
WORKDIR /app
COPY package* ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "index.js"]
Enter fullscreen mode Exit fullscreen mode

And here's the .dockerignore file you should use:

node_modules
npm-debug.log
Enter fullscreen mode Exit fullscreen mode

To build and run the image, use these commands:

docker build -t fastify-app .
docker run -p 3000:3000 fastify-app
Enter fullscreen mode Exit fullscreen mode

Not just here to copy and paste? Let's go over what's happening in the Dockerfile!

let's do this

The Setup

For this tutorial, I assume you have a Fastify project set up. Fastify is a web framework highly focused on providing the best developer experience with the least overhead and a powerful plugin architecture. If you have a different setup, you might need to adjust the Dockerfile accordingly.

Typically, you'd run npm install and then node index.js to work locally. For deployment, we'll use a similar approach but within a Docker container. Let's dive into the details of the Dockerfile.

The Dockerfile

FROM node:20-alpine
LABEL maintainer="jonas@sliplane.io"
WORKDIR /app
COPY package* ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "index.js"]
Enter fullscreen mode Exit fullscreen mode

So what's happening here?

  1. Base image:

    • Uses Node.js 20 on Alpine, providing a lightweight base image.
  2. Working directory:

    • Sets up /app as the working directory for subsequent instructions.
  3. Dependency installation:

    • Copies package.json and package-lock.json (if it exists) to the working directory.
    • Runs npm install to install dependencies.
    • This makes caching possible!
  4. Application code:

    • Copies the rest of the application code into the container.
  5. Port exposure:

    • Exposes port 3000, which we'll use for our Fastify application.
  6. Start command:

    • Specifies the command to run the application using node index.js.

This approach is straightforward and efficient for Fastify applications, which typically don't require a separate build step. If yours does, simply add a RUN npm build in between. Also make sure to add the .dockerignore file to ignore the node_modules folder and npm debug logs. This will speed up the build process and reduce the image size.

The Minimal Fastify JavaScript File

Here's a minimal index.js file to run Fastify with the host set to 0.0.0.0:

const fastify = require("fastify")({ logger: true });

fastify.get("/", async (request, reply) => {
  return { hello: "world" };
});

const start = async () => {
  try {
    await fastify.listen({ port: 3000, host: "0.0.0.0" });
  } catch (err) {
    fastify.log.error(err);
    process.exit(1);
  }
};

start();
Enter fullscreen mode Exit fullscreen mode

Setting the host to 0.0.0.0 is necessary in Docker because it allows the application to accept connections from any IPv4 address. If you don't set this, the application will only be accessible from within the container, not from the host machine or external networks.

Deployment

You can deploy this Docker container to any cloud provider that supports Docker. For example, you could use platforms like Heroku, DigitalOcean, or AWS ECS.

Because I am the co-founder of Sliplane, I will show you how to deploy it there. After signing up, you can create a new service by selecting your Github Repository. Then just keep the default settings and click on deploy.

Sliplane Service Create

After deployment, your Fastify app will be available under a subdomain of sliplane.app, usually it's just your service name. You can also see the logs of your app, see metrics such as CPU and memory usage, add persistent storage, and much more. Whenever you push to your repository, Sliplane will automatically deploy your app.

If you want to try out Sliplane, the first 2 days are free! Try it out and let me know what you think :)

Deploy Fastify in 2 Minutes 🚀

Next Steps

Is there anything else you want to know? Do you need help dockerizing your Fastify app? Do you need help deploying it to a specific platform? Feel free to reach out! You can find me on X or just comment here on this blog.

Cheers,
Jonas

Top comments (5)

Collapse
 
wimadev profile image
Lukas Mauser

any thoughts on fastify vs hono? 👀

Collapse
 
mk0wan profile image
Victor Mk

Super new to the fastify ecosystem, my honest opinion is that it's way easier to setup and manage different plugins compared to express. Middleware and Authentication process are made super easy.

Collapse
 
code42cate profile image
Jonas Scholz

yes! 100% agree

Collapse
 
code42cate profile image
Jonas Scholz

i personally love hono, but the fastify ecosystem is pretty good and hono still so new, need to see where it goes!

Collapse
 
thecodingthesi profile image
Thesi

missed an opportunity to make a fast(ify) pun in the title!