Skip to main content

Dockerfile Template for Nuxt.js Framework

This guide helps you create suitable Dockerfile for Nuxt.js-based projects.

Key Points for All Dockerfiles

  1. Use official Node.js images
  2. Create a non-root user for enhanced security
  3. Use environment variables for greater flexibility
  4. Optimize for each specific framework
FROM node:$node_version

ARG UID=1000
ARG GID=1000

ENV PORT=3000
ENV HOSTNAME=0.0.0.0

RUN groupadd -g $GID -o app && \
useradd -g $GID -u $UID -mr -d /home/app -o -s /bin/bash

USER app
WORKDIR /home/app
COPY --chown=app:app app/ /home/app/

RUN npm install

# RUN generate
# CMD [ "HOSTNAME=0.0.0.0", "PORT=$PORT", "npm", "run", "start" ]
#or
# RUN npm run build
# CMD [ "node", ".output/server/index.mjs" ]
info
  • Two options for execution are provided: using npm run start or directly executing the server output file.
  • Set the PORT variable, which can be replaced in the advanced settings or project dashboard in the PaaS.
caution
  • Replace $node_version with the appropriate Node.js version.
  • Depending on the project needs, choose one of the two CMD options and uncomment it.
  • If using static mode, uncomment the RUN generate command.

Building and Running the Dockerfile

To build the Dockerfile and create an image, use the command:

docker build -t [ProjectName] .
  • -t: Assign a specific tag and name for the project

Then, to run it:

docker run -p outport:inport[3000] --name=container-name -d [ProjectName]
  • -p: Specify the port for running the project
  • outport: External port
  • inport: Internal port of the project specified in the Dockerfile
  • --name: Specify the container name
  • -d: Run the container in detached mode
caution
  1. Security: Always use the latest stable versions of Node.js and Nginx.
  2. Testing: Before deploying to production, make sure to test your Docker image in a production-like environment.