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?
# Dockerfil
If you have the environment variables in an env.sh
locally and want to set it up when the container starts, you could try
COPY env.sh /env.sh
COPY .jar /.jar
ENTRYPOINT ["/bin/bash" , "-c", "source /env.sh && printenv && java -jar /.jar"]
This command would start the container with a bash shell (I want a bash shell since source
is a bash command), sources the env.sh
file(which sets the environment variables) and executes the jar file.
The env.sh
looks like this,
#!/bin/bash
export FOO="BAR"
export DB_NAME="DATABASE_NAME"
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.