Jason Berryman
Jason Berryman

Reputation: 4908

Extract variable from text file during Docker build

I'm creating a Dockerfile and part of the process is to ADD the latest version of a package, which can be downloaded as a text file.

Getting the latest-version.txt file

ADD http://example.com/latest-version.txt

Contents of the latest-version.txt file

package1 = AB1.0.0
package2 = 32.4132
package3 = 123.456

The goal

Search for the string package1, set the variable packageVersion to AB1.0.0 and ADD the file.

ADD http://example.com/package1.${packageVersion}.tar.gz

Can I achieve this in the Dockerfile or will I need some help from a Bash script?

Upvotes: 2

Views: 1554

Answers (1)

Bjoern Rennhak
Bjoern Rennhak

Reputation: 6956

Possible Answer

I don't think the Dockerfile syntax will support what you want out of the box currently. I would suggest you create a simple bash script to do the downloading/renaming for you and then just ADD the static file pointers to your Dockerfile.

Example

  • Pull your version file eg. ADD http://example.com/latest-version.txt
  • Add/run some simple bash script/oneliner to process your version file (see below a suggestion to get started)
  • This script reads the latest-version.txt file and downloads the relevant files, eg.
    • wget http://example.com/package1.${packageVersion1}.tar.gz -O /tmp/package1.tar.gz
    • wget http://example.com/package2.${packageVersion2}.tar.gz -O /tmp/package2.tar.gz
    • etc
  • Since all relevant packages have been saved to static file names you can hardcode it in your Dockerfile as eg.
    • ADD /tmp/package1.tar.gz
    • ADD /tmp/package2.tar.gz
    • etc

Closing thoughts

I would suggest you employ some kind of Configuration Management (CM) to make your life easier as this method of solving issues can get messy quickly. CM has the upside of providing not only more structure to solve problems like this, but also two other important attributes namely idempotence and convergence when building an artifact.

Appendix

while IFS='' read -r line || [[ -n "$line" ]]; do
  NAME=`echo $line | cut -d '=' -f1`; VERSION=`echo $line | cut -d '=' -f2`; echo $NAME; echo $VERSION;
done < latest-version.txt

Upvotes: 1

Related Questions