title: Source OS environment variables from a docker-compose.yaml
body: <p>I want to call a script in the same way it is called within a container. Therefore I have to set a couple of environment variables. Is there a way to export all listed environment variables of a docker-compose.yaml, so that they are available as variables in my shell for subsequent commands?</p>
  <p>Let's assume I use this <code>docker-compose.yaml</code>:</p>
  <pre><code>version: &quot;3.8&quot;
  
  services:
    my-service:
      image: some-image
      build:
        context: .
        dockerfile: Dockerfile
      environment:
        var1: test
        var2: 1
        # var3: test # Some comment
      command: echo $var1 $var2 $var3
  </code></pre>
  <p>Using <a href="https://github.com/mikefarah/yq" rel="nofollow noreferrer">yq</a> it is easy to parse the relevant variables, but I am unsure how to export each of those. Also special cases like comments might be an issue.</p>
  <p>What I got so far: <code>cat ~/docker-compose.yaml| yq .services.my-service.environment</code></p>
  <p>Which will return:</p>
  <pre><code>var1: test
  var2: 1
  # var3: test # Some comment
  </code></pre>