Skip to content

Commit

Permalink
Add working dockerfile and docker compose.
Browse files Browse the repository at this point in the history
  • Loading branch information
d1str0 committed Nov 30, 2024
1 parent c90e2f6 commit 32c8e5a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 10 deletions.
47 changes: 47 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Build stage
FROM node:20-alpine AS builder

WORKDIR /app

# Install build dependencies
RUN apk add --no-cache python3 make g++

COPY package*.json ./
COPY api/package*.json ./api/

# Install dependencies
RUN npm ci
RUN cd api && npm ci

# Copy source code and Prisma schema
COPY . .

# Generate Prisma client and build
RUN cd api && npx prisma generate
RUN npm run api:build

# Production stage
FROM node:20-alpine

WORKDIR /app

# Install production dependencies
RUN apk add --no-cache python3 make g++

COPY --from=builder /app/api/dist ./dist
COPY --from=builder /app/api/package*.json ./
COPY --from=builder /app/api/prisma ./prisma
COPY --from=builder /app/api/node_modules/.prisma ./node_modules/.prisma

# Install production dependencies
RUN npm ci --only=production

# Environment variables
ENV NODE_ENV=production
ENV PORT=3000

# Expose API port
EXPOSE 3000

# Start the server
CMD ["node", "dist/app.js"]
18 changes: 9 additions & 9 deletions api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,21 @@ const start = async () => {
production: true,
test: false,
};

const fastify = require('fastify')({
logger: envToLogger[process.env.NODE_ENV || 'development'],
ignoreTrailingSlash: true, // Ignore trailing slashes at the end of URLs
ignoreTrailingSlash: true,
});

try {
await fastify.register(require('./app')).after(() => {
fastify.listen({ port: 3000 }, (err: Error, address: string) => {
if (err) {
fastify.log.error(err);
process.exit(1);
}
fastify.log.info(`Server listening on ${address}`);
});
await fastify.register(require('./app'));

const address = await fastify.listen({
port: process.env.PORT || 3000,
host: '0.0.0.0',
});

fastify.log.info(`Server listening on ${address}`);
} catch (err) {
fastify.log.error(err);
process.exit(1);
Expand Down
32 changes: 31 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,40 @@ services:
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U api_user -d mydb']
test: ['CMD-SHELL', 'pg_isready -U api_user -d agave']
interval: 10s
timeout: 5s
retries: 5
restart: unless-stopped
networks:
- api-network

api:
build:
context: .
dockerfile: Dockerfile
container_name: api
environment:
- DATABASE_URL=postgresql://api_user:randompassword@postgres:5432/agave
- NODE_ENV=production
- PORT=3000
- FASTIFY_LOG_LEVEL=debug
- HOST=0.0.0.0
ports:
- '3000:3000'
depends_on:
postgres:
condition: service_healthy
command: >
sh -c "npx prisma migrate deploy &&
node dist/app.js"
restart: unless-stopped
networks:
- api-network

networks:
api-network:
driver: bridge

volumes:
postgres_data:

0 comments on commit 32c8e5a

Please sign in to comment.