Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass --build-arg parameters from command line to docker-compose.yml file?

Suppose that I type the following command

docker-compose -f docker-compose.yml build --build_args PARAM=1

How should I write my docker-compose.yml such that the yml fiel gets the argument PARAM, whose value is 1?

My yml file is below.

version: '3.7'

services:
  web:
    build:
      context: .
      dockerfile: ./Dockerfile
      args: 
         - MY_PARAM: ${PARAM}
      ports:
         - "5000:5000"
    image: sample-img:1.0

I added

args: 
   - MY_PARAM: ${PARAM}

below build. Is this correct?

like image 685
user2585578 Avatar asked Oct 27 '25 07:10

user2585578


1 Answers

Here is how you can pass build arguments while using compose -

  1. Allow arguments support in Dockerfile.

    FROM foo:bar   
    ARG PARAM
    ARG PARAM2
    .................
    
  2. Now you can pass these arguments in two different ways -

    1. During runtime -

      docker-compose -f docker-compose.yml build --build-arg "PARAM=1 PARAM2=2"
      
    2. Within compose using shell -

      $ export PARAM=1 PARAM2=2
      

      Change docker compose as below -

      dockerfile: ./Dockerfile.foo
      args:
        PARAM: ${PARAM}
        PARAM2: ${PARAM2}
      

      Build it -

      $ docker-compose -f docker-compose.yml build
      
like image 58
vivekyad4v Avatar answered Oct 29 '25 22:10

vivekyad4v



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!