Docker Community Forums

Share and learn in the Docker community.

Using docker secrets in PHP app

I am trying to deploy my PHP app in the docker container. Using docker secret I want to store the password of the MySQL database. First I created a docker secret mysql_password. My docker-compose file is -

version: '3.9'
services:
    php:
        image: 'php:7.2-fpm'
        build:
            context: ./docker
            dockerfile: php.Dockerfile
        volumes:
            - './app:/var/www/html'
        environment:
            MYSQL_PASSWORD_FILE: /run/secrets/mysql_password
        secrets:
            - mysql_password

    nginx:
        image: nginx:latest
        ports:
            - '80:80'
        links:
            - 'php'
        volumes:
            - './app:/var/www/html'
            - './config/nginx:/etc/nginx/conf.d'
secrets:
    mysql_password:
        external: true
  • When I try to access the secret ‘mysql_password’ is is not returning anything. And if I try environment element ‘MYSQL_PASSWORD_FILE’, it is giving me the path of secret that is ‘/run/secrets/mysql_password’.
  • To access environment variable in PHP I have used .$_ENV[“mysql_password”]

Hi

how did you create the secret?
because that file should CONTAIN your secret.

if your secret is in a key=value format, like mysql_password=randompassword

then you can instead of “enviroment:” use “env_file”

and point it to the secret file, then you should be able to use it at $_ENV or getenv(‘mysql_password’)

I created secret using command echo "my_password" | docker secret create mysql_password.
I tried -

env_file:
        - /run/secrets/MYSQL_PASSWORD

When I run docker-compose or docker stack deploy, it is trying to find the env file at my local

ERROR: Couldn’t find env file: D:\run\secrets\MYSQL_PASSWORD