Skip to content

Commit 11d64d6

Browse files
committed
Fix issue with Postgres Docker image requiring POSTGRES_PASSWORD envvar
1 parent 885cbc8 commit 11d64d6

File tree

3 files changed

+72
-29
lines changed

3 files changed

+72
-29
lines changed

docker-compose.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ services:
112112
# This runs the database that everything else connects to
113113
db:
114114
image: postgres:11
115+
env_file:
116+
- .env
115117

116118
# Unlike above, the Postgres image cannot start as our user account
117119
# otherwise we get permission denied errors. So this one has to run as
@@ -129,7 +131,10 @@ services:
129131
restart: unless-stopped
130132
networks:
131133
- default
132-
command: postgres -c logging_collector=on -c log_destination=stderr -c log_directory=/var/lib/postgresql/data/logs -c log_rotation_age=60 -c log_truncate_on_rotation=on -c log_filename=server_log.hour.%H%M
134+
command:
135+
postgres -c logging_collector=on -c log_destination=stderr -c
136+
log_directory=/var/lib/postgresql/data/logs -c log_rotation_age=60 -c
137+
log_truncate_on_rotation=on -c log_filename=server_log.hour.%H%M
133138

134139
networks:
135140
default:

docker/docker.env

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ FORCE_COLOR=2
77
# `.gmrc` checks for interactive TTY.
88
PG_DUMP=docker-compose exec -T db pg_dump
99

10-
# Instructs to use our docker PostgreSQL service.
11-
DATABASE_HOST=db
12-
ROOT_DATABASE_URL=postgres://postgres@db/template1
1310

1411
# Drops tables without asking in `yarn setup`. Reasoning: 1) docker-compose is
1512
# not tty, 2) it's a dev env anyway.

docker/scripts/yarn-setup.js

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
const { spawnSync: baseSpawnSync } = require("child_process");
22
const { basename, dirname, resolve } = require("path");
33
const platform = require("os").platform();
4+
const { safeRandomString } = require("../../scripts/lib/random");
5+
const fsp = require("fs").promises;
6+
7+
const DOTENV_PATH = `${__dirname}/../../.env`;
48

59
if (platform !== "win32" && !process.env.UID) {
610
console.error(
@@ -23,28 +27,65 @@ function spawnSync(command, args, options = {}) {
2327
}
2428
}
2529

26-
// The `docker-compose` project name defaults to the directory name containing
27-
// `docker-compose.yml`, which is the root folder of our project. Let's call
28-
// that 'ROOT'. We're in ROOT/docker/scripts and we want to get the name of
29-
// ROOT:
30-
const projectName = basename(dirname(dirname(resolve(__dirname))));
31-
32-
// On Windows we must run 'yarn.cmd' rather than 'yarn'
33-
const yarnCmd = platform === "win32" ? "yarn.cmd" : "yarn";
34-
35-
spawnSync(yarnCmd, ["down"]);
36-
spawnSync(yarnCmd, ["db:up"]);
37-
38-
// Fix permissions
39-
spawnSync(yarnCmd, [
40-
"compose",
41-
"run",
42-
"server",
43-
"sudo",
44-
"bash",
45-
"-c",
46-
"chmod o+rwx /var/run/docker.sock && chown -R node /work/node_modules /work/@app/*/node_modules",
47-
]);
48-
49-
// Run setup as normal
50-
spawnSync(yarnCmd, ["compose", "run", "server", "yarn", "setup", projectName]);
30+
async function main() {
31+
// POSTGRES_PASSWORD must be set for the Docker Postgres image to boot
32+
let data;
33+
try {
34+
data = await fsp.readFile(DOTENV_PATH, "utf8");
35+
} catch (e) {
36+
data = "";
37+
}
38+
if (!data.includes("POSTGRES_PASSWORD=")) {
39+
// We cannot use `dotenv` here because we exist outside of Docker, and we
40+
// don't have the module installed yet.
41+
const password = safeRandomString(30);
42+
data += `
43+
# POSTGRES_PASSWORD is the superuser password for PostgreSQL, it's required to
44+
# initialize the Postgres docker volume.
45+
POSTGRES_PASSWORD=${password}
46+
47+
# We're accessing Postgres via Docker:
48+
DATABASE_HOST=db
49+
ROOT_DATABASE_URL=postgres://postgres:${password}@db/template1
50+
`;
51+
await fsp.writeFile(DOTENV_PATH, data);
52+
}
53+
54+
// The `docker-compose` project name defaults to the directory name containing
55+
// `docker-compose.yml`, which is the root folder of our project. Let's call
56+
// that 'ROOT'. We're in ROOT/docker/scripts and we want to get the name of
57+
// ROOT:
58+
const projectName = basename(dirname(dirname(resolve(__dirname))));
59+
60+
// On Windows we must run 'yarn.cmd' rather than 'yarn'
61+
const yarnCmd = platform === "win32" ? "yarn.cmd" : "yarn";
62+
63+
spawnSync(yarnCmd, ["down"]);
64+
spawnSync(yarnCmd, ["db:up"]);
65+
66+
// Fix permissions
67+
spawnSync(yarnCmd, [
68+
"compose",
69+
"run",
70+
"server",
71+
"sudo",
72+
"bash",
73+
"-c",
74+
"chmod o+rwx /var/run/docker.sock && chown -R node /work/node_modules /work/@app/*/node_modules",
75+
]);
76+
77+
// Run setup as normal
78+
spawnSync(yarnCmd, [
79+
"compose",
80+
"run",
81+
"server",
82+
"yarn",
83+
"setup",
84+
projectName,
85+
]);
86+
}
87+
88+
main().catch(e => {
89+
console.error(e);
90+
process.exit(1);
91+
});

0 commit comments

Comments
 (0)