Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i set a variable in docker compose and use it more than once for more clear code?

Can I set a variable in docker-compose yml file for more clear code ?

For example if there is a value that I use more than once and I want to keep it with one declaration

Like :

version: '2.4'
ValueToPassFromVariable: '2020-10-10 12:00:00'

services:
  img1:
     image: img1
     build:
       context: .
       args:
         - STARTDATE=ValueToPassFromVariable
       dockerfile: DockerFileImg1
       
  img2:
     image: img2
     build:
       context: .
       args:
         - STARTDATE=ValueToPassFromVariable
       dockerfile: DockerFileImg2

I want to save some value in ValueToPassFromVariable and use it few time, can i do something like this ?

like image 781
AnGG Avatar asked Oct 30 '25 22:10

AnGG


1 Answers

If you update to a newer version of docker-compose, you can get generic reuse by using extension fields:

version: "3.8"

x-args: &args
  args:
    - STARTDATE=2020-10-10 12:00:00

services:
  img1:
     image: img1
     build:
       context: .
       << : *args
       dockerfile: DockerFileImg1
       
  img2:
     image: img2
     build:
       context: .
       << : *args
       dockerfile: DockerFileImg2
like image 198
jkinkead Avatar answered Nov 02 '25 11:11

jkinkead



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!