truc
truc

Reputation: 19

Use an argument from docker-compose build --build-arg inside a Dockerfile

I want to be able to build an image while replacing the HOSTNAME in my files automatically at build with the use of sed:

This my Dockerfile:

FROM ...            
RUN sed -i -- 's/0.0.0.0/$HOSTNAME/g' index.html

This is my command:

docker-compose build --build-arg HOSTNAME='justanotherhostname.com'

When I check the resulting index.html, my paths went from this:

path = 'http://0.0.0.0/'

to this:

path = 'http://$HOSTNAME/'

and not this:

path = 'http://justanotherhostname.com/'

What am I doing wrong?

Upvotes: 0

Views: 1420

Answers (1)

Lauri
Lauri

Reputation: 4679

First you need to introduce build argument also in Dockerfile (in case that was missing). Also if you want to replace 0.0.0.0 with the content of $HOSTNAME, you can try to enclose the sed command in double-quotes.

FROM ...
ARG HOSTNAME            
RUN sed -i -- "s/0.0.0.0/$HOSTNAME/g" index.html

Upvotes: 1

Related Questions