How to handle seeding in production environment using Docker?

default discord avatar
7 months ago

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


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

    @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
    Payload Team
    7 months ago

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

    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


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

    One other thing: I used used some




    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



  • discord user avatar
    Payload Team
    7 months ago

    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!


Connect with the Payload Community on Discord



Can't find what you're looking for?

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