Skip to content

Commit 32c8e5a

Browse files
committed
Add working dockerfile and docker compose.
1 parent c90e2f6 commit 32c8e5a

File tree

3 files changed

+87
-10
lines changed

3 files changed

+87
-10
lines changed

Dockerfile

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Build stage
2+
FROM node:20-alpine AS builder
3+
4+
WORKDIR /app
5+
6+
# Install build dependencies
7+
RUN apk add --no-cache python3 make g++
8+
9+
COPY package*.json ./
10+
COPY api/package*.json ./api/
11+
12+
# Install dependencies
13+
RUN npm ci
14+
RUN cd api && npm ci
15+
16+
# Copy source code and Prisma schema
17+
COPY . .
18+
19+
# Generate Prisma client and build
20+
RUN cd api && npx prisma generate
21+
RUN npm run api:build
22+
23+
# Production stage
24+
FROM node:20-alpine
25+
26+
WORKDIR /app
27+
28+
# Install production dependencies
29+
RUN apk add --no-cache python3 make g++
30+
31+
COPY --from=builder /app/api/dist ./dist
32+
COPY --from=builder /app/api/package*.json ./
33+
COPY --from=builder /app/api/prisma ./prisma
34+
COPY --from=builder /app/api/node_modules/.prisma ./node_modules/.prisma
35+
36+
# Install production dependencies
37+
RUN npm ci --only=production
38+
39+
# Environment variables
40+
ENV NODE_ENV=production
41+
ENV PORT=3000
42+
43+
# Expose API port
44+
EXPOSE 3000
45+
46+
# Start the server
47+
CMD ["node", "dist/app.js"]

api/src/app.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,21 @@ const start = async () => {
5050
production: true,
5151
test: false,
5252
};
53+
5354
const fastify = require('fastify')({
5455
logger: envToLogger[process.env.NODE_ENV || 'development'],
55-
ignoreTrailingSlash: true, // Ignore trailing slashes at the end of URLs
56+
ignoreTrailingSlash: true,
5657
});
5758

5859
try {
59-
await fastify.register(require('./app')).after(() => {
60-
fastify.listen({ port: 3000 }, (err: Error, address: string) => {
61-
if (err) {
62-
fastify.log.error(err);
63-
process.exit(1);
64-
}
65-
fastify.log.info(`Server listening on ${address}`);
66-
});
60+
await fastify.register(require('./app'));
61+
62+
const address = await fastify.listen({
63+
port: process.env.PORT || 3000,
64+
host: '0.0.0.0',
6765
});
66+
67+
fastify.log.info(`Server listening on ${address}`);
6868
} catch (err) {
6969
fastify.log.error(err);
7070
process.exit(1);

docker-compose.yml

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,40 @@ services:
1313
volumes:
1414
- postgres_data:/var/lib/postgresql/data
1515
healthcheck:
16-
test: ['CMD-SHELL', 'pg_isready -U api_user -d mydb']
16+
test: ['CMD-SHELL', 'pg_isready -U api_user -d agave']
1717
interval: 10s
1818
timeout: 5s
1919
retries: 5
20+
restart: unless-stopped
21+
networks:
22+
- api-network
23+
24+
api:
25+
build:
26+
context: .
27+
dockerfile: Dockerfile
28+
container_name: api
29+
environment:
30+
- DATABASE_URL=postgresql://api_user:randompassword@postgres:5432/agave
31+
- NODE_ENV=production
32+
- PORT=3000
33+
- FASTIFY_LOG_LEVEL=debug
34+
- HOST=0.0.0.0
35+
ports:
36+
- '3000:3000'
37+
depends_on:
38+
postgres:
39+
condition: service_healthy
40+
command: >
41+
sh -c "npx prisma migrate deploy &&
42+
node dist/app.js"
43+
restart: unless-stopped
44+
networks:
45+
- api-network
46+
47+
networks:
48+
api-network:
49+
driver: bridge
2050

2151
volumes:
2252
postgres_data:

0 commit comments

Comments
 (0)