docker-compose icon indicating copy to clipboard operation
docker-compose copied to clipboard

Problems using 'exec', and interacting with postgres

Open jkhartshorne opened this issue 5 years ago • 3 comments

I'm having difficulty using compose.exec. Part of this is because I think the docs are wrong. Here's what docs say:

exec(container, command, options)

However, you definitely have to include information about the docker-compose.yaml. This at least runs:

compose.exec('test_db', psql, {cwd: path.join(initDir, 'pushkin'), config: 'docker-compose.dev.yml'}, '-U postgres', -c 'create database test_db')

However, I get the following error:

'psql: FATAL: role "root" does not exist\n'

So it looks like the -U tag is being ignored. I also tried doing it this way:

compose.exec('test_db', psql -U postgres -c 'create database test_db', {cwd: path.join(initDir, 'pushkin'), config: 'docker-compose.dev.yml'})

with the same result. Here's the critical part of my docker-compose:

test_db: image: 'postgres:11' environment: POSTGRES_PASSWORD: example

Since I didn't set a root user, docker goes with the default 'postgres'. However, trying to set the user to 'root' didn't help:

test_db: image: 'postgres:11' environment: POSTGRES_USER: root POSTGRES_PASSWORD: example

jkhartshorne avatar Jun 24 '20 16:06 jkhartshorne

OK. I think this was a docs problem. The following seems to be working:

compose.exec('test_db', ['psql', '-U', 'postgres', '-c', 'create database test_db'], {cwd: path.join(initDir, 'pushkin'), config: 'docker-compose.dev.yml'})

with the following docker-compose:

test_db: image: 'postgres:11' environment: POSTGRES_PASSWORD: example

jkhartshorne avatar Jun 24 '20 16:06 jkhartshorne

docker-compose.dev.yml is definitely not a standard name for configuration file. This library doesn't do anything fancy with configuration files and relies on normal execution of docker-compose binary. This means that by default docker-compose.y[a]ml and docker-compose.override.y[a]ml files are loaded, just as the docker-compose docs say.

https://docs.docker.com/compose/extends/#understanding-multiple-compose-files

By default, Compose reads two files, a docker-compose.yml and an optional docker-compose.override.yml file.

Steveb-p avatar Jun 24 '20 17:06 Steveb-p

Sure. But for various reasons, I needed to run this command not from the directory where the yaml is. In any case, it turns out that compose.exe() will take options to use a different location and name. It's just not documented. Also, it looks like the documentation on how to specify the command itself is at the very least incomplete, but probably outright wrong. (The one example shows the command presented as a single string, whereas I could only get it to work as an an array of strings.)

jkhartshorne avatar Jun 24 '20 19:06 jkhartshorne