Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dockerfile support #93

Closed
johnykes opened this issue May 21, 2024 · 2 comments
Closed

dockerfile support #93

johnykes opened this issue May 21, 2024 · 2 comments

Comments

@johnykes
Copy link

Hi guys,

Sorry for bothering.

This repo is 🚀

I would also love to host it on docker. I tried creating a docker file, but I can't make the build part work.

I always get this error
Error: Cannot find module '/app/node_modules/nuxt/bin/nuxt.mjs'

ARG NODE_VERSION=18.14.2
FROM node:${NODE_VERSION}-slim as base

WORKDIR /app

# Install pnpm globally
RUN npm install -g pnpm@8.6.0

# Copy package files
COPY package.json pnpm-lock.yaml ./

# Install dependencies
RUN pnpm install

# Debug: List node_modules to check if nuxt is installed
RUN ls -la node_modules/nuxt
RUN ls -la node_modules/.bin

# Copy all source files
COPY . .

# Build the project
RUN pnpm build

# Prune development dependencies
RUN pnpm prune --prod

# Define the final stage for the production image
FROM node:${NODE_VERSION}-slim as production

WORKDIR /app

# Set environment variables
ARG PORT=3000
ENV PORT=$PORT

# Copy built files and production dependencies from the build stage
COPY --from=base /app/.output /app/.output
COPY --from=base /app/node_modules /app/node_modules

# Expose the port
EXPOSE $PORT

# Command to run the application
CMD ["node", ".output/server/index.mjs"]

Any idea?

@johnykes
Copy link
Author

johnykes commented May 22, 2024

Just managed to successfully host it on Docker 😁

steps:

  • replace the deprecated nuxt-simple-sitemap with @nuxtjs/sitemap

  • uncomment in nuxt.config.ts

	routeRules: {
		'/**': {
			prerender: true,
		},
	},
  • dockerfile
# build stage
FROM node:18-alpine as build-stage
WORKDIR /app

COPY package.json .
COPY pnpm-lock.yaml ./

RUN apk update
RUN apk add --no-cache git

RUN npm install -g pnpm@8.6.0

RUN pnpm install

COPY . .

# ENV
ENV DIRECTUS_URL=${DIRECTUS_URL}
ENV DIRECTUS_SERVER_TOKEN=${DIRECTUS_SERVER_TOKEN}
ENV SITE_URL=${SITE_URL}

RUN NODE_OPTIONS="--max-old-space-size=4096" pnpm build

# production stage
FROM node:18-slim as production

COPY --from=build-stage /app/.output /app/.output
COPY --from=build-stage /app/node_modules /app/node_modules

EXPOSE 3000

CMD ["node", "/app/.output/server/index.mjs"]
  • .dockerignore (not working without this, not sure why :) )
node_modules
npm-debug.log
Dockerfile*
docker-compose*
.dockerignore
.git
.gitignore
README.md
LICENSE
.vscode
  • build

docker build -t test-website --build-arg DIRECTUS_URL='http://host.docker.internal:8055' --build-arg DIRECTUS_SERVER_TOKEN='...' --build-arg SITE_URL='http://localhost:3000' --build-arg NODE_OPTIONS="--max-old-space-size=4096" .

  • start docker container

docker run --name test-website --network directus_default -p 3000:3000 -e "DIRECTUS_URL=http://localhost:8055" -e "DIRECTUS_SERVER_TOKEN=..." -e "SITE_URL=https://your_domain" --rm test-website

PS: in production, the DIRECTUS_URL & SITE_URL can't be localhost
PS 2: in .directus/docker-compose.yml you need to update CORS, else, the forms won't work
CORS_ENABLED: 'true'
CORS_ORIGIN: 'https://mywebsite.com,http://localhost:3000'
CORS_METHODS: GET,POST

@ComfortablyCoding
Copy link
Contributor

ComfortablyCoding commented Jul 18, 2024

Thanks for the info and glad you got it working! As I do not see any issue outlined here I am going to close this for now, happy to re-open if needed.

@ComfortablyCoding ComfortablyCoding closed this as not planned Won't fix, can't repro, duplicate, stale Jul 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants