Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning a command output to ARG variable in a Dockerfile

I want to be able to assign an command's output to an ARG variable while inside a Dockerfile. For example:

ARG myvar=$(echo 'hello')

However, this gives me syntax error

ERROR: Dockerfile parse error line 68: ARG requires exactly one argument

Is there a way to work around this?

like image 452
CodeIntern Avatar asked May 31 '26 08:05

CodeIntern


1 Answers

Although not a direct answer to the question, the way I solved this problem was by creating a bash script that is responsible for starting the Dockerfile build. The script runs the command I need and then passes it as an argument to the Dockerfile. Example:

#!/usr/bin/env bash

MY_VAR=$(echo 'hello')
docker build --build-arg MY_VAR=${MY_VAR} -t myapp .

And then the Dockerfile gets it:

ARG MY_VAR
# MY_VAR now equals 'hello'
like image 115
Lucio Paiva Avatar answered Jun 02 '26 23:06

Lucio Paiva