Skip to content

Commit

Permalink
Issue 349/database not created (#372)
Browse files Browse the repository at this point in the history
* #349 Handle some supervisor warnings and tidy up log output

* Fixed a typo in the README

* #349 --force is necessary when running with APP_ENV=production

* Coderabbit changes for #349
  • Loading branch information
donuk authored Apr 12, 2024
1 parent a854113 commit 1dd02cc
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 30 deletions.
9 changes: 5 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ FROM --platform=linux/amd64 ubuntu:23.04

# supervisord is a process manager which will be responsible for managing the
# various server processes. These are configured in docker/supervisord.conf

CMD ["/usr/bin/supervisord"]
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"]

WORKDIR /app

Expand All @@ -69,8 +68,10 @@ RUN apt-get update \
supervisor nginx sudo postgresql-15 redis\
$PHP_PACKAGES php8.1-fpm wget\
&& apt-get clean
RUN wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.39.3/install.sh | bash
RUN . /root/.nvm/nvm.sh && nvm install 20

RUN useradd nuxt && mkdir ~nuxt && chown nuxt ~nuxt
RUN wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.39.3/install.sh | sudo -u nuxt bash
RUN sudo -u nuxt bash -c ". ~nuxt/.nvm/nvm.sh && nvm install --no-progress 20"

ADD docker/postgres-wrapper.sh docker/php-fpm-wrapper.sh docker/redis-wrapper.sh docker/nuxt-wrapper.sh docker/generate-api-secret.sh /usr/local/bin/
ADD docker/php-fpm.conf /etc/php/8.1/fpm/pool.d/
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Custom Nuxt .env file:
docker run --name opnform -v $PWD/custom-nuxt-env-file.env:/app/client/.env -v $PWD/my-opnform-data:/persist -p 80:80 jhumanj/opnform
```

This would load load in the env file located at `my-custom-env-file.env`, note that if you are creating a .env file for use like this it's best to start from the `.docker.env` example file as there are slightly different defaults for the dockerized setup.
This would load load in the env file located at `my-custom-env-file.env`, note that if you are creating a .env file for use like this it's best to start from the `.env.docker` example file as there are slightly different defaults for the dockerized setup.

#### Using a custom HTTP port

Expand Down
10 changes: 5 additions & 5 deletions docker/generate-api-secret.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/bin/bash
#!/bin/bash -e

main() {
( flock -n 100 || wait_for_other_instance; generate_api_secrets) 100> /var/lock/api_secret.lock
}

generate_api_secrets() {
if ! is_configured; then
SECRET=$(random_string)
add_secret_to_env_file /app/client/.env NUXT_API_SECRET $SECRET
add_secret_to_env_file /app/.env FRONT_API_SECRET $SECRET
SECRET="$(random_string)"
add_secret_to_env_file /app/client/.env NUXT_API_SECRET "$SECRET"
add_secret_to_env_file /app/.env FRONT_API_SECRET "$SECRET"
fi
}

Expand All @@ -27,7 +27,7 @@ add_secret_to_env_file() {
VAR=$2
VAL=$3

grep "^$VAR=" $FILE || ( echo $VAR= >> $FILE )
grep "^$VAR=" "$FILE" || ( echo "$VAR=" >> "$FILE" )

cp $FILE $TEMP_FILE
sed "s/^$VAR=.*$/$VAR=$VAL/" -i $TEMP_FILE
Expand Down
20 changes: 15 additions & 5 deletions docker/nuxt-wrapper.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
#!/bin/bash
#!/bin/bash -e

. /root/.nvm/nvm.sh
nvm install 20
echo + . ~nuxt/.nvm/nvm.sh
. ~nuxt/.nvm/nvm.sh

echo + nvm install --no-progress 20
nvm install --no-progress 20
echo + nvm use 20
nvm use 20

cd /app/nuxt/server/

. /app/client/.env
echo + . /app/client/.env
[ -f /app/client/.env ] && . /app/client/.env || echo "Environment file missing!"

[ "x$NUXT_API_SECRET" != "x" ] || generate-api-secret.sh
[ "x$NUXT_API_SECRET" != "x" ] || (
echo + generate-api-secret.sh
generate-api-secret.sh
)

echo + eval \$\(sed 's/^/export /' \< /app/client/.env\)
eval $(sed 's/^/export /' < /app/client/.env)

echo + node index.mjs
node index.mjs
15 changes: 10 additions & 5 deletions docker/php-fpm-wrapper.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash +ex
#!/bin/bash -ex

[ -L /app/storage ] || {
echo "Backing up initial storage directory"
Expand All @@ -19,20 +19,25 @@ chown opnform /var/log/opnform.log
echo "Linking persistent storage into app"
ln -t /app -sf /persist/storage

. /app/.env
read_env() {
set +x
. /app/.env
set -x
}
read_env

[ "x$APP_KEY" != "x" ] || {
artisan key:generate
. /app/.env
read_env
}
[ "x$JWT_SECRET" != "x" ] || {
artisan jwt:secret -f
. /app/.env
read_env
}

[ "x$FRONT_API_SECRET" != "x" ] || {
generate-api-secret.sh
. /app/.env
read_env
}

/usr/sbin/php-fpm8.1
Expand Down
6 changes: 4 additions & 2 deletions docker/postgres-wrapper.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/bash -ex

DATA_DIR=/persist/pgsql/data
CONFIG_FILE=/etc/postgresql/postgresql.conf
Expand All @@ -10,7 +10,9 @@ mkdir -p $DATA_DIR
chown postgres -R $DATA_DIR
chmod 0700 $DATA_DIR

set +x
. /app/.env
set -x

test -f $DATA_DIR/postgresql.conf || NEW_DB=true

Expand Down Expand Up @@ -41,6 +43,6 @@ EOF
fi

wait_for_database_to_be_ready
artisan migrate
sudo -u opnform artisan migrate --force

wait
2 changes: 1 addition & 1 deletion docker/redis-wrapper.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/bash -ex

sysctl vm.overcommit_memory=1
mkdir -p /persist/redis/data
Expand Down
9 changes: 2 additions & 7 deletions docker/supervisord.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,43 @@
nodaemon=true
logfile=/dev/null
logfile_maxbytes=0
user=root

[program:nginx]
command=/usr/sbin/nginx
stdout_logfile=/dev/stdout
stderr_logfile=/dev/stderr
stdout_logfile_maxbytes=0
redirect_stderr=true

[program:php-fpm]
command=/usr/local/bin/php-fpm-wrapper.sh
stdout_logfile=/dev/stdout
stderr_logfile=/dev/stderr
stdout_logfile_maxbytes=0
redirect_stderr=true

[program:php-queue]
process_name=%(program_name)s_%(process_num)02d
command=/usr/local/bin/artisan queue:work
stdout_logfile=/dev/stdout
stderr_logfile=/dev/stderr
stdout_logfile_maxbytes=0
redirect_stderr=true
numprocs=5

[program:postgres]
command=/usr/local/bin/postgres-wrapper.sh
stdout_logfile=/dev/stdout
stderr_logfile=/dev/stderr
stdout_logfile_maxbytes=0
redirect_stderr=true

[program:redis]
command=/usr/local/bin/redis-wrapper.sh
stdout_logfile=/dev/stdout
stderr_logfile=/dev/stderr
stdout_logfile_maxbytes=0
redirect_stderr=true

[program:nuxt-backend]
command=/usr/local/bin/nuxt-wrapper.sh
stdout_logfile=/dev/stdout
stderr_logfile=/dev/stderr
stdout_logfile_maxbytes=0
redirect_stderr=true

user=nuxt

0 comments on commit 1dd02cc

Please sign in to comment.