diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 00000000..59f629a4 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,20 @@ +{ + "name": "Algora TV Development", + "dockerComposeFile": "../docker-compose.yml", + "service": "app", + "workspaceFolder": "/app", + "customizations": { + "vscode": { + "extensions": [ + "phoenixframework.phoenix", + "lexical-lsp.lexical", + "bradlc.vscode-tailwindcss" + ] + } + }, + "forwardPorts": [4000], + "remoteUser": "root", + "remoteEnv": { + "MIX_ENV": "dev" + } +} diff --git a/Dockerfile-dev b/Dockerfile-dev new file mode 100644 index 00000000..f13df8f6 --- /dev/null +++ b/Dockerfile-dev @@ -0,0 +1,72 @@ +# Base images +ARG BUILDER_IMAGE="hexpm/elixir:1.15.7-erlang-26.2-debian-bookworm-20231009-slim" +ARG RUNNER_IMAGE="hexpm/elixir:1.15.7-erlang-26.2-debian-bookworm-20231009-slim" + +FROM ${BUILDER_IMAGE} as builder + +# Install build dependencies +RUN apt-get update -y && \ + apt-get install -y build-essential git curl postgresql-client && \ + apt-get clean && \ + rm -f /var/lib/apt/lists/*_* + +# Prepare build dir +WORKDIR /app + +# Install hex + rebar +RUN mix local.hex --force && \ + mix local.rebar --force + +# Set build ENV to development +ENV MIX_ENV="dev" + +# Install mix dependencies +COPY mix.exs mix.lock ./ +RUN mix deps.get --only $MIX_ENV +RUN mkdir config + +# Copy compile-time config files +COPY config/config.exs config/${MIX_ENV}.exs config/ +RUN mix deps.compile + +COPY priv priv + +# Compile the release +COPY lib lib + +COPY assets assets +RUN mix assets.deploy +RUN mix compile + +# Copy runtime config +COPY config/runtime.exs config/ +COPY rel rel + +# Don't run mix release for dev +# RUN mix release + +FROM ${RUNNER_IMAGE} + +# Install runtime dependencies +RUN apt-get update -y && \ + apt-get install -y libstdc++6 curl sudo openssl libncurses5 locales ffmpeg imagemagick postgresql-client inotify-tools git && \ + apt-get clean && \ + rm -f /var/lib/apt/lists/*_* + +WORKDIR /app + +# Copy the built artifact from the builder stage +COPY --from=builder /app /app +COPY --from=builder /root/.mix /root/.mix + +# Install hex and rebar +RUN mix local.hex --force && \ + mix local.rebar --force + +# Add this near the end of the file +COPY entrypoint.sh /app/ +RUN chmod +x /app/entrypoint.sh + +# Change the ENTRYPOINT to use shell form +ENTRYPOINT ["/bin/bash", "/app/entrypoint.sh"] +CMD ["phx.server"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..000eadf9 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,43 @@ +services: + db: + image: postgres:13 + environment: + POSTGRES_USER: dev_user + POSTGRES_PASSWORD: dev_password + POSTGRES_DB: dev_db + ports: + - "15432:15432" + volumes: + - postgres_data:/var/lib/postgresql/data + command: -p 15432 + healthcheck: + test: [ "CMD-SHELL", "pg_isready -U dev_user -d dev_db -p 15432" ] + interval: 5s + timeout: 5s + retries: 5 + + app: + build: + context: . + dockerfile: Dockerfile-dev + environment: + MIX_ENV: dev + DATABASE_URL: "postgres://dev_user:dev_password@db:15432/dev_db" + SEED_DB: "true" + GITHUB_CLIENT_ID: ${GITHUB_CLIENT_ID} + GITHUB_CLIENT_SECRET: ${GITHUB_CLIENT_SECRET} + ports: + - "4000:4000" + depends_on: + db: + condition: service_healthy + command: [ "phx.server" ] + volumes: + - .:/app + - deps:/app/deps + - _build:/app/_build + +volumes: + postgres_data: + deps: + _build: diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 00000000..0b4a0839 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,35 @@ +#!/bin/bash +set -e + +# Source the .env file if it exists +if [ -f .env ]; then + export $(cat .env | xargs) +fi +export DATABASE_URL="postgresql://dev_user:dev_password@db:15432/dev_db" + + +echo "Starting entrypoint script..." +echo "DATABASE_URL: $DATABASE_URL" + +# Wait for the database to be ready +while ! pg_isready -h db -p 15432 -q -U dev_user; do + echo "Waiting for database connection..." + sleep 2 +done + +echo "Database is ready." +echo "Creating database..." +mix ecto.create +# Run migrations +echo "Running migrations..." +mix ecto.migrate + +# Run seeds if the SEED_DB environment variable is set to true +if [ "$SEED_DB" = "true" ]; then + echo "Seeding database..." + mix run priv/repo/seeds.exs +fi + +echo "Starting Phoenix app..." +# Start the Phoenix app +exec mix "$@" \ No newline at end of file diff --git a/start.sh b/start.sh new file mode 100644 index 00000000..77cf51fe --- /dev/null +++ b/start.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +# Wait for Postgres to become available +until psql $DATABASE_URL -c '\q'; do + >&2 echo "Postgres is unavailable - sleeping" + sleep 1 +done + +>&2 echo "Postgres is up - executing command" + +# Run migrations +mix ecto.setup + +# Start the Phoenix app +mix phx.server \ No newline at end of file