Skip to content

Commit 8d2d157

Browse files
committed
Use docker/.env for Docker-specific secrets
1 parent 11d64d6 commit 8d2d157

File tree

3 files changed

+26
-28
lines changed

3 files changed

+26
-28
lines changed

docker-compose.yml

Lines changed: 3 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
@@ -113,7 +113,7 @@ services:
113113
db:
114114
image: postgres:11
115115
env_file:
116-
- .env
116+
- docker/.env
117117

118118
# Unlike above, the Postgres image cannot start as our user account
119119
# otherwise we get permission denied errors. So this one has to run as

docker/docker.env

Lines changed: 0 additions & 13 deletions
This file was deleted.

docker/scripts/yarn-setup.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const platform = require("os").platform();
44
const { safeRandomString } = require("../../scripts/lib/random");
55
const fsp = require("fs").promises;
66

7-
const DOTENV_PATH = `${__dirname}/../../.env`;
7+
const DOCKER_DOTENV_PATH = `${__dirname}/../.env`;
88

99
if (platform !== "win32" && !process.env.UID) {
1010
console.error(
@@ -28,27 +28,38 @@ function spawnSync(command, args, options = {}) {
2828
}
2929

3030
async function main() {
31-
// POSTGRES_PASSWORD must be set for the Docker Postgres image to boot
32-
let data;
31+
// Check that docker/.env exists
3332
try {
34-
data = await fsp.readFile(DOTENV_PATH, "utf8");
33+
await fsp.access(DOCKER_DOTENV_PATH, fs.constants.F_OK);
3534
} 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.
35+
// Does not exist, write it
4136
const password = safeRandomString(30);
42-
data += `
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+
4353
# POSTGRES_PASSWORD is the superuser password for PostgreSQL, it's required to
4454
# initialize the Postgres docker volume.
4555
POSTGRES_PASSWORD=${password}
4656
47-
# We're accessing Postgres via Docker:
57+
# We're accessing Postgres via Docker, so we must use the db host and the
58+
# relevant password.
4859
DATABASE_HOST=db
4960
ROOT_DATABASE_URL=postgres://postgres:${password}@db/template1
5061
`;
51-
await fsp.writeFile(DOTENV_PATH, data);
62+
await fsp.writeFile(DOCKER_DOTENV_PATH, data);
5263
}
5364

5465
// The `docker-compose` project name defaults to the directory name containing

0 commit comments

Comments
 (0)