Staged Docker builds for dev and prod

This commit is contained in:
Erki Aas 2022-10-11 16:24:15 +03:00
parent dde5a13550
commit 693ab863c2
5 changed files with 35 additions and 9 deletions

View File

@ -1,5 +1,5 @@
# pull official node image # pull official node image
FROM node FROM node AS dev
# define /app as working directory # define /app as working directory
WORKDIR /app WORKDIR /app
@ -13,4 +13,10 @@ RUN npm install
COPY . /app COPY . /app
# launch node server # launch node server
ENTRYPOINT npm run dev
# production
FROM node AS prod
WORKDIR /app
COPY --from=dev /app ./
ENTRYPOINT npm run start ENTRYPOINT npm run start

View File

@ -5,8 +5,8 @@
"main": "server.js", "main": "server.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon server.js -e ejs,js,css,html,jpg,png,scss", "dev": "nodemon server.js -e ejs,js,css,html,jpg,png,scss",
"build": "cd frontend && npm install && npm run build" "prod": "node.js server.js"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",

View File

@ -1,9 +1,22 @@
FROM node:16-alpine FROM node AS dev
WORKDIR /app WORKDIR /app
EXPOSE 8080 EXPOSE 8080
CMD ["npm", "run", "dev"]
COPY package* ./ COPY package* ./
RUN npm install RUN npm install
COPY . . COPY . .
ENTRYPOINT ["npm", "run", "dev"]
# builder
FROM dev AS builder
RUN npm run build
# serve
FROM nginx AS prod
WORKDIR /usr/share/nginx/html
RUN rm -rf ./*
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=builder /app/dist .
ENTRYPOINT ["nginx", "-g", "daemon off;"]

9
frontend/nginx.conf Normal file
View File

@ -0,0 +1,9 @@
server {
listen 8080;
server_name _;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}

View File

@ -30,8 +30,7 @@ profiles:
- image: harbor.k-space.ee/playground/log-viewer-backend - image: harbor.k-space.ee/playground/log-viewer-backend
context: backend context: backend
docker: docker:
buildArgs: target: dev
ENV: development
sync: sync:
manual: manual:
- src: '**/*.js' - src: '**/*.js'
@ -39,8 +38,7 @@ profiles:
- image: harbor.k-space.ee/playground/log-viewer-frontend - image: harbor.k-space.ee/playground/log-viewer-frontend
context: frontend context: frontend
docker: docker:
buildArgs: target: dev
ENV: development
sync: sync:
manual: manual:
- src: 'src/**/*.vue' - src: 'src/**/*.vue'