Docker avec Nuxt js
Développement et Production
Production
Créer les fichiers Dockerfile
et docker-compose.yml
.
# syntax = docker/dockerfile:1
ARG NODE_VERSION=20
FROM node:${NODE_VERSION}-alpine as base
ARG PORT=3000
ENV NODE_ENV=production
WORKDIR /src
# BuildFROM base as build
COPY --link package.json package-lock.json .RUN npm install --production=false
COPY --link . .
RUN npm run buildRUN npm prune
# RunFROM base
ENV PORT=$PORT
COPY --from=build /src/.output /src/.output # Optional, only needed if you rely on unbundled dependencies # COPY --from=build /src/node_modules /src/node_modules
CMD [ "node", ".output/server/index.mjs" ]
version: "3"services: production: # Ajout de la plateforme de destination platform: linux/amd64 build: context: . ports: - "3001:3000"
Développement
# syntax = docker/dockerfile:1
ARG NODE_VERSION=20
FROM node:${NODE_VERSION}-slim as base
ENV NODE_ENV=development
WORKDIR /src
# BuildFROM base as build
COPY --link package.json package-lock.json .RUN npm install
# RunFROM base
COPY --from=build /src/node_modules /src/node_modules
CMD [ "npm", "run", "dev" ]
version: "3"
volumes: node_modules:services: development: build: context: . dockerfile: ./Dockerfile.dev ports: - "3000:3000" - "24678:24678" volumes: - .:/src - node_modules:/src/node_modules
Commandes
docker compose -f docker-compose.yml -f docker-compose.dev.yml up --build
Développement uniquement
Créer un fichier docker-compose.yml
et y ajouter le contenu suivant.
version: '3.3'
services: nuxt: build: context: . image: your-image-name container_name: your-container-name command: npm run dev volumes: - .:/app - /app/node_modules ports: - "3000:3000"
Créer un fichier Dockerfile
et y ajouter le contenu suivant.
# Le nom du fichier est `Dockerfile` sans extension.FROM node:20-alpine
WORKDIR /app
RUN apk update && apk upgradeRUN apk add git
COPY ./package*.json /app/
RUN npm install && npm cache clean --force
COPY . .
ENV PATH ./node_modules/.bin/:$PATH
Exécuter le conteneur :
docker-compose up