0

There is a docker php container run by docker run command.

1.How to pass by using docker run command a variable to the php container?

2.How to read value of the passed variable from php script in that container?

Context: PHP script inside a docker container connects to a database that is located on an another container. I need to run several php containers that connect to differently named database containers.

I can't have hardcoded database container name inside the php script and need to pass database conatainer name dynamically when I use docker run to startup php containers.

Jimmix
  • 4,744
  • 2
  • 25
  • 56
  • You can set environment variables - https://stackoverflow.com/questions/30494050/how-do-i-pass-environment-variables-to-docker-containers – Nigel Ren Feb 06 '19 at 17:08

1 Answers1

0

You can pass an environment variable using the -e DB_NAME='hostname' as mentioned in this answer https://stackoverflow.com/a/30494145/2940966.

The environment variable can be accessed from the PHP script as follows.

echo 'The DB Name is ' .$_ENV["DB_NAME"];
joseph
  • 829
  • 10
  • 19