Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: devcontainer setup #67

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
72 changes: 72 additions & 0 deletions Dockerfile-dev
Original file line number Diff line number Diff line change
@@ -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"]
43 changes: 43 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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:
35 changes: 35 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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 "$@"
15 changes: 15 additions & 0 deletions start.sh
Original file line number Diff line number Diff line change
@@ -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