devcontainers / devcontainers/spec
Environment and pre-defined variables are not supported in `forwardPorts` and keys of `portsAttributes`
- Dominant language
- No language data
- Stars
- 5.7k
- Forks
- 496
- PR merge metrics
- No merged PRs in 30d
Description
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:
```yaml
services:
postgres:
image: postgres:latest
hostname: ${POSTGRES_HOSTNAME}
...
...
```
And same variables should be used in `forwardPorts` and `portsAttributes` of dev container configuration:
```json
{
...
"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:
```json
{
...
"forwardPorts": [
"postgres:5432",
...
],
"portsAttributes": {
"postgres:5432": {
...
},
...
},
...
}
```
...meaning that hostname needs to be changed manually in two more places if the environment variable changes.
Contributor guide
Assessment
This issue has not been assessed yet.