Krister Johansson
Krister Johansson

Reputation: 711

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.

Upvotes: 3

Views: 5487

Answers (3)

you could also set node ENV process.env.mysql-host inside your dockerfile

FROM node:latest

WORKDIR /home/app

ADD . /home/app

ENV PORT 3000 
ENV mysql-host 127.0.0.1

EXPOSE 3000

Upvotes: 2

Peter Szeto
Peter Szeto

Reputation: 356

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.

Upvotes: 5

Adrian Mouat
Adrian Mouat

Reputation: 46470

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.

Upvotes: 5

Related Questions