Skip to content

Commit 94cc8d0

Browse files
authored
Merge pull request #3 from codedmonkey/docker-scripts
Improve standalone Docker image with init scripts
2 parents 9018bea + 4a80b18 commit 94cc8d0

24 files changed

+297
-67
lines changed

.dockerignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
/.github/
2+
/.idea/
23
/config/dirigent.yaml
34
/config/packages/dirigent.yaml
45
/node_modules/
6+
/public/build/
7+
/public/bundles/
58
/storage/
9+
/tests/
610
/var/
711
/vendor/

Dockerfile

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ RUN composer install \
1515
--no-scripts \
1616
--prefer-dist
1717

18-
FROM node:latest AS node_build
18+
FROM node:23 AS node_build
1919

2020
WORKDIR /srv/app
2121

@@ -35,6 +35,8 @@ LABEL org.opencontainers.image.licenses=FSL-1.1-MIT
3535
ARG UID=1000
3636
ARG GID=1000
3737

38+
COPY docker/entrypoint.sh docker/init.sh /srv/
39+
3840
RUN set -e; \
3941
addgroup -g $GID -S dirigent; \
4042
adduser -u $UID -S -G dirigent dirigent; \
@@ -43,6 +45,7 @@ RUN set -e; \
4345
caddy \
4446
curl \
4547
git \
48+
openssl \
4649
php82 \
4750
php82-ctype \
4851
php82-curl \
@@ -64,44 +67,45 @@ RUN set -e; \
6467
supervisor; \
6568
ln -s /usr/sbin/php-fpm82 /usr/sbin/php-fpm; \
6669
mkdir -p /run/postgresql /srv/config /srv/data; \
67-
chown -R dirigent:dirigent /run /srv;
70+
chown -R dirigent:dirigent /run /srv; \
71+
chmod +x /srv/entrypoint.sh /srv/init.sh;
6872

6973
COPY --from=composer_build /usr/bin/composer /usr/bin/composer
7074

71-
COPY docker/init.sh /
7275
COPY docker/Caddyfile /etc/caddy/
7376
COPY docker/php.ini /etc/php82/conf.d/
7477
COPY docker/php-fpm.conf /etc/php82/
7578
COPY docker/supervisord.conf /etc/
7679
COPY docker/process /srv/process/
80+
COPY docker/scripts /srv/scripts/
7781

7882
USER dirigent
7983

80-
ENV APP_ENV="prod"
81-
ENV DATABASE_URL="postgresql://dirigent@127.0.0.1:5432/dirigent?serverVersion=16&charset=utf8"
82-
ENV DIRIGENT_IMAGE=1
83-
8484
WORKDIR /srv/app
8585

86-
COPY --chown=dirigent:dirigent --from=composer_build /srv/app ./
87-
COPY --chown=dirigent:dirigent --from=node_build /srv/app/public/build public/build/
88-
COPY --chown=dirigent:dirigent readme.md license.md ./
89-
COPY --chown=dirigent:dirigent .env.dirigent ./
90-
COPY --chown=dirigent:dirigent bin bin/
91-
COPY --chown=dirigent:dirigent config config/
92-
COPY --chown=dirigent:dirigent migrations migrations/
93-
COPY --chown=dirigent:dirigent public public/
94-
COPY --chown=dirigent:dirigent src src/
95-
COPY --chown=dirigent:dirigent translations translations/
96-
COPY --chown=dirigent:dirigent templates templates/
86+
COPY --chown=$UID:$GID --from=composer_build /srv/app ./
87+
COPY --chown=$UID:$GID --from=node_build /srv/app/public/build public/build/
88+
COPY --chown=$UID:$GID readme.md license.md ./
89+
COPY --chown=$UID:$GID bin/console bin/dirigent bin/
90+
COPY --chown=$UID:$GID docker/config.yaml config/dirigent.yaml
91+
COPY --chown=$UID:$GID docker/env.php ./.env.dirigent.local.php
92+
COPY --chown=$UID:$GID config config/
93+
COPY --chown=$UID:$GID docs docs/
94+
COPY --chown=$UID:$GID migrations migrations/
95+
COPY --chown=$UID:$GID public public/
96+
COPY --chown=$UID:$GID src src/
97+
COPY --chown=$UID:$GID translations translations/
98+
COPY --chown=$UID:$GID templates templates/
9799

