Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run sed in dockerfile to replace text with build arg value

Tags:

dockerfile

sed

I'm trying to use sed to replace the text "localhost" in my nginx.conf with the IP address of my docker host (FYI. using docker machine locally, which is running docker on 192.168.99.100).

My nginx Dockerfile looks like this:

FROM nginx:alpine
ARG DOCKER_HOST
COPY ./nginx.conf /etc/nginx/nginx.conf
RUN sed -i 's/localhost/${DOCKER_HOST}/g' /etc/nginx/nginx.conf
EXPOSE 80

My nginx.conf file looks like this (note: majority removed for simplicity)

http {

   sendfile on;

   upstream epd {
      server localhost:8080;
   }

   # ...
}

I'm expecting "localhost" to get replaced with "192.168.99.100", but it actually gets replaced with "${DOCKER_HOST}". This causes an error

host not found in upstream "${DOCKER_HOST}:8080"

I've tried a few other things, but I can't seem to get this working. I can confirm the DOCKER_HOST build arg is getting through to the Dockerfile via my docker compose script, as I can echo this out.

Many thanks for any responses...

like image 521
Ryan.Bartsch Avatar asked Feb 04 '23 20:02

Ryan.Bartsch


1 Answers

Replace the single quotes ' around s/localhost/${DOCKER_HOST}/g with double quotes ". Variables will not be interpolated within single quotes.

like image 196
Marcus Avatar answered Feb 20 '23 13:02

Marcus