Skip to content

Commit 4d3ae61

Browse files
authored
improve docker layer caching for faster container builds (#1379)
* merge duplicate dockerfiles for monolith, better first yarn install layer caching * slightly better layer caching for building `ott-common`, `ott-client`, `ott-server`
1 parent dcab0d9 commit 4d3ae61

7 files changed

+53
-56
lines changed

.dockerignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ dist/
1111
client/dist/
1212
client/coverage/
1313
server/coverage/
14+
common/ts-out/
15+
server/ts-out/
1416
.git/
1517
.optic/
1618
node_modules/
1719
benchmarks/
1820
target/
1921
.slugignore
20-
ts-out
22+
ts-out

deploy/fly.prod.monolith.toml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ kill_timeout = 15
44

55
[build]
66
dockerfile = "monolith.Dockerfile"
7+
build-target = "deploy-stage"
78

89
[build.args]
910
DEPLOY_TARGET = "ott-prod"

deploy/fly.staging.monolith.toml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ kill_timeout = 15
44

55
[build]
66
dockerfile = "monolith.Dockerfile"
7+
build-target = "deploy-stage"
78

89
[build.args]
910
DEPLOY_TARGET = "ott-staging"

deploy/monolith.Dockerfile

+42-17
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,60 @@
1+
# Optimized for layer cache hits to speed up builds
2+
3+
FROM node:18-alpine3.19 as dep-install-stage
4+
5+
WORKDIR /app
6+
RUN apk update -q && apk --no-cache add libc6-compat python3 make g++ autoconf automake libtool -q
7+
COPY package.json yarn.lock ./
8+
COPY common/package.json common/
9+
COPY client/package.json client/
10+
COPY server/package.json server/
11+
RUN yarn install --frozen-lockfile
12+
113
FROM node:18-alpine3.19 as build-stage
214
ARG GIT_COMMIT
315
ENV GIT_COMMIT=$GIT_COMMIT
416

5-
WORKDIR /usr/app
6-
17+
WORKDIR /app
718
RUN apk update -q && apk --no-cache add libc6-compat python3 make g++ autoconf automake libtool -q
8-
COPY . .
9-
RUN yarn
10-
RUN yarn run build
11-
19+
COPY tsconfig.json ./
20+
COPY common common
21+
COPY client client
22+
COPY server server
23+
COPY --from=dep-install-stage /app /app
24+
RUN yarn workspace ott-common run build && yarn workspace ott-client run build && yarn workspace ott-server run build
25+
RUN rm -rf packages/ott-vis*
1226
RUN rm -rf node_modules && yarn install --production=true
1327

1428
FROM node:18-alpine3.19 as production-stage
15-
ARG DEPLOY_TARGET
1629

17-
WORKDIR /usr/app/
30+
WORKDIR /app
31+
COPY --from=build-stage /app /app
32+
RUN rm -rf client/public client/src client/.browserslistrc .eslintrc.js .gitignore client/vite.config.js client/babel.config.js docker-compose.yml /root/.npm tools crates
33+
34+
FROM node:18-alpine3.19 as docker-stage
35+
# For use in docker-compose
1836

37+
WORKDIR /app
1938
ENV NODE_ENV production
2039
ENV FFPROBE_PATH /usr/bin/ffprobe
21-
2240
RUN apk update -q && apk --no-cache add curl ffmpeg -q
41+
COPY docker/scripts/wait_for_db.sh /app/wait_for_db.sh
42+
COPY --from=production-stage /app /app
43+
HEALTHCHECK --interval=30s --timeout=3s CMD ( curl -f http://localhost:8080/api/status || exit 1 )
2344

24-
COPY --from=build-stage /usr/app/ /usr/app/
25-
26-
RUN rm -rf client/public client/src client/.browserslistrc .eslintrc.js .gitignore client/vite.config.js docker-compose.yml /root/.npm deploy crates tools
45+
CMD ["/bin/sh", "wait_for_db.sh", "--", "yarn", "run", "start"]
2746

28-
COPY deploy/base.toml /usr/app/env/
29-
COPY deploy/$DEPLOY_TARGET.toml /usr/app/env/production.toml
47+
FROM node:18-alpine3.19 as deploy-stage
48+
# For deployment on Fly
49+
ARG DEPLOY_TARGET
3050

31-
# Healthcheck API, WEB, REDIS
51+
WORKDIR /app
52+
ENV NODE_ENV production
53+
ENV FFPROBE_PATH /usr/bin/ffprobe
54+
RUN apk update -q && apk --no-cache add curl ffmpeg -q
55+
COPY --from=production-stage /app /app
56+
COPY deploy/base.toml /app/env/
57+
COPY deploy/$DEPLOY_TARGET.toml /app/env/production.toml
3258
HEALTHCHECK --interval=30s --timeout=3s CMD ( curl -f http://localhost:8080/api/status || exit 1 )
3359

34-
# Start Server
35-
CMD ["yarn", "workspace", "ott-server", "run", "start-lean"]
60+
CMD ["yarn", "workspace", "ott-server", "run", "start-lean"]

docker/Dockerfile

-34
This file was deleted.

docker/docker-compose.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ services:
44
image: opentogethertube
55
build:
66
context: ../
7-
dockerfile: docker/Dockerfile
7+
dockerfile: deploy/monolith.Dockerfile
8+
target: docker-stage
89
args:
910
- GIT_COMMIT=${GIT_COMMIT:-$(git rev-parse HEAD)}
1011
container_name: opentogethertube
@@ -29,7 +30,7 @@ services:
2930
- redis_db
3031
- postgres_db
3132
volumes:
32-
- "../env:/usr/app/env"
33+
- "../env:/app/env"
3334
restart: "${DOCKER_RESTART_POLICY:-unless-stopped}"
3435

3536
redis_db:

docker/with-balancer.docker-compose.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ services:
44
image: opentogethertube
55
build:
66
context: ../
7-
dockerfile: docker/Dockerfile
7+
dockerfile: deploy/monolith.Dockerfile
8+
target: docker-stage
89
args:
910
- GIT_COMMIT=${GIT_COMMIT:-$(git rev-parse HEAD)}
1011
environment:
@@ -26,7 +27,7 @@ services:
2627
- redis_db
2728
- postgres_db
2829
volumes:
29-
- "../env:/usr/app/env"
30+
- "../env:/app/env"
3031
restart: "${DOCKER_RESTART_POLICY:-unless-stopped}"
3132

3233
balancer:

0 commit comments

Comments
 (0)