compose_yml icon indicating copy to clipboard operation
compose_yml copied to clipboard

Valueless keys

Open khuey opened this issue 7 years ago • 6 comments

In environment sections you can specify valueless keys like

environment:
  - FOO
  - BAR

And docker-compose will pick up the relevant values on the machine compose runs on.

The other form,

environment:
  FOO:
  BAR:

would require serde_yaml changes, I believe.

khuey avatar Dec 21 '17 04:12 khuey

Serde_yaml sees the second form as having null values. This matches other YAML libraries I tried.

extern crate serde_yaml;
use serde_yaml::Value;

const Y: &str = "
---
environment:
  FOO:
  BAR:
";

fn main() {
    println!("{:#?}", serde_yaml::from_str::<Value>(Y).unwrap());
}

dtolnay avatar Dec 21 '17 05:12 dtolnay

I don't know if there's a spec for docker-compose.yml beyond the reference, but it's documented at https://docs.docker.com/compose/compose-file/compose-file-v2/#environment with no minor version qualifications.

The reason I didn't add an assert_roundtrip! test is that this already fails even for keys with values because compose_yml canonicalizes the array form to the object form. i.e.

environment:
  - FOO=bar

fails an assert_roundtrip! with

  left: `Object({"environment": Object({"FOO": String("bar")})})`,
 right: `Object({"environment": Array([String("FOO=bar")])})`'

khuey avatar Dec 21 '17 13:12 khuey

What else needs to happen here?

khuey avatar Jan 02 '18 03:01 khuey

I don't see it documented anywhere but it does happen.

khuey@workspace:~/scratch/docker-test$ docker -v
Docker version 1.13.1, build 092cba3
khuey@workspace:~/scratch/docker-test$ docker-compose -v
docker-compose version 1.18.0, build 8dd22a9
khuey@workspace:~/scratch/docker-test$ cat docker-compose.yml
test:
  image: jwilder/whoami
  env_file: test.env
khuey@workspace:~/scratch/docker-test$ cat test.env
TEST_VALUE
khuey@workspace:~/scratch/docker-test$ export TEST_VALUE=Hi
khuey@workspace:~/scratch/docker-test$ docker-compose up -d
Recreating dockertest_test_1 ... done
khuey@workspace:~/scratch/docker-test$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
56dfcff90ec5        jwilder/whoami      "/app/http"         3 seconds ago       Up 2 seconds        8000/tcp            dockertest_test_1
khuey@workspace:~/scratch/docker-test$ docker exec -it 56df sh
/app # echo $TEST_VALUE
Hi

khuey avatar Jan 02 '18 15:01 khuey

(docker-compose behaves this way with an explicit version: "2" in the compose file as well)

khuey avatar Jan 02 '18 15:01 khuey

@khuey OK, if docker-compose supports it, we can certainly implement it.

But please keep in mind my comments on https://github.com/faradayio/cage/issues/87#issuecomment-354767603. We don't have any kind of coherent, principled model for how cage should handle env interpolation, so I'd be careful about relying on these features too heavily if you're using them with cage—at least until we have solid conceptual model about how they should work.

Anyway, this looks about ready to merge. If you don't hear from me by Wednesday, please ping me.

emk avatar Jan 02 '18 16:01 emk