ホーム > React > When Gatsby.js + Docker Local Server Commands Are a Hassle
React

When Gatsby.js + Docker Local Server Commands Are a Hassle

Thank you for your continued support.
This article contains advertisements that help fund our operations.

This post explains how to set up a more convenient workflow when local server commands become tedious in a Gatsby.js + Docker environment.

Conclusion

FROM node:20-alpine
WORKDIR /home/node/app
RUN apk update && \
    apk add git yarn build-base autoconf automake libtool pkgconfig nasm && \
    npm install -g gatsby-cli
EXPOSE 8000 9000

CMD ["gatsby", "develop","--host=0.0.0.0"]

Background

I used to always enter the container and run the local server command manually, like this:

docker compose exec app sh
gatsby develop --host=0.0.0.0

Typing these commands twice every time became a hassle, so I improved the process.

How to Write the CMD

The correct way to write the CMD is:

CMD ["gatsby", "develop","--host=0.0.0.0"]

I tried writing it like this:

CMD ["gatsby", "develop --host=0.0.0.0"]

But it didn’t work. Apparently, you need to write each option as a separate array element. So much to learn!

When You Need to Rebuild

There are times when Gatsby.js requires a rebuild. In those cases, I stop the docker container like this:

docker compose down
docker compose up -d

You can check the build logs using Docker Desktop.

Summary

There are pros and cons to this setup, but I plan to run it this way for now. If you have a local server that you can leave running, this might not be as effective, but if you’re starting it once a day, you might save 10 seconds a day (give or take).

I always felt it was awkward to enter the container and run the server manually, so this approach feels like an improvement.

Please Provide Feedback
We would appreciate your feedback on this article. Feel free to leave a comment on any relevant YouTube video or reach out through the contact form. Thank you!