-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDockerfile.prod
57 lines (38 loc) · 2.96 KB
/
Dockerfile.prod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# I need to run "npm install" to install all dependencies, then "npm run build" (to create a production build in the /dist folder), then "npm prune --production" (to remove `devDependencies` packages) and then copy over only the node_modules directory (which contains the production-only packages) and the dist directory (which is the production build).
# TODO: I need to figure out what these instructions are doing. I thought they were installing dependencies in an intermediate Docker container and then copying over the necessary files and folders into the final Docker container. But if I delete the `dist` folder and try to run `make prod-rebuild` and then `make prod`, I get an error that says that Docker cannot find the `dist/server/server.js` module. So right now I have to first run `npm run build` outside of any Docker commands and then I can run `make prod-rebuild` and then `make prod` and everything works properly. Hmmm. I need all development and production build steps contained within the Dockerfiles and docker-compose files and contained within Docker containers so that they install the same package versions no matter what the host machine is. So I need to rework these files. I want to install and copy over fresh new `node_modules` and `dist` directies all from within my Docker files. I need to read up on multi-stage builds again.
# --------------------------------------------------
# Instructions for first stage of multi-stage build
# --------------------------------------------------
FROM node:12 AS build
# WORKDIR /usr/src/app
# Copy package.json and package-lock.json files from host system to intermediate container.
COPY package.json .
COPY package-lock.json .
# Install Node packages into the intermediate container.
RUN npm install
# Copy all files from the host to the intermediate container in preparation for the build step.
COPY . .
# Create production version of the app inside the intermediate container.
RUN npm run build
# At this stage there is no dist folder on the host, so this isn't copying anything.
# COPY the `dist` directory from the host to the intermediate container.
# COPY dist dist/
# Remove all `devDependencies` packages from the `node_modules` directory. These packages won't be
# used in the production app after it has been built. Be careful that the only packages that are
# installed in `devDependencies` are Babel, testing, linting, and other development-only or
# testing-only packages.
RUN npm prune --production
# ---------------------------------------------------
# Instructions for second stage of multi-stage build
# ---------------------------------------------------
FROM node:12 AS production
WORKDIR /usr/src/app
# Copy the package.json and package-lock.json files
COPY --from=build package.json package.json
COPY --from=build package-lock.json package-lock.json
COPY --from=build .env .env
COPY --from=build node_modules node_modules/
COPY --from=build dist dist/
# RUN echo $PWD
# RUN ls dist/server
CMD [ "npm", "run", "prod" ]