Dockerfile from documentation causing errors

default discord avatar
toms0
8 months ago
29

Hey everyone,



I'm using the Dockerfile from the documentation (

https://payloadcms.com/docs/production/deployment#docker

) and it's causing errors on Northflank, see attached. Docker isn't really my strong point so any advice would be great!



I think I understand it, it's because it's doing

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

and not

CMD ["yarn", "run", "serve"]

which sets the

PAYLOAD_CONFIG_PATH

in the script.



I'm curious to why it is setup this way? Is it for more control over dev vs production? In my use-case I'm deploying my codebase so the config stays in the same location so it may just be a case of not relevant to me.



Actually that doesn't completely work either, I'm going to go back to using the Dockerfile provided by Northflank for now, but this may need looking at in the documentation.

  • discord user avatar
    denolfe
    Payload Team
    8 months ago

    Hey @toms0, I tested and wrote the Dockerfile from the template. I can give it another once over.



    It was done w/o yarn because we wanted to support both npm and yarn.



    Really, the Dockerfile that ships w/ the template is highly coupled with the code in the template itself



    The main problem I had with NorthFlank's Dockerfile was that it was copying

    everything

    from the src dir, which technically isn't secure.



    # Copy the built application.
    COPY --from=build /home/node /home/node

    This line copies everything from the workdir, which would also bloat the image size



    Seems to run fine for me on a fresh create-payload-app project



    Ahhh, I see the problem.



    Looks like the docs are missing setting of PAYLOAD_CONFIG_PATH, but it is set in the create-payload-template. Thanks for the heads up @toms0 👍



    Maybe this needs to be more prominent in the docs


    This is an example of a multi-stage docker build of Payload for production. Ensure you are setting your environment variables on deployment, like PAYLOAD_SECRET, PAYLOAD_CONFIG_PATH, and MONGODB_URI if needed.
  • default discord avatar
    toms0
    8 months ago

    This makes sense to me, I think yarn should be opt-in now opt-out since npm comes with every node install and yarn doesn't with additional configuration. I did in fact use the

    PAYLOAD_CONFIG_PATH

    in my second deploy but it gave a different error.



    Which is this one, I'm not entirely sure what it's trying to tell me however.



    It seems even with the PAYLOAD_CONFIG_PATH copied over, it's failing to find it because in the Dockerfile, it never copies the src folder. I'll do a local build would it be a case of doing

    PAYLOAD_CONFIG_PATH=dist/payload.config.ts

    ?

  • discord user avatar
    denolfe
    Payload Team
    8 months ago

    If you're point to dist/ you'll need to do payload.config.js



    or add a step to copy over the ts file and point at that

  • default discord avatar
    toms0
    8 months ago

    Hmm, I'm unsure if I'm understanding correctly.

  • discord user avatar
    denolfe
    Payload Team
    8 months ago

    You'll notice this mirrors what is in package.json for

    serve


    cross-env PAYLOAD_CONFIG_PATH=dist/payload.config.js NODE_ENV=production node dist/server.js
  • default discord avatar
    toms0
    8 months ago

    Yeah, that is what I did, strange.



    Got it working, thanks Elliot, I thought I had copied over the dist/payload.config.js but it turns out I copied the src/payload.config.js from the

    generate:types

    by accident.

  • discord user avatar
    denolfe
    Payload Team
    8 months ago

    Great, glad we were able to figure it out 👍

  • default discord avatar
    teroonsimckooni
    7 months ago

    @denolfe is it safe to add the PAYLOAD_SECRET, PAYLOAD_CONFIG_PATH etc. to the dockerfile in the same way as:


    ENV NODE_ENV=production

    ?

  • discord user avatar
    denolfe
    Payload Team
    7 months ago

    PAYLOAD_CONFIG_PATH is safe



    However, PAYLOAD_SECRET should be set at runtime in your deployment environment



    You typically would not bake sensitive values into a docker image



    Most deployment platforms let you set these externally to the image



    For running locally if you use dotenv, you can do something like this

    docker run --env-file .env -p 3000:3000 imagename
  • default discord avatar
    teroonsimckooni
    7 months ago

    Makes sense, thank you!

  • default discord avatar
    generator101
    3 months ago

    @toms0 can you share your docker file and docker yml file? is it it same as the one in the doc? I cant get docker working too

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.