0

I am using this below command to run my docker containers and want to pass --build-arg LEnv=dev UEnv=Dev in this same command.

docker-compose -f docker-compose.yml up --build --force-recreate

Want to read this arg in docker-Compose file: enter image description here

amit singh
  • 23
  • 6
  • Are you trying to use build args or env vars? – jonrsharpe Jun 11 '20 at 07:53
  • Does this answer your question? [How to define build-args in docker-compose?](https://stackoverflow.com/questions/50734271/how-to-define-build-args-in-docker-compose) – jonrsharpe Jun 11 '20 at 07:54
  • Or maybe [this one](https://stackoverflow.com/questions/49293967/how-to-pass-environment-variable-to-docker-compose-up/50991623#50991623). – Lucas Campos Jun 11 '20 at 07:55
  • @jonrsharpe Trying to use --build-arg as below : docker-compose -f docker-compose.yml up --build --force-recreate --build-arg LEnv=dev --build-arg UEnv=Dev – amit singh Jun 11 '20 at 08:05
  • Well you can see the `docker-compose up` CLI reference [here](https://docs.docker.com/compose/reference/up/), it doesn't take `--build-arg`. But you can pass build args to the containers as shown in the dupe I proposed, and use variable substitution to pass values into the compose file as shown in the file reference [here](https://docs.docker.com/compose/compose-file/#variable-substitution). – jonrsharpe Jun 11 '20 at 08:15
  • You seem to have attached an image to your question instead of the text of your `docker-compose.yml` file; can you replace the image with the actual YAML? Can you clarify what specific problem you're running into (is it just that you can't pass `docker-compose up --build-arg`, or are you having a different problem)? – David Maze Jun 11 '20 at 09:52

1 Answers1

0

You can not pass --build-arg to docker-compose up command. as these are the flag/options of docker-compose build.

but you utilize environment variable

version: "3.8"
services:
  webapp:
    build:
      context: ./
      dockerfile: Dockerfile
      args:
        buildno: ${buildno}

and then run

buildno=2 docker-compose up --build

you can check possible flag/option using below command.

docker-compose build --help
Build or rebuild services.
Usage: build [options] [--build-arg key=val...] [SERVICE...]

Options:
    --build-arg key=val     Set build-time variables for services.
    --compress              Compress the build context using gzip.
    --force-rm              Always remove intermediate containers.
    -m, --memory MEM        Set memory limit for the build container.
    --no-cache              Do not use cache when building the image.
    --no-rm                 Do not remove intermediate containers after a successful build.
    --parallel              Build images in parallel.
    --progress string       Set type of progress output (auto, plain, tty).
                            EXPERIMENTAL flag for native builder.
                            To enable, run with COMPOSE_DOCKER_CLI_BUILD=1)
    --pull                  Always attempt to pull a newer version of the image.
    -q, --quiet             Don't print anything to STDOUT

or for docker-compose up command

docker-compose up --help

Usage: up [options] [--scale SERVICE=NUM...] [SERVICE...]

Options:
    -d, --detach               Detached mode: Run containers in the background,
                               print new container names. Incompatible with
                               --abort-on-container-exit.
    --no-color                 Produce monochrome output.
    --quiet-pull               Pull without printing progress information
    --no-deps                  Don't start linked services.
    --force-recreate           Recreate containers even if their configuration
                               and image haven't changed.
    --always-recreate-deps     Recreate dependent containers.
                               Incompatible with --no-recreate.
    --no-recreate              If containers already exist, don't recreate
                               them. Incompatible with --force-recreate and -V.
    --no-build                 Don't build an image, even if it's missing.
    --no-start                 Don't start the services after creating them.
    --build                    Build images before starting containers.
    --abort-on-container-exit  Stops all containers if any container was
                               stopped. Incompatible with -d.
    --attach-dependencies      Attach to dependent containers
    -t, --timeout TIMEOUT      Use this timeout in seconds for container
                               shutdown when attached or when containers are
                               already running. (default: 10)
    -V, --renew-anon-volumes   Recreate anonymous volumes instead of retrieving
                               data from the previous containers.
    --remove-orphans           Remove containers for services not defined
                               in the Compose file.
    --exit-code-from SERVICE   Return the exit code of the selected service
                               container. Implies --abort-on-container-exit.
    --scale SERVICE=NUM        Scale SERVICE to NUM instances. Overrides the
                               `scale` setting in the Compose file if present.
Adiii
  • 54,482
  • 7
  • 145
  • 148