iconDirEasy
Deployment

Docker

Learn how to deploy DirEasy as a Docker container.

In this guide we are going to show you how to deploy your DirEasy application as a Docker container, so you can deploy it to any platform or server that supports docker images.

Why deploy your app as a Docker container?

Deploying your app as a Docker container allows you to have complete control over your server environment. It ensures better privacy, contributes to cost savings if managed correctly, and offers you the flexibility to customize your server setup to suit your specific needs. It can also give your application a performance boost compared to hosting on a serverless platform like Vercel, as it removes cold starts.

Setup Next.js app for docker deployment

To get started we first need to configure our Next.js app to be built as a standalone app, so we can later run the application in a docker container. To do so, add the following next.config.ts file to your project:

nextConfig.output = 'standalone';

Setup docker configuration for the Next.js app

Next, create a Dockerfile in the root of your codebase (Dockerfile) with the following content:

Dockerfile
FROM node:20-alpine AS base
FROM base AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json pnpm-lock.yaml* ./
RUN corepack enable pnpm && pnpm i --frozen-lockfile
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN corepack enable pnpm \
  && pnpm build
FROM base AS runner
WORKDIR /app
 
ENV NODE_ENV production
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
 
COPY --from=builder /app/public ./public
RUN mkdir .next
RUN chown nextjs:nodejs .next
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
 
USER nextjs
 
EXPOSE 3000
 
ENV PORT 3000
ENV HOSTNAME "0.0.0.0"
CMD ["node", "server.js"]

In the root of the DirEasy project, also add a .dockerignore file with the following content:

.dockerignore
Dockerfile
.dockerignore
node_modules
**/node_modules
npm-debug.log
README.md
.next
.git

And that's all the changes necessary! Now you can build your app as a docker image and deploy it anywhere you can run docker images.

Running your app locally with Docker

If you have Docker installed on your local machine and want to run your Next.js app there for testing the docker image, simply run the following commands from your project’s root:

Terminal
docker build -f Dockerfile . --no-cache -t direasy-docker
docker run -p 3000:3000 direasy-docker

To fully use the application, you need to pass the necessary environment variables you have defined in your .env file to the container.

Now you can deploy your app to any server that supports docker images.

On this page