directus-labs / agency-os

The open source operating system for digital agencies. Built with Directus and Nuxt.
https://agencyos.dev
MIT License
552 stars 107 forks source link

dockerfile support #93

Closed johnykes closed 2 months ago

johnykes commented 4 months ago

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 commented 4 months ago

Just managed to successfully host it on Docker 😁

steps:

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 commented 2 months ago

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.