I'm trying to hide some sensitive information about my application with docker secrets. I did great in most of them but I'm having trouble with the env of the database connection.
When I pass the string to connect the database through the secrets, I get this error:
sqlalchemy.exc.ArgumentError: Could not parse rfc1738 URL from string 'db_lang_driverdb_userdb_passworddb_hostdb_database'
I created the secrets in .txt files at my local machine.
This is my docker-compose file
version: '3.3'
services:
stripe:
image: stripe-full-env
container_name: stripe
ports:
- "7000:7000"
secrets:
- stripe-secret-key
- db_lang_driver
- db_user
- db_password
- db_host
- db_database
environment:
- STRIPE_SECRET_KEY=stripe-secret-key
- DB_LANG_DRIVER=db_lang_driver
- DB_USER=db_user
- DB_PASSWORD=db_password
- DB_HOST=db_host
- DB_DATABASE=db_database
secrets:
stripe-secret-key:
file: ./stripe-secret-key.txt
db_lang_driver:
file: ./db_lang_driver.txt
db_user:
file: ./db_user.txt
db_password:
file: ./db_password.txt
db_host:
file: ./db_host.txt
db_database:
file: ./db_database.txt
ALL of the ENV variables except the STRIPE_SECRET_KEY, are from the database connection string that I'm concatenating at the code written in python. The stripe-secret-key secret is doing ok. But when the code need to access the strConnection2, it doesn't get the information that is being passed by the secrets.
strConnection2 = os.getenv('DB_LANG_DRIVER') + os.getenv('DB_USER') + os.getenv('DB_PASSWORD') + os.getenv('DB_HOST') + os.getenv('DB_DATABASE')
if strConnection2 == None:
raise TypeError("Database string not found")
engine = create_engine(
strConnection2,
echo=False,
pool_recycle=900,
pool_pre_ping=True,
pool_size=1000,
max_overflow=2000
)
I tried to use external and file secrets. Both showed the sql alchemy error.
Important. The same string when used in Dockerfile works fine to connect the database.
- I tried using the string not concatenated with one long env variable.
- Tried using the concatenated string with multiple env variables
- Used external secrets in Swarm trough docker secret create
- Used secrets created manually at my local machine
B_LANG_DRIVER->db_lang_driver. So your docker compose file isn't picking up the values from the environment. Are they being set?