'How to access docker secret from orther container
I have a docker compose file for my nodejs application. I need to hide the connection string and take it out from docker secret and assign it to the env variable to initiate the database connection. But the env being assigned seems to be a path rather than the desired secret value. Please guide me. Thank you.
here is my docker-compose.yml
version: "3.1"
services:
web_server:
image: "nginxdemos/hello"
secrets:
- secret_value
environment:
NODE_ENV: production
SECRET_ENV: /run/secrets/secret_value
ports:
- "80:5001"
secrets:
secret_value:
external: true
My index.js
router.get('/', function(req, res, next) {
res.send('Server is running ' + process.env.SECRET_ENV);
});
Commands:
nguyenthanh@MacBook-Pro-cua-Nguyen NextZone % docker secret ls
ID NAME DRIVER CREATED UPDATED
tw9c2q55u511s5xt0n46s9cam db_host 2 days ago 2 days ago
plslnygpghdh1jljz8iytzjq4 my_external_secret 2 days ago 2 days ago
z8jalp982098swxy4abycl6so secret_value 2 hours ago 2 hours ago
nguyenthanh@MacBook-Pro-cua-Nguyen NextZone % docker stack deploy --compose-file=docker-compose.yml secret_test
Creating service secret_test_web_server
nguyenthanh@MacBook-Pro-cua-Nguyen NextZone % docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9635bd581a80 nginxdemos/hello:latest "/docker-entrypoint.…" 11 seconds ago Up 11 seconds 80/tcp secret_test_web_server.1.b6wg1s5usb6euaw79unpbh9f7
nguyenthanh@MacBook-Pro-cua-Nguyen NextZone % docker exec 9635bd581a80 cat /run/secrets/secret_value
sec180499
nguyenthanh@MacBook-Pro-cua-Nguyen NextZone % docker exec 9635bd581a80 cat printenv
cat: can't open 'printenv': No such file or directory
nguyenthanh@MacBook-Pro-cua-Nguyen NextZone % docker exec 9635bd581a80 printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=9635bd581a80
NODE_ENV=production
SECRET_ENV=/run/secrets/secret_value
NGINX_VERSION=1.21.6
NJS_VERSION=0.7.2
PKG_RELEASE=1
HOME=/root
I'm expecting my env to look like this SECRET_ENV=sec180499
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
