Skip to content

Commit

Permalink
chore: add devcontainer (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Feb 20, 2024
1 parent 5663e6c commit 6316fa6
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 0 deletions.
54 changes: 54 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Update the VARIANT arg in docker-compose.yml to pick a PHP version: 8, 8.0, 8.1
ARG VARIANT=8
FROM mcr.microsoft.com/devcontainers/php:${VARIANT}

# Install packages
RUN set -ex; \
\
apt-get update; \
apt-get install -y --no-install-recommends \
sqlite3 \
; \
rm -rf /var/lib/apt/lists/*

# Update args in docker-compose.yaml to set the UID/GID of the "vscode" user.
ARG USER_UID=1000
ARG USER_GID=$USER_UID
RUN if [ "$USER_GID" != "1000" ] || [ "$USER_UID" != "1000" ]; then \
groupmod --gid $USER_GID vscode \
&& usermod --uid $USER_UID --gid $USER_GID vscode \
&& chmod -R $USER_UID:$USER_GID /home/vscode \
&& chmod -R $USER_UID:root /usr/local/share/nvm; \
fi

# [Optional] Install a version of Node.js using nvm for front end dev
ARG INSTALL_NODE="true"
ARG NODE_VERSION="lts/*"
RUN if [ "${INSTALL_NODE}" = "true" ]; then bash -c "source /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION}"; fi

# [Optional] Uncomment this section to install additional OS packages.
RUN set -ex; \
\
apt-get update; \
apt-get install -y --no-install-recommends \
libicu-dev \
zlib1g-dev \
libzip-dev \
libpq-dev \
libgmp-dev \
; \
\
debMultiarch="$(dpkg-architecture --query DEB_BUILD_MULTIARCH)"; \
docker-php-ext-configure intl; \
docker-php-ext-configure gmp; \
docker-php-ext-install -j$(nproc) \
intl \
zip \
bcmath \
gmp \
; \
\
rm -rf /var/lib/apt/lists/*

# [Optional] Uncomment this line to install global node packages.
RUN npm install -g --unsafe-perm yarn
16 changes: 16 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "PHP & SQLite",
"service": "app",
"workspaceFolder": "/workspace",
"dockerComposeFile": "docker-compose.yml",

// For use with PHP or Apache (e.g.php -S localhost:8080 or apache2ctl start)
"forwardPorts": [8080],

// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": ".devcontainer/postCreate.sh",
"postStartCommand": ".devcontainer/postStart.sh"

// Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root.
// "remoteUser": "vscode"
}
21 changes: 21 additions & 0 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: '3'

services:
app:
build:
context: .
dockerfile: Dockerfile
args:
VARIANT: '8.2'
# [Option] Install Node.js
INSTALL_NODE: 'true'
NODE_VERSION: 'lts/*'
# On Linux, you may need to update USER_UID and USER_GID below if not your local UID is not 1000.
USER_UID: 1000
USER_GID: 1000

volumes:
- ..:/workspace:cached

# Overrides default command so things don't shut down after the process ends.
command: sleep infinity
72 changes: 72 additions & 0 deletions .devcontainer/postCreate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/bin/bash

DATABASE=database/database.sqlite

realpath ()
{
f=$@;
if [ -z "$f" ]; then
f=$(pwd)
fi
if [ -d "$f" ]; then
base="";
dir="$f";
else
base="/$(basename "$f")";
dir=$(dirname "$f");
fi;
dir=$(cd "$dir" && /bin/pwd -P);
echo "$dir$base"
}

setenv() {
sed -i "s%$1=.*%$1=$2%" $ROOT/.env
}

set_apache() {
chgrp -R www-data $ROOT/storage && chmod -R g+w $ROOT/storage
sudo rm -rf /var/www/html && sudo ln -s "$ROOT/public" /var/www/html
sudo a2enmod rewrite
sudo apache2ctl restart
}

set_database() {
cp $ROOT/.env.example $ROOT/.env && echo "APP_TRUSTED_PROXIES=*" >> $ROOT/.env
setenv "DB_CONNECTION" "sqlite"
setenv "DB_DATABASE" "$ROOT/$DATABASE"
touch $ROOT/$DATABASE && chgrp www-data $ROOT/$DATABASE && chmod g+w $ROOT/$DATABASE
}

set_conf() {
setenv "CACHE_DRIVER" "file"
setenv "QUEUE_CONNECTION" "sync"
setenv "SESSION_DRIVER" "database"
setenv "MAIL_MAILER" "log"
setenv "MAIL_FROM_ADDRESS" "[email protected]"
setenv "MAIL_REPLY_TO_ADDRESS" "[email protected]"
setenv "APP_URL" "https://${CODESPACE_NAME}-8080.${GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN}"
}

composer_install() {
composer install --no-progress --no-interaction --prefer-dist --optimize-autoloader --working-dir=$ROOT
}

yarn_install() {
yarn --cwd $ROOT install --immutable
yarn --cwd $ROOT run build
}

setup() {
php $ROOT/artisan key:generate --no-interaction
php $ROOT/artisan setup --force -vvv
}

SELF_PATH=$(cd -P -- "$(dirname -- "$0")" && /bin/pwd -P)
ROOT=$(realpath $SELF_PATH/..)

set_apache
set_database
set_conf
composer_install
setup
yarn_install
3 changes: 3 additions & 0 deletions .devcontainer/postStart.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

sudo apache2ctl restart
21 changes: 21 additions & 0 deletions config/trustedproxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Application trusted proxies
|--------------------------------------------------------------------------
|
| Set trusted proxy IP addresses.
| Both IPv4 and IPv6 addresses are supported, along with CIDR notation.
|
| The "*" character is syntactic sugar within TrustedProxy to trust any
| proxy that connects directly to your server, a requirement when you
| cannot know the address of your proxy (e.g. if using ELB or similar).
|
*/

'proxies' => env('APP_TRUSTED_PROXIES', null),

];

0 comments on commit 6316fa6

Please sign in to comment.