Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
636 views
in Technique[技术] by (71.8m points)

docker - how to pass environment variable to dockerfile

how to pass kaps-service as a environment variable to my dockerfile

file.txt:- Match $Match name prod

dockerfile:- FROM image ENV Match=kaps-service COPY file.txt /etc/

the requirement is I want to replace the $Match value in file.txt from dockerfile or can I pass this value while starting the docker container ? is there a way to do it ?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From Dockerfile you can use something like:

FROM image

ARG Match="kaps-service"

COPY file.txt /etc/

RUN sed -i 's/$Match/'$Match'/g' /etc/file.txt

The last line will replace all occurrences of $Match with "kaps-service". This will happen during build step, and you can modify Match argument with the --build-arg flag

In case you want the replacement to be in run step (docker run) you can take the RUN command and put it in some script that will execute this command before your application. It can be something like:

FROM image

WORKDIR /app

ENV Match="kaps-service"

COPY file.txt /etc/
COPY run .

CMD "./run"

where run is:

#!/bin/bash

sed -i 's/$Match/'$Match'/g' /etc/file.txt
# run you app

The change will happen when you starts a container with docker run and you can modify Match variable add -e Match=something to the command.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...