GithubHelp home page GithubHelp logo

Comments (13)

aanand avatar aanand commented on July 28, 2024 2

@jamestenglish That would be cool, but the environment variables added by links are pretty unnecessary anyway, since:

  1. An entry is added to /etc/hosts with the name of the link (in this case, db)
  2. The port given to web will always be the one exposed by db, (in this case, 3306).

(This is why we no longer recommend using the environment variables provided by links.)

You can therefore just write:

environment:
  DATABASE_URL: mysql://user:password@db:3306/db

from compose.

bfirsh avatar bfirsh commented on July 28, 2024

Yes! We were talking about this yesterday and I would really like this to work. I haven't thought a whole lot about how it should be implemented though โ€“ย it's a little complex because the env variables aren't available until the container has been started (I think).

Maybe we could run a one off container that simply runs env to get the environment the container is going to see, then use that to set extra custom environment variables.

from compose.

SkinyMonkey avatar SkinyMonkey commented on July 28, 2024

As you said the environment variables are not available until you run the container.
The thing is that environment variables injected by docker, like the one for the links, as in your exemple :

"DB_1_PORT_3306_TCP_ADDR"

will not be the same between two instances of db, even if they have the same name.
For example here the IP won't be the same, except if you change the network settings (i assume, never tried though)

I thought that you could do this:

db:
     image: example/mysql

test:
      links:
            - db
      environment:
            - MYSQL_USER=root
            - MYSQL_ROOT=toor
            -DATABASE_URL=mysql://$MYSQL_ROOT:$MYSQL_PASSWORD$DB_1_PORT_3306_TCP_ADDR/db

But Docker is simply injecting the environment variables without expanding them ... too bad.
That could be a feature to add directly to docker though.

from compose.

sherzberg avatar sherzberg commented on July 28, 2024

@SkinyMonkey i agree this would be a great addition to docker. are you familiar with the code? any idea where to start poking?

@bfirsh until then, any thoughts on adding this here? id love to send a pull request, but not sure where in your code is the best to do this? i see a few places in container and service where it could live. if you have thought about this, let me know where to start i can give it a shot. maybe a proof of concept here can vet out some ideas for attempting to get this in docker itself.

from compose.

thestonefox avatar thestonefox commented on July 28, 2024

Hi,

Could this be extended to being able to reference environment variables from the host machine to be injected into the docker container via fig?

e.g.

environment:

  • DB_PASSWORD=$DB_PASSWORD <--this being an env variable on the host machine

from compose.

aanand avatar aanand commented on July 28, 2024

@thestonefox Yep, useful feature. See #76 - still under discussion.

from compose.

bajohns avatar bajohns commented on July 28, 2024

Greetings - looks like the discussion might be resolved - any chance this will make it into the next release?

Thanks!

from compose.

aanand avatar aanand commented on July 28, 2024

Closed by #297.

from compose.

pnegahdar avatar pnegahdar commented on July 28, 2024

@aanand I don't think this was actually close by #297, the issue in question here is reassigning autoassigned env vars to another env var (like one the codebase already expects), i.e an alias. 297 seems to allow blank envs to resolve to the host env.

from compose.

jamestenglish avatar jamestenglish commented on July 28, 2024

@aanand I agree with @pnegahdar the original use case:

web:
  build: .
  ports:
   - 8000:8000
  links:
   - db
  environment:
   DATABASE_URL: mysql://user:password@${DB_1_PORT_3306_TCP_ADDR}/db
db:
  image: orchardup/mysql

Does not seem to be supported by #297

specifically how would one be able to set an environment variable based on other environment variables like this line: DATABASE_URL: mysql://user:password@${DB_1_PORT_3306_TCP_ADDR}/db

I think this is an important use case as apps should not have to be burdened with changing code to support esoteric environment variable names like: DB_1_PORT_3306_TCP_ADDR

from compose.

jamestenglish avatar jamestenglish commented on July 28, 2024

@aanand Ah thanks, I had been out of the docker world for a little bit and didn't realize they added that feature

from compose.

dimroc avatar dimroc commented on July 28, 2024

Thanks a lot @aanand, your approach is perfect.

from compose.

kelvv avatar kelvv commented on July 28, 2024

@aanand Thank you , perfect

from compose.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.