Last week, I posted an article that contained a Dockerfile that I have been using for my React apps. I used a Docker pattern that I have been using for a long time, despite knowing it’s not actually fully correct. But my rebuild times have always been fast and I just feel like it makes a container more readable when you expose the port at the very end of the Dockerfile.

But that weighed on me and I started thinking about it, so started doing some testing. And while rebuilding this image is very fast regardless, exposing the port earlier on does make better use of Docker caching so the impact on restart times are significant. Based on my testing (i9 processor + 32GB of RAM), exposing the port earlier on saves an average of almost 16% per rebuild. This only works out to around half a second of difference on my machine and nobody is going to get a performance bonus for saving milliseconds, but it’s still significantly better.

In the future, when I know that something isn’t exactly right, I’ll test it before I publish. I’m sorry for my oversight. Both the original and this document are corrected.

# syntax=docker/dockerfile:1.4

FROM node:lts-slim AS development
WORKDIR /code

# Expose the application port
EXPOSE 3000

COPY package.json package-lock.json ./

# Install dependencies
RUN npm ci

# Copy the rest of the application code
COPY . .

          

Thanks for reading!

Return to top.