Skip to content

Commit e0edb67

Browse files
committed
ROX-19980 pre-built scanner-db image
1 parent 0811d63 commit e0edb67

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

image/db/rhel/Dockerfile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,16 @@ USER 70:70
5959

6060
COPY --from=extracted_bundle /bundle/docker-entrypoint-initdb.d/definitions.sql.gz /docker-entrypoint-initdb.d/
6161

62-
ENTRYPOINT ["docker-entrypoint.sh"]
62+
COPY scripts/custom-entrypoint.sh /usr/local/bin/
63+
COPY scripts/start-db.sh /usr/local/bin/
64+
65+
RUN /usr/local/bin/start-db.sh
66+
USER root
67+
RUN rm -rf /usr/local/bin/start-db.sh && \
68+
rm -rf /docker-entrypoint-initdb.d/definitions.sql
69+
USER 70:70
70+
ENV DATABASE_ALREADY_EXISTS=true
71+
ENTRYPOINT ["custom-entrypoint.sh"]
6372

6473
EXPOSE 5432
6574
CMD ["postgres", "-c", "config_file=/etc/postgresql.conf"]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env bash
2+
3+
# The postgres server has been started once during the build process in the Dockerfile.
4+
# Now we need to start it again, but this time with the correct password.
5+
# So we need to issue a command to change the password.
6+
7+
set -e
8+
9+
echo "Starting database..."
10+
POSTGRES_PASSWORD=postgres /usr/local/bin/docker-entrypoint.sh postgres -c config_file=/etc/postgresql.conf &
11+
12+
echo "Waiting for database to be ready..."
13+
while ! pg_isready -U postgres -h localhost -p 5432; do
14+
sleep 1
15+
done
16+
17+
echo "Changing password..."
18+
if [ "$POSTGRES_PASSWORD" != "postgres" ]; then
19+
PGPASSWORD=postgres psql -c "ALTER USER postgres WITH PASSWORD '$POSTGRES_PASSWORD';"
20+
fi
21+
22+
echo "Renaming postgres user if necessary..."
23+
if [ "$POSTGRES_USER" != "postgres" ]; then
24+
PGPASSWORD="$POSTGRES_PASSWORD" psql -c "ALTER USER postgres RENAME TO $POSTGRES_USER;"
25+
fi
26+
27+
echo "Stopping database..."
28+
pg_ctl -D /var/lib/postgresql/data/pgdata -w stop
29+
30+
# Now we can start the database for real. But we will
31+
# forward any arguments to the actual entrypoint script
32+
echo "Starting database for real..."
33+
34+
exec /usr/local/bin/docker-entrypoint.sh "$@"

image/db/rhel/scripts/docker-entrypoint.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
###
77
### [1]: https://github.com/docker-library/postgres/blob/master/12/bullseye/docker-entrypoint.sh
88

9+
if [ -n "$ROX_SCANNER_DB_INIT" ]; then
10+
exit 0
11+
fi
12+
913
set -Eeo pipefail
1014
# TODO swap to -Eeuo pipefail above (after handling all potentially-unset variables)
1115

image/db/rhel/scripts/start-db.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
3+
set -eu
4+
5+
echo "Creating postgres.conf for initialization..."
6+
cat <<EOF > /tmp/postgres.conf
7+
listen_addresses = '*'
8+
EOF
9+
10+
echo "Starting database..."
11+
POSTGRES_PASSWORD=postgres /usr/local/bin/docker-entrypoint.sh postgres -c config_file=/tmp/postgres.conf
12+
13+
echo "Waiting for database to stop..."
14+
pg_ctl -D /var/lib/postgresql/data/pgdata -w stop
15+
16+
rm /tmp/postgres.conf

0 commit comments

Comments
 (0)