Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment and pre-defined variables are not supported in forwardPorts and keys of portsAttributes #489

Open
i-ky opened this issue Aug 14, 2024 · 7 comments
Assignees

Comments

@i-ky
Copy link

i-ky commented Aug 14, 2024

To avoid hardcoding hostnames I am using environment variables, e.g. ${POSTGRES_HOSTNAME}. This allows pointing application or any supplementary scripts to the correct host depending on the environment (e.g. in and out of dev container).
Same variables are used as hostname: in Docker Compose files:

services:
  postgres:
    image: postgres:latest
    hostname: ${POSTGRES_HOSTNAME}
    ...
  ...

And same variables should be used in forwardPorts and portsAttributes of dev container configuration:

{
    ...
    "forwardPorts": [
        "${containerEnv:POSTGRES_HOSTNAME}:5432",
        ...
    ],
    "portsAttributes": {
        "${containerEnv:POSTGRES_HOSTNAME}:5432": {
            ...
        },
        ...
    },
    ...
}

Unfortunately, forwardPorts and portsAttributes don't support variables, so it needs to be hardcoded instead:

{
    ...
    "forwardPorts": [
        "postgres:5432",
        ...
    ],
    "portsAttributes": {
        "postgres:5432": {
            ...
        },
        ...
    },
    ...
}

...meaning that hostname needs to be changed manually in two more places if the environment variable changes.

@chrmarti
Copy link
Contributor

chrmarti commented Sep 3, 2024

Does ${localEnv:POSTGRES_HOSTNAME} work?

@chrmarti chrmarti self-assigned this Sep 3, 2024
@i-ky
Copy link
Author

i-ky commented Sep 13, 2024

Hi @chrmarti!

Thank you for looking into this.

I am not sure what "work" means if we are talking about the spec here. Do you have a specific implementation in mind?

https://containers.dev/implementors/json_reference/ seems to talk about pre-defined variables as a whole. Either

Environment and pre-defined variables may be referenced in the values.

...or nothing at all. So my expectation is that if containerEnv: does not work, neither will localEnv:.

@chrmarti
Copy link
Contributor

containerEnv: only works with container env variables and I don't see from your report if you set POSTGRES_HOSTNAME in the container. I do see that you are using the local env variable POSTGRES_HOSTNAME in the docker-compose.yml, so localEnv: might work, depending on where you set POSTGRES_HOSTNAME locally.

@i-ky
Copy link
Author

i-ky commented Sep 17, 2024

I don't see from your report if you set POSTGRES_HOSTNAME in the container. I do see that you are using the local env variable POSTGRES_HOSTNAME in the docker-compose.yml, so localEnv: might work, depending on where you set POSTGRES_HOSTNAME locally.

Sorry, I didn't mention it. POSTGRES_HOSTNAME is set in .env file (from there POSTGRES_HOSTNAME gets into Compose file) and .env file is also passed to the dev container in Compose file as env_file: (this is how POSTGRES_HOSTNAME gets into container environment).

Since POSTGRES_HOSTNAME is not set in host environment, I think that localEnv: will not work. Do you want me to try?

@LabTopProductions
Copy link

LabTopProductions commented Sep 17, 2024 via email

@chrmarti
Copy link
Contributor

Since POSTGRES_HOSTNAME is not set in host environment, I think that localEnv: will not work. Do you want me to try?

You're right. Support for env files has come up before. Related: microsoft/vscode-remote-release#3007

@i-ky
Copy link
Author

i-ky commented Sep 18, 2024

Since POSTGRES_HOSTNAME is not set in host environment, I think that localEnv: will not work. Do you want me to try?

You're right. Support for env files has come up before. Related: microsoft/vscode-remote-release#3007

Are you implying that if POSTGRES_HOSTNAME was set in host environment then localEnv: would work in forwardPorts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants