diff --git a/.env.example b/.env.example index ae4bbc1..2152708 100644 --- a/.env.example +++ b/.env.example @@ -12,3 +12,9 @@ DATABASE_URL=postgres://codesce:changeme@database:5432/codesce?sslmode=disable # Frontend FRONTEND_PORT=3000 VITE_API_URL=http://localhost:6767 + +# Redis +REDIS_URL=redis://redis:6379 +WORKER_COUNT=4 +EXECUTION_TIMEOUT=10 +SANDBOX_MEMORY_LIMIT=256 \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 7a2bbd3..dfd0c51 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,4 +23,4 @@ COPY --from=build /bin/server /bin/server EXPOSE 6767 -CMD ["/bin/server"] +CMD ["/bin/server"] \ No newline at end of file diff --git a/Dockerfile.executor b/Dockerfile.executor new file mode 100644 index 0000000..cb63801 --- /dev/null +++ b/Dockerfile.executor @@ -0,0 +1,31 @@ +# typing everythig up manually: high cortisol +# COPY AND PASTE FROM OTHER FILES: low cortisol + +# executor dockerfile (go) thinking monkey.... + +# --- Build stage --- + +# go image with compiler, names it build +FROM golang:1.25-alpine AS build + +# set working directory inside container +WORKDIR /app + +# copy two dependency files for main.go into ./ inside the container aka /app +COPY cmd/runner/go.mod cmd/runner/go.sum ./ +RUN go mod download + +# copy source code (left) into image (right) +COPY cmd/runner/ . + +# compiles code in /app into service called runner +RUN go build -o runner . + +# --- Production stage --- +FROM alpine:3.20 AS production + +WORKDIR /app + +COPY --from=build /app/runner /app/runner + +CMD ["/app/runner"] diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index a308fb0..47da342 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -2,7 +2,26 @@ services: database: ports: - "${DB_PORT:-5432}:5432" - + redis: + ports: + - "6379:6379" + executor: + build: + context: . + dockerfile: Dockerfile.executor + environment: + REDIS_URL: ${REDIS_URL} + DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@database:5432/${POSTGRES_DB}?sslmode=disable + WORKER_COUNT: ${WORKER_COUNT} + EXECUTION_TIMEOUT: ${EXECUTION_TIMEOUT} + SANDBOX_MEMORY_LIMIT: ${SANDBOX_MEMORY_LIMIT} + volumes: + - /var/run/docker.sock:/var/run/docker.sock + depends_on: + redis: + condition: service_healthy + database: + condition: service_healthy backend: build: context: . @@ -11,6 +30,7 @@ services: GIN_MODE: debug PORT: ${BACKEND_PORT:-6767} DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@database:5432/${POSTGRES_DB}?sslmode=disable + REDIS_URL: ${REDIS_URL} volumes: - .:/app - go-mod-cache:/go/pkg/mod diff --git a/docker-compose.yml b/docker-compose.yml index 4b8d47c..985dbb7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,34 @@ services: +# each service is a container + redis: + image: redis:7-alpine + ports: + - "6379:6379" + # will return pong if up + healthcheck: + test: ["CMD-SHELL", "redis-cli", "ping"] + interval: 5s + timeout: 5s + retries: 5 + executor: + build: + context: . + dockerfile: Dockerfile.executor + environment: + REDIS_URL: ${REDIS_URL} + DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@database:5432/${POSTGRES_DB}?sslmode=disable + WORKER_COUNT: ${WORKER_COUNT} + EXECUTION_TIMEOUT: ${EXECUTION_TIMEOUT} + SANDBOX_MEMORY_LIMIT: ${SANDBOX_MEMORY_LIMIT} + # executor allows us to run other containers + # left side is file from host, right side is file inside container + volumes: + - /var/run/docker.sock:/var/run/docker.sock + depends_on: + redis: + condition: service_healthy + database: + condition: service_healthy database: image: postgres:16-alpine container_name: codesce-db @@ -25,6 +55,7 @@ services: DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@database:5432/${POSTGRES_DB}?sslmode=disable GIN_MODE: ${GIN_MODE:-release} PORT: ${BACKEND_PORT:-6767} + REDIS_URL: ${REDIS_URL} ports: - "${BACKEND_PORT:-6767}:6767" depends_on: diff --git a/docker/Dockerfile.python b/docker/Dockerfile.python new file mode 100644 index 0000000..560fcb9 --- /dev/null +++ b/docker/Dockerfile.python @@ -0,0 +1 @@ +FROM python3.12-slim \ No newline at end of file