Hashnode/mern-starter

Hosting static assets and build artifacts using Docker volumes

andyfaizan opened this issue · 1 comments

This is a follow up question to my doubt with Nginx (Issue #151 ).

I am currently hosting a MERN based project with docker where I serve the contents of the dist folder. I would like to update the content being served by just changing / updating the dist folder and/or server.bundle.js (using docker volumes) but this fails to work. Is it possible to do the same or do I have to build and redeploy a new docker image everytime I change something?

Here is my Dockerfile (I use multi-stage builds)

FROM node AS build

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY package.json /usr/src/app
RUN npm install

COPY . /usr/src/app

ENV NODE_ENV production

RUN npm run clean && npm run build && npm run build:server

FROM node

RUN npm install pm2 -g

COPY --from=build /usr/src/app/node_modules /usr/src/app/node_modules
COPY --from=build /usr/src/app/dist /usr/src/app/dist
COPY --from=build /usr/src/app/package.json /usr/src/app/package.json
COPY --from=build /usr/src/app/index.js /usr/src/app/index.js
COPY --from=build /usr/src/app/client/index.js /usr/src/app/client/index.js
COPY --from=build /usr/src/app/server/refurbishment_mail /usr/src/app/server/refurbishment_mail
COPY --from=build /usr/src/app/server/ssl /usr/src/app/server/ssl

WORKDIR /usr/src/app
EXPOSE 8000
CMD ["npm", "run", "start:docker"] # start:docker uses pm2-docker to run node

And this is the docker-compose.yml

web:
  build: .
  ports: # the port mapping is for a test environment
  - "8000:8000"
  - "8001:443"
  volumes:
  - /usr/src/app/node_modules
  - ./dist:/usr/src/app/dist
  environment:
    NODE_ENV: production

I'm closing this until I have further tested it.