98100
RUN set -e; \
99101
chmod +x bin/console; \
100102
chmod +x bin/dirigent; \
101-
composer dump-autoload --classmap-authoritative --no-ansi --no-interaction
103+
composer dump-autoload --classmap-authoritative --no-ansi --no-interaction;
102104

105+
VOLUME /srv/config
103106
VOLUME /srv/data
104107

105108
EXPOSE 7015
106109

107-
CMD ["sh", "/init.sh"]
110+
ENTRYPOINT ["/srv/entrypoint.sh"]
111+
CMD ["-init"]

docker/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
framework:
2+
secret: '%env(file:KERNEL_SECRET_FILE)%'

docker/entrypoint.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env sh
2+
3+
set -e
4+
5+
# If the first argument is `-init`, run the application. This is
6+
# also the default command.
7+
if [ "$1" = "-init" ]; then
8+
set -- /srv/init.sh
9+
else
10+
# If the first argument is `--`, execute the remaining arguments as a
11+
# new command, otherwise pass the arguments to the Dirigent binary.
12+
if [ "$1" = "--" ]; then
13+
set -- ${@:2}
14+
else
15+
set -- bin/dirigent "$@"
16+
fi
17+
fi
18+
19+
exec "$@"

docker/env.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
return [
4+
'APP_ENV' => 'prod',
5+
'DATABASE_URL' => 'postgresql://dirigent@127.0.0.1:5432/dirigent?serverVersion=16&charset=utf8',
6+
'DIRIGENT_IMAGE' => '1',
7+
'GITHUB_TOKEN' => '',
8+
'KERNEL_SECRET_FILE' => '/srv/config/secrets/kernel_secret',
9+
'MAILER_DSN' => 'null://null',
10+
'MESSENGER_TRANSPORT_DSN' => 'doctrine://default?auto_setup=0',
11+
'SENTRY_DSN' => '',
12+
'SYMFONY_DOTENV_PATH' => './.env.dirigent',
13+
'TRUSTED_PROXIES' => '',
14+
];

docker/init.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
#!/bin/sh
1+
#!/usr/bin/env sh
22

33
set -e
44

5+
# Run init scripts
6+
for file in $(find "/srv/scripts/init" -type f | sort -t '-' -k1,1n)
7+
do
8+
echo "Execute init script: $file"
9+
10+
sh "$file"
11+
done
12+
13+
# Start Supervisor
514
exec supervisord

docker/process/caddy.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/usr/bin/env sh
22

33
set -e
44

docker/process/consumer.sh

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
#!/bin/sh
1+
#!/usr/bin/env sh
22

33
set -e
44

5-
while [ -z "$(netstat -an | grep :9000)" ]; do
6-
echo "Waiting for app";
7-
sleep 5;
8-
done;
5+
while [ ! "$(netstat -an | grep :9000)" ]; do
6+
echo "Worker is waiting for application"
97

10-
exec /srv/app/bin/console messenger:consume async scheduler_packages --sleep 10
8+
sleep 5
9+
done
10+
11+
function shutdown() {
12+
bin/console messenger:stop-workers
13+
}
14+
15+
trap shutdown HUP INT QUIT ABRT KILL ALRM TERM TSTP
16+
17+
exec bin/console messenger:consume async scheduler_packages --sleep 10

docker/process/fpm.sh

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
#!/bin/sh
1+
#!/usr/bin/env sh
22

33
set -e
44

5-
composer run-script --no-ansi --no-interaction auto-scripts
5+
while [ ! $(pg_isready) ]; do
6+
echo "Application is waiting for the database"
67

7-
# todo temporary timeout for database connection
8-
while ! nc -z localhost 5432; do
9-
echo "Waiting for database connection";
10-
sleep 3;
11-
done;
12-
13-
bin/console doctrine:database:create --if-not-exists --no-ansi --no-interaction
14-
bin/console doctrine:migrations:migrate --allow-no-migration --no-ansi --no-interaction
8+
sleep 3
9+
done
1510

1611
exec php-fpm

docker/process/postgres.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env sh
2+
3+
set -e
4+
5+
function shutdown() {
6+
pkill postgres
7+
}
8+
9+
trap shutdown HUP INT QUIT ABRT KILL ALRM TERM TSTP
10+
11+
exec postgres -D /srv/data/postgresql

0 commit comments

Comments
 (0)