Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Help] Config and docker-compose #11

Closed
otroboe opened this issue Feb 12, 2018 · 8 comments
Closed

[Help] Config and docker-compose #11

otroboe opened this issue Feb 12, 2018 · 8 comments

Comments

@otroboe
Copy link

otroboe commented Feb 12, 2018

Hey guys !

I'm using docker-compose for my mysql server, but I'm quite annoyed by having connection configuration in two different places:
docker-compose.yml
ormconfig.json

Would you have some ideas how to keep the connection in only one place ?

@otroboe
Copy link
Author

otroboe commented Feb 12, 2018

I'm closing it, it's not related to Nest, but TypeORM :-)

@otroboe otroboe closed this as completed Feb 12, 2018
@jcloutz
Copy link

jcloutz commented Feb 16, 2018

@otroboe Take a look at using a .env file for config. It has some limitations with typeorm if you want to use more than one database but it should get the job done in most cases

Docker Compose Environment Vars & Typeorm

If you couple with something like Direnv to parse the .env file when you enter the directory it should work out pretty good.

# .env
HOST=localhost
PORT=8000
NODE_ENV=development
LOG_LEVEL=debug

TYPEORM_CONNECTION=postgres
TYPEORM_HOST=localhost
TYPEORM_USERNAME=postgres
TYPEORM_PASSWORD=postgres
TYPEORM_DATABASE=project_database
TYPEORM_PORT=5432
TYPEORM_SYNCHRONIZE=false
TYPEORM_DROP_SCHEMA=false
TYPEORM_LOGGING=all
TYPEORM_ENTITIES=src/common/entity/**/*.ts
TYPEORM_MIGRATIONS=src/database/migrations/**/*.ts
TYPEORM_SUBSCRIBERS=src/database/subscribers/**/*.ts
# .envrc used for direnv to modify shell when entering directory

# add {project_dir}/bin to path for helpers
export PATH=$(PWD)/bin:$PATH

# parse .env file when entering project dir
export $(egrep -v '^#' .env | xargs)
version: '3.1'

services:
  pgsql:
    image: postgres:9
    ports:
      - ${TYPEORM_PORT}:5432
    environment:
      POSTGRES_PASSWORD: ${TYPEORM_PASSWORD}
      POSTGRES_DB: ${TYPEORM_DATABASE}
    volumes:
      - pg_data:/var/lib/postgres/data

volumes:
  pg_data:

@otroboe
Copy link
Author

otroboe commented Feb 16, 2018

Thank you ! I'll test your solution as soon as I can.

@jcloutz
Copy link

jcloutz commented Feb 17, 2018

@otroboe another cool thing you can do with direnv is create shell scripts for a specific project and modify the path for the current working directory.

This works like a charm with typeorm, i got annoyed having to invoke ts-node everytime i wanted to create or run migrations.

#!/bin/bash
#/path/to/program/bin/typeorm

./node_modules/.bin/ts-node ./node_modules/.bin/typeorm $1 $2 $3 $4 $5

@joshuamanns
Copy link

@jcloutz you can also load the .env file within docker-compose using env_file instead of enumerating each value:

version: '3.1'

services:
  pgsql:
    image: postgres:9
    ports:
      - ${TYPEORM_PORT}:5432
    env_file:
      - .env
    volumes:
      - pg_data:/var/lib/postgres/data

volumes:
  pg_data:

@otroboe
Copy link
Author

otroboe commented Mar 27, 2018

You don't need to do that with docker-compose, it loads the .env file automatically.
https://docs.docker.com/compose/environment-variables/#the-env-file

@joshuamanns
Copy link

@otroboe That's convenient! I guess in my habit of being explicit with Docker, that nuance got lost on me.

@otroboe
Copy link
Author

otroboe commented Mar 27, 2018

You can still use the env_file option for files not called .env ;-)
Like if you need different env files for each container.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants