DEV Community

Discussion on: My Docker Setup For GatsbyJS (and NextJS)

Collapse
 
codefull profile image
Ernesto de la Cruz Guevara Ramírez

Hi, Here the configuration that worked for me.

FROM node:12.18.2-alpine3.12

# Install PM2 globally
RUN npm install --global pm2

#Install Gatsby CLI
RUN npm install -g gatsby-cli

RUN mkdir /app
WORKDIR /app
RUN chmod -R 777 /app

# Add node_modules to the path
ENV PATH /app/node_modules/.bin:$PATH

# Installing dependencies
COPY package*.json /app/
RUN cd /app/

RUN npm install

COPY . /app/

# Building app
RUN npm run build



# Run container as non-root (unprivileged) user
# The node user is provided in the Node.js Alpine base image
USER node

EXPOSE 8000

# Run npm start script with PM2 when container starts
CMD [ "pm2-runtime", "npm", "--", "serve" ]
Enter fullscreen mode Exit fullscreen mode

Then "serve" in package.json goes like this .

{
...
"scripts": {
...
serve: "gatsby serve -H 0.0.0.0 -p 8000",
...
},
...
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
fwahlqvist profile image
Fredrik Wahlqvist

Hey Ernesto et all thanks for sharing,
When trying to run this i get stuck with the following error

"Recreating frontend_web_1 ... done
Attaching to frontend_web_1
web_1 | 2020-11-14T02:58:15: PM2 log: Launching in no daemon mode
web_1 | 2020-11-14T02:58:15: PM2 log: App [npm:0] starting in -fork mode-
web_1 | 2020-11-14T02:58:15: PM2 log: App [npm:0] online
web_1 | Usage: npm
web_1 | where is one of:"

that repeats itself until i get
"web_1 | 2020-11-14T02:58:24: PM2 log: App [npm:0] exited with code [1] via signal [SIGINT]
web_1 | 2020-11-14T02:58:24: PM2 log: Script /usr/local/bin/npm had too many unstable restarts (16). Stopped. "errored"
web_1 | 2020-11-14T02:58:25: PM2 log: 0 application online, retry = 3
web_1 | 2020-11-14T02:58:27: PM2 log: 0 application online, retry = 2
web_1 | 2020-11-14T02:58:29: PM2 log: 0 application online, retry = 1
web_1 | 2020-11-14T02:58:31: PM2 log: 0 application online, retry = 0
web_1 | 2020-11-14T02:58:31: PM2 log: Stopping app:npm id:0
web_1 | 2020-11-14T02:58:31: PM2 error: app=npm id=0 does not have a pid
web_1 | 2020-11-14T02:58:31: PM2 log: PM2 successfully stopped"

Any ideas?
Many thanks