Skip to content

Commit 129e7fe

Browse files
donukJhumanJ
andauthored
Reworked docker scripts for new nuxt infrastructure (#285)
* Reworked docker scripts for new nuxt infrastructure * Switched to log mailer for default docker config --------- Co-authored-by: Julien Nahum <julien@nahum.net>
1 parent a651c60 commit 129e7fe

File tree

11 files changed

+144
-25
lines changed

11 files changed

+144
-25
lines changed

.dockerignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
.git
2-
Dockerfile
1+
/.git
2+
/Dockerfile
3+
/data

.env.docker

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ REDIS_HOST=127.0.0.1
2828
REDIS_PASSWORD=null
2929
REDIS_PORT=6379
3030

31-
MAIL_MAILER=smtp
32-
MAIL_HOST=smtp.mailtrap.io
33-
MAIL_PORT=2525
34-
MAIL_USERNAME=null
35-
MAIL_PASSWORD=null
36-
MAIL_ENCRYPTION=null
37-
MAIL_FROM_ADDRESS=null
38-
MAIL_FROM_NAME="${APP_NAME}"
31+
MAIL_MAILER=log
32+
MAIL_HOST=
33+
MAIL_PORT=
34+
MAIL_USERNAME=
35+
MAIL_PASSWORD=
36+
MAIL_ENCRYPTION=
37+
MAIL_FROM_ADDRESS=
38+
MAIL_FROM_NAME=
3939

4040
AWS_ACCESS_KEY_ID=
4141
AWS_SECRET_ACCESS_KEY=

Dockerfile

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,20 @@ ARG PHP_PACKAGES="php8.1 composer php8.1-common php8.1-pgsql php8.1-redis php8.1
22
php8.1-simplexml php8.1-bcmath php8.1-gd php8.1-curl php8.1-zip\
33
php8.1-imagick php8.1-bz2 php8.1-gmp php8.1-int php8.1-pcov php8.1-soap php8.1-xsl"
44

5-
FROM node:16 AS javascript-builder
5+
FROM node:20-alpine AS javascript-builder
66
WORKDIR /app
77

88
# It's best to add as few files as possible before running the build commands
99
# as they will be re-run everytime one of those files changes.
1010
#
1111
# It's possible to run npm install with only the package.json and package-lock.json file.
1212

13-
ADD package.json package-lock.json ./
13+
ADD client/package.json client/package-lock.json ./
1414
RUN npm install
1515

16-
ADD resources /app/resources
17-
ADD public /app/public
18-
ADD tailwind.config.js vite.config.js postcss.config.js /app/
16+
ADD client /app/
1917
RUN npm run build
2018

21-
2219
# syntax=docker/dockerfile:1.3-labs
2320
FROM --platform=linux/amd64 ubuntu:23.04 AS php-dependency-installer
2421

@@ -29,10 +26,18 @@ RUN apt-get update \
2926

3027
WORKDIR /app
3128
ADD composer.json composer.lock artisan ./
29+
30+
# NOTE: The project would build more reliably if all php files were added before running
31+
# composer install. This would though introduce a dependency which would cause every
32+
# dependency to be re-installed each time any php file is edited. It may be necessary in
33+
# future to remove this 'optimisation' by moving the `RUN composer install` line after all
34+
# the following ADD commands.
35+
3236
# Running artisan requires the full php app to be installed so we need to remove the
3337
# post-autoload command from the composer file if we want to run composer without
3438
# adding a dependency to all the php files.
3539
RUN sed 's_@php artisan package:discover_/bin/true_;' -i composer.json
40+
ADD app/helpers.php /app/app/helpers.php
3641
RUN composer install --ignore-platform-req=php
3742

3843
ADD app /app/app
@@ -61,18 +66,22 @@ ARG PHP_PACKAGES
6166
RUN apt-get update \
6267
&& apt-get install -y \
6368
supervisor nginx sudo postgresql-15 redis\
64-
$PHP_PACKAGES php8.1-fpm\
69+
$PHP_PACKAGES php8.1-fpm wget\
6570
&& apt-get clean
71+
RUN wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.39.3/install.sh | bash
72+
RUN . /root/.nvm/nvm.sh && nvm install 20
6673

67-
ADD docker/postgres-wrapper.sh docker/php-fpm-wrapper.sh docker/redis-wrapper.sh /usr/local/bin/
74+
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/
6875
ADD docker/php-fpm.conf /etc/php/8.1/fpm/pool.d/
6976
ADD docker/nginx.conf /etc/nginx/sites-enabled/default
7077
ADD docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
71-
ADD .env.docker .env
7278

7379
ADD . .
80+
ADD .env.docker .env
81+
ADD client/.env.docker client/.env
7482

75-
COPY --from=javascript-builder /app/public/build/ ./public/build/
83+
COPY --from=javascript-builder /app/.output/ ./nuxt/
84+
RUN cp -r nuxt/public .
7685
COPY --from=php-dependency-installer /app/vendor/ ./vendor/
7786

7887
RUN chmod a+x /usr/local/bin/*.sh /app/artisan \

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,18 @@ The `-v` argument creates a local directory called `my-opnform-data` which will
8484
The `--name` argument names the running container so that you can refer back to it later, with e.g. `docker stop opnform`. You can use any name you'd like.
8585

8686

87-
#### Using a custom .env file
87+
#### Using custom .env files
8888

89-
If you have a custom env file you can use this like so:
89+
If you have custom env file you can use them like so:
9090

91+
Custom Laravel .env file:
9192
```
92-
docker run --name opnform -v $PWD/my-custom-env-file.env:/app/.env -v $PWD/my-opnform-data:/persist -p 80:80 jhumanj/opnform
93+
docker run --name opnform -v $PWD/custom-laravel-env-file.env:/app/.env -v $PWD/my-opnform-data:/persist -p 80:80 jhumanj/opnform
94+
```
95+
96+
Custom Nuxt .env file:
97+
```
98+
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
9399
```
94100

95101
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.

client/.env.docker

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
NUXT_LOG_LEVEL=
2+
NUXT_PUBLIC_APP_URL=http://localhost/
3+
NUXT_PUBLIC_API_BASE=http://localhost/api
4+
NUXT_PUBLIC_AI_ENABLED=
5+
NUXT_PUBLIC_AMPLITUDE_CODE=
6+
NUXT_PUBLIC_CRISP_WEBSITE_ID=
7+
NUXT_PUBLIC_CUSTOM_DOMAINS_ENABLED=
8+
NUXT_PUBLIC_ENV=local
9+
NUXT_PUBLIC_GOOGLE_ANALYTICS_CODE=
10+
NUXT_PUBLIC_H_CAPTCHA_SITE_KEY=
11+
NUXT_PUBLIC_PAID_PLANS_ENABLED=
12+
NUXT_PUBLIC_S3_ENABLED=
13+
NUXT_API_SECRET=

client/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ logs
2323
.env
2424
.env.*
2525
!.env.example
26+
!.env.docker

docker/generate-api-secret.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
2+
3+
main() {
4+
( flock -n 100 || wait_for_other_instance; generate_api_secrets) 100> /var/lock/api_secret.lock
5+
}
6+
7+
generate_api_secrets() {
8+
if ! is_configured; then
9+
SECRET=$(random_string)
10+
add_secret_to_env_file /app/client/.env NUXT_API_SECRET $SECRET
11+
add_secret_to_env_file /app/.env FRONT_API_SECRET $SECRET
12+
fi
13+
}
14+
15+
random_string() {
16+
array=()
17+
for i in {a..z} {A..Z} {0..9};
18+
do
19+
array[$RANDOM]=$i
20+
done
21+
printf %s ${array[@]::8} $'\n'
22+
}
23+
24+
add_secret_to_env_file() {
25+
FILE=$1
26+
TEMP_FILE=/tmp/env.$$
27+
VAR=$2
28+
VAL=$3
29+
30+
grep "^$VAR=" $FILE || ( echo $VAR= >> $FILE )
31+
32+
cp $FILE $TEMP_FILE
33+
sed "s/^$VAR=.*$/$VAR=$VAL/" -i $TEMP_FILE
34+
cat $TEMP_FILE > $FILE
35+
}
36+
37+
wait_for_other_instance() {
38+
while ! is_configured; do
39+
sleep 1;
40+
done
41+
}
42+
43+
is_configured() {
44+
grep -q "FRONT_API_SECRET=.\+" /app/.env
45+
}
46+
47+
main

docker/nginx.conf

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
map $original_uri $api_uri {
2+
~^/api(/.*$) $1;
3+
default $original_uri;
4+
}
5+
16
server {
27
listen 80;
38
server_name opnform;
@@ -9,14 +14,23 @@ server {
914
index index.html index.htm index.php;
1015

1116
location / {
12-
try_files $uri $uri/ /index.php$is_args$args;
17+
proxy_pass http://localhost:3000;
18+
proxy_set_header X-Real-IP $remote_addr;
19+
proxy_set_header X-Forwarded-Host $host;
20+
proxy_set_header X-Forwarded-Port $server_port;
21+
}
22+
23+
location /api/ {
24+
set $original_uri $uri;
25+
try_files $uri $uri/ /index.php$is_args$args;
1326
}
1427

1528
location ~ \.php$ {
1629
fastcgi_split_path_info ^(.+\.php)(/.+)$;
1730
fastcgi_pass unix:/var/run/php-fpm-opnform-site.sock;
1831
fastcgi_index index.php;
1932
include fastcgi.conf;
20-
}
33+
fastcgi_param REQUEST_URI $api_uri;
34+
}
2135
}
2236

docker/nuxt-wrapper.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
. /root/.nvm/nvm.sh
4+
nvm install 20
5+
nvm use 20
6+
7+
cd /app/nuxt/server/
8+
9+
. /app/client/.env
10+
[ "x$NUXT_API_SECRET" != "x" ] || generate-api-secret.sh
11+
12+
sed 's/^/export /' < /app/.nuxt.env > env.sh
13+
14+
. env.sh
15+
16+
node index.mjs

docker/php-fpm-wrapper.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ ln -sf /persist/storage /app/storage
3030
. /app/.env
3131
}
3232

33+
[ "x$FRONT_API_SECRET" != "x" ] || {
34+
generate-api-secret.sh
35+
. /app/.env
36+
}
37+
3338
/usr/sbin/php-fpm8.1
3439

3540
tail -f /var/log/opnform.log

docker/supervisord.conf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,10 @@ stderr_logfile=/dev/stderr
4040
stdout_logfile_maxbytes=0
4141
redirect_stderr=true
4242

43+
[program:nuxt-backend]
44+
command=/usr/local/bin/nuxt-wrapper.sh
45+
stdout_logfile=/dev/stdout
46+
stderr_logfile=/dev/stderr
47+
stdout_logfile_maxbytes=0
48+
redirect_stderr=true
49+

0 commit comments

Comments
 (0)