I have a docker-compose file that connects an app with a database. I'm using docker secrets to hide sensitive data, such as in this case DB credentials, while I can do so with the database service, I'm not able to do the same within the app service, specifically the ConnectionString. I couldn't find much in the documentation regarding this, neither did I find such cases online, there is one example that is given in the documentation with Wordpress service that suggests the solution I'm seeking, but that seems to be only Wordpress related. Where do I go from here?
Wordpress example in documentation:
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD_FILE: /run/secrets/db_password
secrets:
- db_password
My docker-compose file:
version: '3.8'
services:
testapp:
image: testapp
depends_on:
- testapp_db
build:
context: .
dockerfile: TestApp/Dockerfile
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=http://+:80
# This works
- ConnectionStrings__DefaultConnection=Server=testapp_db;Database=TestApp;UserId=postgres;Password=postgres;
# But I want to do something like this
- ConnectionStrings__DefaultConnection=/run/secrets/connection_string
ports:
- "60001:80"
secrets:
- connection_string
networks:
- testapp-network
testapp_db:
image: postgres:latest
environment:
POSTGRES_USER_FILE: /run/secrets/db_user
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
POSTGRES_DB: TestApp
ports:
- "5500:5432"
volumes:
- postgres-data:/var/lib/postgresql/data
- ./Persistence/Scripts/db_init.sql:/docker-entrypoint-initdb.d/create_tables.sql
restart: always
secrets:
- db_user
- db_password
networks:
- testapp-network
volumes:
postgres-data:
secrets:
db_password:
file: ./run/secrets/docker-postgres/db_password
db_user:
file: ./run/secrets/docker-postgres/db_user
connection_string:
file: ./run/secrets/docker-postgres/connection_string
networks:
testapp-network:
driver: bridge