Skip to content

Commit

Permalink
feat(docker): dockerfile for production
Browse files Browse the repository at this point in the history
Added a multi-stage Production.Dockerfile to allow serving of a docker image
Added a Production.docker-compose to run and build the Production.Dockerfile
Updated the server.js to take ports from the environment
  • Loading branch information
DinoSourcesRex committed Nov 12, 2018
1 parent 6adbfed commit 4f850fe
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
35 changes: 35 additions & 0 deletions Production.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
FROM node:11.1.0 as npm_builder
# Set the entrypoint as bin bash incase we want to inspect the container
ENTRYPOINT ["/bin/bash"]
# Manually copy the package.json
COPY ./package.json /usr/src/app/package.json
COPY ./package-lock.json /usr/src/app/package-lock.json
COPY cypress /usr/src/app/cypress
# Set the work directory to where we copied our source files
WORKDIR /usr/src/app
# Install all of our dependencies
RUN npm install

FROM npm_builder as builder
# Copy the app excluding everything in the .dockerignore
COPY . /usr/src/app
# Put node_modules into the path, this will purely be used for accessing the angular cli
ENV PATH /usr/src/app/node_modules/.bin:$PATH
# Set the work directory to where we copied our source files
WORKDIR /usr/src/app
# Build our distributable
RUN npm run build:prod

FROM node:11.1.0 as production
# Copy the dist folder from builder
COPY --from=builder /usr/src/app/dist /usr/src/app/dist
COPY --from=builder /usr/src/app/server.js /usr/src/app/server.js
# Set the work directory to where we copied our source files
WORKDIR /usr/src/app
RUN npm install compression@1.7.3
RUN npm install express@4.16.4
# Create 2 empty environment variables
ENV CONTEXT=
ENV PORT=
# Run the node server which should be used for production
CMD ["node", "server.js"]
14 changes: 14 additions & 0 deletions Production.docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: '3.7'
services:
web:
image: persistentcontainerregistry.azurecr.io/brothinjord.client
container_name: b_client
build:
context: .
dockerfile: Production.Dockerfile
target: production
environment:
- CONTEXT=angular-ngrx-material-starter
- PORT=150
ports:
- '100:150'
6 changes: 3 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const express = require('express');
const compression = require('compression');

const CONTEXT = '/angular-ngrx-material-starter';
const PORT = 4000;
const CONTEXT = `/${process.env.CONTEXT || 'angular-ngrx-material-starter'}`;
const PORT = process.env.PORT || 4000;

const app = express();

app.use(compression());
app.use(CONTEXT, express.static(__dirname + '/dist'));
app.use('/', express.static(__dirname + '/dist'));
app.listen(PORT, () => console.log(`App running on localhost:${PORT}/${CONTEXT}`));
app.listen(PORT, () => console.log(`App running on http://localhost:${PORT}${CONTEXT}`));

0 comments on commit 4f850fe

Please sign in to comment.