mmornati / docker-ghostblog

Ghost Blog Docker Container
MIT License
10 stars 6 forks source link

[BRAINSTORMING] Optimise the multi-stage Dockerfile build further more #27

Closed pascalandy closed 5 years ago

pascalandy commented 6 years ago

This is the idea.

4 stages

  1. base
  2. dependencies
  3. build
  4. release

Inspired by: https://blog.hasura.io/an-exhaustive-guide-to-writing-dockerfiles-for-node-js-web-apps-bbee6bd2f3c4

# ---- Base Node ----
FROM node:carbon AS base
# Create app directory
WORKDIR /app

# ---- Dependencies ----
FROM base AS dependencies  
# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./
# install app dependencies including 'devDependencies'
RUN npm install

# ---- Copy Files/Build ----
FROM dependencies AS build  
WORKDIR /app
COPY src /app
# Build react/vue/angular bundle static files
# RUN npm run build

# --- Release with Alpine ----
FROM node:8.9-alpine AS release  
# Create app directory
WORKDIR /app
# optional
# RUN npm -g install serve
COPY --from=dependencies /app/package.json ./
# Install app dependencies
RUN npm install --only=production
COPY --from=build /app ./
#CMD ["serve", "-s", "dist", "-p", "8080"]
CMD ["node", "server.js"]
mmornati commented 6 years ago

Cool. But I don't get the need of the base step with only the app directory creation... If then it is created even in all other steps. The base can maybe the step you named dependencies?

But yeah... I think it is easy to understand the docker file in this way and we can maybe gain some other bytes :)

pascalandy commented 6 years ago

I didn't give it a real thought yet. But as a template, I think it makes sense.

Don't know yet if it applies to our project thought :p If it sparks something in our heads cool. Else just drop it :p

sr229 commented 6 years ago

I think we can further clean the Dockerfile for readability since @mmornati suggested using USER for commands that require to be ran on node on this Pull Request