Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

How to handle seeding in production environment using Docker?

default discord avatar
twoxic2 years ago
4

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
    notchr2 years ago
    @399871533276987392

    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
    2 years 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
    twoxic2 years 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

    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
    2 years 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.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.