Skip to content

Commit 0f5cc50

Browse files
authored
Docker production overrides (#5)
* Add production overrides * Add config stub * Stop building after dev stage for dev override * Fixup ports for production build * Add entrypoint script to write relevant env vars to file
1 parent 2f1eb2f commit 0f5cc50

5 files changed

+73
-1
lines changed

.env.default

+3
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ COMPOSE_PROJECT_NAME=
77

88
# docker-compose development overrides; uncomment to enable
99
# COMPOSE_FILE=docker-compose.yaml:docker-compose.dev.yaml
10+
11+
# docker-compose production overrides; uncomment to enable
12+
# COMPOSE_FILE=docker-compose.yaml:docker-compose.prod.yaml

Dockerfile

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# TODO update to newer version: Active LTS or Current
2-
FROM node:14
2+
FROM node:14 as dev
33

44
# cache hack (very fragile): initially only copy list of project dependencies
55
COPY --chown=node:node package.json package-lock.json /opt/node/
@@ -18,3 +18,17 @@ WORKDIR /opt/node/app
1818

1919
EXPOSE 3000
2020
CMD ["npm", "start"]
21+
22+
23+
FROM dev as node-prod
24+
ENV NODE_ENV=production
25+
RUN npm run build
26+
27+
28+
FROM nginx as prod
29+
COPY docker-entrypoint-override.sh /usr/bin/docker-entrypoint-override.sh
30+
# write environment variables to config file and start
31+
ENTRYPOINT ["/usr/bin/docker-entrypoint-override.sh", "/docker-entrypoint.sh"]
32+
CMD ["nginx","-g","daemon off;"]
33+
34+
COPY --from=node-prod /opt/node/app/build /usr/share/nginx/html

docker-compose.dev.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
version: "3.4"
44
services:
55
frontend:
6+
build:
7+
# stop building Dockerfile after dev stage
8+
target: dev
69
volumes:
710
# mount code directory into container to allow hot-reloading
811
- ./:/opt/node/app

docker-compose.prod.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# docker-compose production overrides
2+
---
3+
version: "3.4"
4+
services:
5+
frontend:
6+
build:
7+
target: prod
8+
environment:
9+
NODE_ENV: production
10+
ports:
11+
# map container port 80 to $FRONTEND_EXTERNAL_PORT (port 3000 by default) on localhost (127.0.0.1)
12+
- 127.0.0.1:${FRONTEND_EXTERNAL_PORT:-3000}:80

docker-entrypoint-override.sh

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/sh
2+
set -eu
3+
4+
repo_path="$(cd "$(dirname "$0")" && pwd)"
5+
cmdname="$(basename "$0")"
6+
7+
usage() {
8+
cat << USAGE >&2
9+
Usage:
10+
$cmdname command
11+
12+
Docker entrypoint script
13+
Wrapper script that executes docker-related tasks before running given command
14+
15+
USAGE
16+
exit 1
17+
}
18+
19+
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
20+
usage
21+
exit 0
22+
fi
23+
24+
25+
write_env_to_json() {
26+
# write project-specific environment variables to app config file
27+
local json_file="$1"
28+
29+
# only write environment variables frontend will read (beginning with "REACT_" or "VUE_")
30+
local json_contents="{$(printenv | grep -e REACT_ -e VUE_ | sed 's/^\|$/"/g' | sed 's|=|":"|' | paste -sd, -)}"
31+
echo "$json_contents" > "$json_file"
32+
}
33+
34+
35+
write_env_to_json /usr/share/nginx/html/env.json
36+
37+
38+
echo $cmdname complete
39+
echo executing given command $@
40+
exec "$@"

0 commit comments

Comments
 (0)