How to Securely Manage API Database Passwords Using Secrets in a Docker Swarm Stack?

27 views Asked by At

I have a Swarm stack consisting of two services. The first service is "db," which uses the MariaDB image, and the second service is "api," a standard Node.js application. In my "db" service, I'm currently using secrets as shown below.

  .
  .

  api:
    image: api
    environment:
      - DB_HOST=db
      - DB_USER=db_user
      - DB_NAME=db_name
      - DB_PASSWORD=password1
      - ANOTHER_PASSWORD=password2
      - API_SECRET=secret
  db:
    image: mariadb
    environment:
      - MARIADB_PASSWORD_FILE=/run/secrets/DB_PASSWORD
      - MARIADB_ROOT_PASSWORD_FILE=/run/secrets/DB_PASSWORD_ROOT
      - MARIADB_DATABASE=db_name
      - MARIADB_USER=db_user
    secrets:
      - DB_PASSWORD
      - DB_PASSWORD_ROOT
    volumes:
      - 'db_data:/var/lib/mysql'
   .
   .

My question is, how can I utilize secrets in api service to manage the database password instead of manually inputting it? By the way, I attempted to implement the same mechanism in api as i did in db service, but it didn't work. Is there any way to utilize these secrets located under /run/secrets/XX in a simple manner in the container ?

0

There are 0 answers