Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Node JS set env

How to set node ENV process.env.mysql-host with docker run?

Can i somehow do like this? docker run --mysql-host:127.0.0.1 -p 80:80 -d myApp

I am using FROM node:onbuild as image.

like image 674
Krister Johansson Avatar asked Apr 02 '26 20:04

Krister Johansson


2 Answers

Node's process.env is an object containing the user environment. Docker's CLI allows you to set the environment variable for the container using the -e or --env options.

You can run

docker run --env mysql_host=127.0.0.1 -p 80:80 -d myApp

To pass the mysql_host into the container.

like image 99
Peter Szeto Avatar answered Apr 04 '26 11:04

Peter Szeto


I don't know much about node, but I think you just need to do:

docker run -e mysql-host=127.0.0.1 -p 80:80 -d myApp

Note that this will look for mysql-host in the same container, not on the host, if that's what you're expecting. I think what you really want to do is:

$ docker run -d --name db mysql
...
$ docker run -d --link db:mysql-host -p 80:80 -d myApp

Which will run the myApp container linked to the db container and resolvable as "mysql-host" inside the myApp container with no need for environment variables.

like image 36
Adrian Mouat Avatar answered Apr 04 '26 12:04

Adrian Mouat