Skip to content

Commit bf260a4

Browse files
committed
Add init scripts to Docker image
1 parent e29902e commit bf260a4

File tree

10 files changed

+74
-23
lines changed

10 files changed

+74
-23
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ COPY docker/php.ini /etc/php82/conf.d/
7676
COPY docker/php-fpm.conf /etc/php82/
7777
COPY docker/supervisord.conf /etc/
7878
COPY docker/process /srv/process/
79+
COPY docker/scripts /srv/scripts/
7980

8081
USER dirigent
8182

docker/init.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,13 @@
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/consumer.sh

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
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: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@
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/postgresql.sh

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,10 @@
22

33
set -e
44

5-
function shutdown()
6-
{
5+
function shutdown() {
76
pkill postgres
87
}
98

10-
if [ ! -d "/srv/data/postgresql" ]; then
11-
mkdir -p /srv/data/postgresql
12-
initdb /srv/data/postgresql
13-
fi
14-
159
trap shutdown HUP INT QUIT ABRT KILL ALRM TERM TSTP
1610

1711
exec postgres -D /srv/data/postgresql
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
if [ -d "/srv/data/postgresql" ]; then
6+
echo "Database directory found"
7+
8+
# Start Postgres server
9+
pg_ctl start -D /srv/data/postgresql
10+
11+
exit 0
12+
fi
13+
14+
echo "Creating PostgreSQL database..."
15+
16+
# Create database directory
17+
mkdir -p /srv/data/postgresql
18+
19+
# Initialize database storage
20+
initdb /srv/data/postgresql
21+
22+
# Start Postgres server
23+
pg_ctl start -D /srv/data/postgresql
24+
25+
# Create database
26+
createdb dirigent
27+
28+
echo "Created PostgreSQL database"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
composer run-script --no-ansi --no-interaction auto-scripts
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
bin/console doctrine:migrations:sync-metadata-storage --no-ansi --no-interaction
6+
bin/console doctrine:migrations:migrate --allow-no-migration --no-ansi --no-interaction
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
# Stop Postgres server
6+
pg_ctl stop -D /srv/data/postgresql

tests/Docker/Standalone/DockerStandaloneTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ protected function setUp(): void
1515
{
1616
$this->container = (new GenericContainer('dirigent-standalone'))
1717
->withExposedPorts(7015)
18-
->withMount(__DIR__ . '/scripts', '/srv/tests')
18+
->withMount(__DIR__ . '/scripts', '/srv/scripts/tests')
1919
->withWait(new WaitForLog('ready to handle connections'))
2020
->start();
2121
}
@@ -27,7 +27,7 @@ protected function tearDown(): void
2727

2828
protected function assertCommandSuccessful(array $command, ?string $message = null): void
2929
{
30-
$result = $this->container->exec(['sh', '/srv/tests/command-successful.sh', ...$command]);
30+
$result = $this->container->exec(['sh', '/srv/scripts/tests/command-successful.sh', ...$command]);
3131
if ('0' === $result) {
3232
$this->addToAssertionCount(1);
3333
} else {

0 commit comments

Comments
 (0)