Skip to content

Commit 706aa32

Browse files
authored
Merge pull request #117 from graphile/fix-docker-postgres-issues
Fix docker postgres issues
2 parents 9e894a4 + 8d2d157 commit 706aa32

File tree

3 files changed

+85
-44
lines changed

3 files changed

+85
-44
lines changed

docker-compose.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ services:
1414
user: $UID
1515

1616
env_file:
17-
- docker/docker.env
17+
- docker/.env
1818
volumes:
1919
- .:/work
2020
# let's us run docker & docker-compose from inside container (used for yarn setup)
@@ -52,7 +52,7 @@ services:
5252
SETUP_MODE: dev
5353

5454
env_file:
55-
- docker/docker.env
55+
- docker/.env
5656

5757
# On Linux, this will prevent new files getting created as root, but you
5858
# may need to update the USER_UID and USER_GID in `Dockerfile` to match
@@ -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+
- docker/.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 & 16 deletions
This file was deleted.

docker/scripts/yarn-setup.js

Lines changed: 77 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 DOCKER_DOTENV_PATH = `${__dirname}/../.env`;
48

59
if (platform !== "win32" && !process.env.UID) {
610
console.error(
@@ -23,28 +27,76 @@ 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+
// Check that docker/.env exists
32+
try {
33+
await fsp.access(DOCKER_DOTENV_PATH, fs.constants.F_OK);
34+
} catch (e) {
35+
// Does not exist, write it
36+
const password = safeRandomString(30);
37+
const data = `
38+
# We'd like scripts ran through Docker to pretend they're in a normal
39+
# interactive terminal.
40+
FORCE_COLOR=2
41+
42+
# \`pg_dump\` is run from inside container, which doesn't have pg tools installed
43+
# so it needs a way to still run it. \`docker-compose run\` would start an
44+
# instance inside the current running container which doesn't work with volume
45+
# mappings, so we must use \`docker-compose exec\`. \`-T\` is needed because our
46+
# \`.gmrc\` checks for interactive TTY.
47+
PG_DUMP=docker-compose exec -T db pg_dump
48+
49+
# Drops tables without asking in \`yarn setup\`. Reasoning: 1) docker-compose is
50+
# not tty, 2) it's a dev env anyway.
51+
CONFIRM_DROP=y
52+
53+
# POSTGRES_PASSWORD is the superuser password for PostgreSQL, it's required to
54+
# initialize the Postgres docker volume.
55+
POSTGRES_PASSWORD=${password}
56+
57+
# We're accessing Postgres via Docker, so we must use the db host and the
58+
# relevant password.
59+
DATABASE_HOST=db
60+
ROOT_DATABASE_URL=postgres://postgres:${password}@db/template1
61+
`;
62+
await fsp.writeFile(DOCKER_DOTENV_PATH, data);
63+
}
64+
65+
// The `docker-compose` project name defaults to the directory name containing
66+
// `docker-compose.yml`, which is the root folder of our project. Let's call
67+
// that 'ROOT'. We're in ROOT/docker/scripts and we want to get the name of
68+
// ROOT:
69+
const projectName = basename(dirname(dirname(resolve(__dirname))));
70+
71+
// On Windows we must run 'yarn.cmd' rather than 'yarn'
72+
const yarnCmd = platform === "win32" ? "yarn.cmd" : "yarn";
73+
74+
spawnSync(yarnCmd, ["down"]);
75+
spawnSync(yarnCmd, ["db:up"]);
76+
77+
// Fix permissions
78+
spawnSync(yarnCmd, [
79+
"compose",
80+
"run",
81+
"server",
82+
"sudo",
83+
"bash",
84+
"-c",
85+
"chmod o+rwx /var/run/docker.sock && chown -R node /work/node_modules /work/@app/*/node_modules",
86+
]);
87+
88+
// Run setup as normal
89+
spawnSync(yarnCmd, [
90+
"compose",
91+
"run",
92+
"server",
93+
"yarn",
94+
"setup",
95+
projectName,
96+
]);
97+
}
98+
99+
main().catch(e => {
100+
console.error(e);
101+
process.exit(1);
102+
});

0 commit comments

Comments
 (0)