How to use docker secret/environment variable in golang dockertest.resource instead of hardcoded password

Issue

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"})

Solution

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

Answered By – Jun Pan

Answer Checked By – Pedro (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.