I'm new to Docker, and it's unclear how to access an external database from a container. Is the best way to hard-code in the connection string?
# Dockerfile
ENV DATABASE_URL amazon:rds/connection?string
I'm new to Docker, and it's unclear how to access an external database from a container. Is the best way to hard-code in the connection string?
# Dockerfile
ENV DATABASE_URL amazon:rds/connection?string
Use
-e
or --env value to set environment variables (default []).An example from a startup script:
If you want to use multiple environments from the command line then before every environment variable use the
-e
flag.Example:
If you need to set up many variables, use the
--env-file
flagFor example,
For any other help, look into the Docker help:
Official documentation: https://docs.docker.com/compose/environment-variables/
Using
docker-compose
, the example below shows how you can inherit shell env variables within both docker-compose.yml and in turn any Dockerfile(s) called bydocker-compose
to build images. I've found this useful if say in theDockerfile
RUN
command I need to execute commands specific to the environment.(your shell has
RAILS_ENV=development
already existing in the environment)docker-compose.yml:
Dockerfile:
This way I dont need to specify environment variables in files or
docker-compose
build
/up
commands:For Amazon AWS ECS/ECR, you should manage your environment variables (especially secrets) via a private S3 bucket. See blog post How to Manage Secrets for Amazon EC2 Container Service–Based Applications by Using Amazon S3 and Docker.
Another way is to use the powers of
/usr/bin/env
:If you have the environment variables in an
env.sh
locally and want to set it up when the container starts, you could tryThis command would start the container with a bash shell (I want a bash shell since
source
is a bash command), sources theenv.sh
file(which sets the environment variables) and executes the jar file.The
env.sh
looks like this,I added the
printenv
command only to test that actual source command works. You should probably remove it when you confirm the source command works fine or the environment variables would appear in your docker logs.There is a nice hack how to pipe host machine environment variables to a docker container: