Skip to content

Commit 43aa68e

Browse files
authored
cleaned up multi-service compose (#75)
* cleaned up multi-service compose * refactor docker setup * add all ENVs and remove unneeded variables * remove comments, unneeded vars and syntax
1 parent 3d747f4 commit 43aa68e

File tree

4 files changed

+53
-64
lines changed

4 files changed

+53
-64
lines changed

client/Dockerfile

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,50 @@
1-
# Install dependencies only when needed
2-
FROM node:16-alpine AS deps
1+
FROM node:17-alpine AS deps
2+
33
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
44
RUN apk add --no-cache libc6-compat
5+
56
WORKDIR /app
7+
68
COPY package.json yarn.lock ./
9+
710
RUN yarn install --frozen-lockfile
811

9-
# If using npm with a `package-lock.json` comment out above and use below instead
10-
# COPY package.json package-lock.json ./
11-
# RUN npm ci
12+
FROM node:17-alpine AS builder
1213

13-
# Rebuild the source code only when needed
14-
FROM node:16-alpine AS builder
1514
WORKDIR /app
15+
1616
COPY --from=deps /app/node_modules ./node_modules
1717
COPY . .
1818

19-
# Next.js collects completely anonymous telemetry data about general usage.
20-
# Learn more here: https://nextjs.org/telemetry
21-
# Uncomment the following line in case you want to disable telemetry during the build.
22-
ENV NEXT_TELEMETRY_DISABLED 1
23-
ARG API_URL http://localhost:3000
24-
ARG SECRET_KEY secret
19+
ARG API_URL
20+
21+
ENV NEXT_TELEMETRY_DISABLED=1
22+
ENV API_URL=${API_URL:-http://localhost:3000}
2523

2624
RUN yarn build
2725

28-
# If using npm comment out above and use below instead
29-
# RUN npm run build
26+
FROM node:17-alpine AS runner
3027

31-
# Production image, copy all the files and run next
32-
FROM node:16-alpine AS runner
3328
WORKDIR /app
3429

35-
ENV NODE_ENV production
30+
ARG NODE_ENV
3631

37-
ENV NEXT_TELEMETRY_DISABLED 1
32+
ENV NEXT_TELEMETRY_DISABLED=1
33+
ENV NODE_ENV=${NODE_ENV:-production}
3834

3935
RUN addgroup --system --gid 1001 nodejs
4036
RUN adduser --system --uid 1001 nextjs
4137

42-
# You only need to copy next.config.js if you are NOT using the default configuration
4338
COPY --from=builder /app/next.config.mjs ./
4439
COPY --from=builder /app/public ./public
4540
COPY --from=builder /app/package.json ./package.json
46-
47-
# Automatically leverage output traces to reduce image size
48-
# https://nextjs.org/docs/advanced-features/output-file-tracing
4941
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
5042
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
5143

5244
USER nextjs
5345

54-
EXPOSE 3001
46+
ENV PORT=3001
5547

56-
ENV PORT 3001
48+
EXPOSE 3001
5749

5850
CMD ["node", "server.js"]

docker-compose.yml

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,34 @@
1-
version: '3.8'
21
services:
32
server:
4-
build:
3+
build:
54
context: ./server
65
args:
7-
SECRET_KEY: secret
6+
- NODE_ENV=production
7+
container_name: server
88
restart: unless-stopped
99
user: 1000:1000
1010
environment:
11-
- JWT_SECRET=change_me! # use `openssl rand -hex 32` to generate a strong secret
12-
- SECRET_KEY=secret
11+
- PORT
12+
- JWT_SECRET=jwt_secret # change_me! # use `openssl rand -hex 32` to generate a strong secret
13+
- SECRET_KEY=secret # change me!
14+
- MEMORY_DB
15+
- REGISTRATION_PASSWORD
16+
- WELCOME_CONTENT
17+
- WELCOME_TITLE
18+
- ENABLE_ADMIN
19+
- DRIFT_HOME
1320
ports:
1421
- "3000:3000"
15-
networks:
16-
- general
17-
container_name: server
1822
client:
1923
build:
2024
context: ./client
21-
network: host
2225
args:
23-
API_URL: http://localhost:3000
24-
SECRET_KEY: secret
26+
- API_URL=http://server:3000
27+
container_name: client
2528
restart: unless-stopped
2629
user: 1000:1000
2730
environment:
28-
- API_URL=http://localhost:3000
29-
- SECRET_KEY=secret
31+
- API_URL=http://server:3000
32+
- SECRET_KEY=secret # change me!
3033
ports:
3134
- "3001:3001"
32-
expose:
33-
- 3001
34-
networks:
35-
- general
36-
# depends_on:
37-
# server:
38-
# condition: service_healthy
39-
container_name: client
40-
41-
networks:
42-
general:
43-
driver: bridge

server/Dockerfile

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
1-
# Install dependencies only when needed
2-
FROM node:16-alpine AS deps
1+
FROM node:17-alpine AS deps
2+
33
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
44
RUN apk add --no-cache libc6-compat git
5+
56
WORKDIR /app
7+
68
COPY package.json yarn.lock tsconfig.json tslint.json ./
9+
710
RUN yarn install --frozen-lockfile
811

9-
# If using npm with a `package-lock.json` comment out above and use below instead
10-
# COPY package.json package-lock.json ./
11-
# RUN npm ci
12+
FROM node:17-alpine AS builder
1213

13-
# Rebuild the source code only when needed
14-
FROM node:16-alpine AS builder
1514
WORKDIR /app
15+
1616
COPY --from=deps /app/node_modules ./node_modules
1717
COPY . .
18-
ENV NODE_ENV production
18+
19+
ARG NODE_ENV
20+
21+
ENV NODE_ENV=${NODE_ENV:-production}
22+
1923
RUN apk add --no-cache git
20-
ARG SECRET_KEY secret
24+
RUN yarn build:docker
2125

22-
RUN yarn build
26+
FROM node:17-alpine AS runner
2327

24-
FROM node:16-alpine AS runner
2528
WORKDIR /app
2629

27-
ENV NODE_ENV production
30+
ARG NODE_ENV
31+
32+
ENV NODE_ENV=${NODE_ENV:-production}
2833

2934
RUN addgroup --system --gid 1001 nodejs
3035
RUN adduser --system --uid 1001 drift
@@ -34,8 +39,8 @@ COPY --from=builder /app/node_modules ./node_modules
3439

3540
USER drift
3641

37-
EXPOSE 3000
42+
ENV PORT=3000
3843

39-
ENV PORT 3000
44+
EXPOSE 3000
4045

4146
CMD ["node", "dist/index.js"]

server/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"start": "cross-env NODE_ENV=production node dist/index.js",
88
"dev": "cross-env NODE_ENV=development nodemon index.ts",
99
"build": "mkdir -p ./dist && cp .env ./dist/.env && tsc -p ./tsconfig.json && tsc-alias -p ./tsconfig.json && yarn post-build",
10+
"build:docker": "mkdir -p ./dist && cp .env.test ./dist/.env && tsc -p ./tsconfig.json && tsc-alias -p ./tsconfig.json && yarn post-build",
1011
"post-build": "cp package.json ./dist/package.json && cp yarn.lock ./dist/yarn.lock && cd dist && env NODE_ENV=production yarn install",
1112
"migrate:up": "ts-node migrate up",
1213
"migrate:down": "ts-node migrate down",

0 commit comments

Comments
 (0)