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
If you are using 'docker-compose' as the method to spin up your container(s), there is actually a useful way to pass an environment variable defined on your server to the Docker container.
In your
docker-compose.yml
file, let's say you are spinning up a basic hapi-js container and the code looks like:Let's say that the local server that your docker project is on has an environment variable named 'NODE_DB_CONNECT' that you want to pass to your hapi-js container, and you want its new name to be 'HAPI_DB_CONNECT'. Then in the
docker-compose.yml
file, you would pass the local environment variable to the container and rename it like so:I hope this helps you to avoid hard-coding a database connect string in any file in your container!
You can pass using
-e
parameters withdocker run ..
command as mentioned here and as mentioned by @errata.However, the possible downside of this approach is that your credentials will be displayed in the process listing, where you run it.
To make it more secure, you may write your credentials in a configuration file and do
docker run
with--env-file
as mentioned here. Then you can control the access of that config file so that others having access to that machine wouldn't see your credentials.You can pass environment variables to your containers with the
-e
flag.An example from a startup script:
Or, if you don't want to have the value on the command-line where it will be displayed by
ps
, etc.,-e
can pull in the value from the current environment if you just give it without the=
:If you have many environment variables and especially if they're meant to be secret, you can use an env-file: