Home > Blockchain >  How to use docker secret/environment variable in golang dockertest.resource instead of hardcoded pas
How to use docker secret/environment variable in golang dockertest.resource instead of hardcoded pas

Time:04-01

We use the below code for unit testing the services that talk to a database system.

https://sergiocarracedo.es/integration-tests-in-golang-with-dockertest/

MySQL root password is hardcoded in the particular line and creates security issues. Is there any way we can pass that as env variable or docker secret ?

resource, err := pool.Run("mysql", "5.7", []string{"MYSQL_ROOT_PASSWORD=secret"})

CodePudding user response:

You can use the environment variable.

  1. First of all, get the env variable via os.Getenv() in your code
mysqlPwd := os.Getenv("MYSQL_ROOT_PASSWORD")
  1. Then run the docker with the -e option
docker run -e MYSQL_ROOT_PASSWORD=secret
  • Related