How to handle seeding in production environment using Docker?

default discord avatar
Twoxic
last month
6

I'm wondering how to seed content (images and text) in a production environment I just set up using Docker. I'm using a multi-stage dockerfile that looks like this :



FROM node:18-alpine as base

FROM base as builder


WORKDIR /home/node
COPY package*.json ./

COPY . .
RUN yarn install
RUN yarn build

FROM base as runtime

ENV PAYLOAD_CONFIG_PATH=/home/node/dist/payload.config.js
ENV NODE_ENV=production

WORKDIR /home/node
COPY package*.json  ./

RUN yarn install --production

COPY --from=builder /home/node/dist ./dist
COPY --from=builder /home/node/build ./build

EXPOSE 3000

CMD ["node", "dist/server.js"]


In development to seed I use this in payload.config.ts:


onInit: async (payload) => {
    if (process.env.PAYLOAD_SEED) {
      await seed(payload);
    }
  }


However I don't think this would work in my production environment since I already build the code, so how would I tackle this? Thanks in advance!

  • default discord avatar
    thisisnotchris
    last month

    @Twoxic Hmm, I think that it will still read the env file post-build (could be wrong).



    Have you tried to see if it parses the env vars?

  • discord user avatar
    denolfe
    Payload Team
    last month

    Where/how are you deploying? I would expect you to be able to set env vars for your runtime environment completely separate from your Dockerfile.



    I'd expect any platform to have this ability, though can be a bit different between them

  • default discord avatar
    Twoxic
    last month

    I was under the impression that it would tree-shake certain seed code, however that doesn't really make sense on second thoughts.



    Got it to work, the only downside to this method is that after seeding you've got to re-deploy without the

    PAYLOAD_SEED

    environment variable; otherwise, in an event of a crash and reboot the application will attempt to reseed.



    One other thing: I used used some

    .png

    and

    .json

    files in my seed. I've had to add those to

    "copyfiles": "copyfiles -u 1 \"src/**/*.{html,css,scss,ttf,woff,woff2,eot,svg,jpg,jpeg,png,json,geojson}\" dist/",

    in my

    package.json

    .

  • discord user avatar
    denolfe
    Payload Team
    last month

    Correct, that is one downside. If you wanted this to be more robust, you could create some sort of collection that specifically stores whether a migration has run yet or not.

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.