From 2f3294b89974681c57e5444f638e581f04406d23 Mon Sep 17 00:00:00 2001 From: Matchu Date: Tue, 29 Aug 2023 11:39:12 -0700 Subject: [PATCH] [WIP] Build a dev container Idk why, but unlike my previous experience with Rails devcontainers, this time the setup process is running so wildly slowly? Might just be a transient issue on my machine, maybe something that would be improved with a restart and trying again another time? Or could be something about the MySQL image that doesn't run great in this context? In any case, I'm just gonna set this down for now! --- .devcontainer/Dockerfile | 15 ++++++++++++ .devcontainer/create-db.sql | 5 ++++ .devcontainer/devcontainer.json | 39 ++++++++++++++++++++++++++++++++ .devcontainer/docker-compose.yml | 29 ++++++++++++++++++++++++ .devcontainer/post-create.sh | 13 +++++++++++ config/database.yml | 8 +++---- vendor/.gitignore | 1 + 7 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 .devcontainer/Dockerfile create mode 100644 .devcontainer/create-db.sql create mode 100644 .devcontainer/devcontainer.json create mode 100644 .devcontainer/docker-compose.yml create mode 100755 .devcontainer/post-create.sh create mode 100644 vendor/.gitignore diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 00000000..2cc67040 --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,15 @@ +FROM mcr.microsoft.com/devcontainers/ruby:1-3.1-bullseye + +# Default value to allow debug server to serve content over GitHub Codespace's port forwarding service +# The value is a comma-separated list of allowed domains +ENV RAILS_DEVELOPMENT_HOSTS=".githubpreview.dev,.preview.app.github.dev,.app.github.dev" + +# [Optional] Uncomment this section to install additional OS packages. +# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ +# && apt-get -y install --no-install-recommends + +# [Optional] Uncomment this line to install additional gems. +# RUN gem install + +# [Optional] Uncomment this line to install global node packages. +# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g " 2>&1 diff --git a/.devcontainer/create-db.sql b/.devcontainer/create-db.sql new file mode 100644 index 00000000..b5a805a1 --- /dev/null +++ b/.devcontainer/create-db.sql @@ -0,0 +1,5 @@ +CREATE DATABASE openneo_impress; +GRANT ALL PRIVILEGES ON openneo_impress.* TO impress_dev; + +CREATE DATABASE openneo_id; +GRANT ALL PRIVILEGES ON openneo_id.* TO impress_dev; diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 00000000..cd9b5afe --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,39 @@ +// For format details, see https://aka.ms/devcontainer.json. For config options, see the +// README at: https://github.com/devcontainers/templates/tree/main/src/ruby-rails-postgres +{ + "name": "Dress to Impress", + "dockerComposeFile": "docker-compose.yml", + "service": "app", + "workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}", + "features": { + "ghcr.io/devcontainers/features/node:1": { + "nodeGypDependencies": true, + "version": "lts" + } + }, + + // Features to add to the dev container. More info: https://containers.dev/features. + // "features": {}, + + // Use 'forwardPorts' to make a list of ports inside the container available locally. + // This can be used to network with other containers or the host. + "forwardPorts": [3000], + + // Use 'postCreateCommand' to run commands after the container is created. + "postCreateCommand": ".devcontainer/post-create.sh", + + // HACK: Out of the box, this dev container doesn't allow installation to the + // default GEM_HOME, because of a weird thing going on with RVM. Instead, we + // set a custom GEM_HOME and GEM_PATH in our home directory! + // https://github.com/devcontainers/templates/issues/188 + "containerEnv": { + "GEM_HOME": "~/.rubygems", + "GEM_PATH": "~/.rubygems" + } + + // Configure tool-specific properties. + // "customizations": {}, + + // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. + // "remoteUser": "root" +} diff --git a/.devcontainer/docker-compose.yml b/.devcontainer/docker-compose.yml new file mode 100644 index 00000000..87dae876 --- /dev/null +++ b/.devcontainer/docker-compose.yml @@ -0,0 +1,29 @@ +version: '3' + +services: + app: + build: + context: .. + dockerfile: .devcontainer/Dockerfile + + volumes: + - ../..:/workspaces:cached + + # Overrides default command so things don't shut down after the process ends. + command: sleep infinity + + # Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function. + network_mode: service:db + + # Use "forwardPorts" in **devcontainer.json** to forward an app port locally. + # (Adding the "ports" property to this file will not forward from a Codespace.) + + db: + image: mysql:latest + restart: unless-stopped + volumes: + - ./create-db.sql:/docker-entrypoint-initdb.d/create-db.sql + environment: + MYSQL_ROOT_PASSWORD: impress_dev + MYSQL_USER: impress_dev + MYSQL_PASSWORD: impress_dev diff --git a/.devcontainer/post-create.sh b/.devcontainer/post-create.sh new file mode 100755 index 00000000..1828c430 --- /dev/null +++ b/.devcontainer/post-create.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +set -e # Quit if any part of this script fails. + +# Mark all git repositories as safe to execute, including cached gems. +# NOTE: This would be dangerous to run on a normal multi-user machine, +# but for a dev container that only we use, it should be fine! +git config --global safe.directory '*' + +# Install the app's Ruby gem dependencies. +bundle install + +# Set up the databases: create the schema, and load in some default data. +rake db:schema:load db:seed diff --git a/config/database.yml b/config/database.yml index c17385f8..e57ed12f 100644 --- a/config/database.yml +++ b/config/database.yml @@ -2,8 +2,8 @@ development: primary: adapter: mysql2 database: openneo_impress - username: openneo_impress - password: openneo_impress + username: impress_dev + password: impress_dev pool: 5 variables: sql_mode: TRADITIONAL @@ -11,8 +11,8 @@ development: openneo_id: adapter: mysql2 database: openneo_id - username: openneo_impress - password: openneo_impress + username: impress_dev + password: impress_dev pool: 2 variables: sql_mode: TRADITIONAL diff --git a/vendor/.gitignore b/vendor/.gitignore new file mode 100644 index 00000000..3bd6b832 --- /dev/null +++ b/vendor/.gitignore @@ -0,0 +1 @@ +/bundle \ No newline at end of file