Fastify not working in Docker

I’ve been using Express since it’s inception and while I’ve never really had any
issues with it, I’m also always up for messing around with something new. That
something new (to me at least) is Fastify.

A recent project of mine included building an extremely lightweight API to power
some widgets and Slack notifications. This seemed like a good time to play
around with Fastify and learn a bit about it.

While very similar to Express, it did feel like it was a bit lighter in terms of
syntax to get up and running. Since this project was something small in nature,
the speed improvements that Fastify touts probably won’t ever be noticed, let
alone fully realized.

Where I ended up hitting a wall with Fastify is when I attempted to Dockerize
the API I was building. Things ran just fine, but I wasn’t able to access the
API at all.

After some investigation, specifically running nmap to verify that the port in
question was in fact open, I started to think that perhaps it had something to
do with the IP address binding within Docker.

This is something I’ve run into previously with webpack, so I had a
good idea of where to start.

Fortunately, the fix was similar in nature and quite simple.

Inside of a Docker container, you need to explicitly bind some apps to the
address 0.0.0.0. To do so with Fastify, you just need to do the following.

Instead of doing this:

await fastify.listen({ port: 3000 })

You need to do this:

await fastify.listen({ host: '0.0.0.0', port: 3000 

And then you should be good to go to access your Dockerized Fastify service.

Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.


If you found this article helpful, please consider buying me a coffee.