0

For some integration tests that I'm implementing for a project, I need to pass a location of a file to the Dockerfile, so that I can reuse the same Dockerfile, docker-compose and other build artifacts for several tests. I'm executing the docker command from golang.


func StartContainers(composePath string) error {
    cmd := exec.Command("docker-compose", "-f", composePath, "up", "--build", "-d")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    err := cmd.Run()
    if err != nil {
        return err
    }
    return nil
}

In the Dockerfile, I need to access some value passed by golang command to change the value of ${ENVOY} to use it as the source file.

FROM envoyproxy/envoy:v1.18.3

COPY ${ENVOY} /etc/envoy/envoy.yaml
COPY ./artifacts/cache_filter.wasm /usr/local/bin/cache_filter.wasm
COPY ./artifacts/singleton_service.wasm /usr/local/bin/singleton_service.wasm
COPY ./artifacts/threescale_wasm_auth.wasm /usr/local/bin/threescale_wasm_auth.wasm
RUN chmod go+r /etc/envoy/envoy.yaml /usr/local/bin/cache_filter.wasm /usr/local/bin/singleton_service.wasm
CMD /usr/local/bin/envoy -c /etc/envoy/envoy.yaml -l trace

My docker-compose looks like below.

version: '3.7'
services:
  proxy:
    build:
      context: .
      dockerfile: Dockerfile
    depends_on:
      - backend_service
    networks:
      - envoymesh
    ports:
      - "9095:9095"
      - "9000:9000"
  
  backend_service:
    image: solsson/http-echo
    networks:
      - envoymesh
    environment:
      - PORT=8000 

networks:
  envoymesh: {}

How to pass values from golang command? I referred to several answers on Stackoverflow, but none of them worked for me.

How to pass environment variable to docker-compose up

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Lahiru Udayanga
  • 309
  • 4
  • 13
  • 1
    What you are looking for is most probably a combination of `ARG` and `ENV` variables. See [this article over at [vsupalov](https://vsupalov.com/docker-build-pass-environment-variables/). We can set arguments via the [`arg`-directive in docker-compose files](https://docs.docker.com/compose/compose-file/compose-file-v3/#args). – Turing85 Jun 27 '21 at 19:40
  • The issue here is I can't add a build arg to docker-compose. Need to pass it dynamically from the golang code. Can you show a simple example how to pass ? – Lahiru Udayanga Jun 27 '21 at 19:55
  • @LahiruUdayanga Have a look [here](https://docs.docker.com/compose/reference/build/) at `--build-arg` – super Jun 27 '21 at 20:01
  • I tried it. Up doesn't have that argument. So have to build and then run using up. 2 steps – Lahiru Udayanga Jun 27 '21 at 20:02
  • And that's a problem because... ? – super Jun 27 '21 at 20:07
  • Not a problem actually. Just wanted to have minimal code – Lahiru Udayanga Jun 27 '21 at 20:11
  • Build args are for things like `COPY`, environment variables are for things like `up` and `run`. You cannot mix. You need to decide if this is a build time or run time variable. If it is a built time (as you have used it with `COPY`), you will have to build a different image for each test (using the same Dockerfile of course). – DannyB Jun 28 '21 at 11:11

0 Answers0