Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add dynamic file to docker container

Tags:

docker

whenever I run a docker container, I want to send dynamic filename as some environment variable.

That is accessible in container so its printing its value when we 'echo'.

But ADD command not adding that file.

Dockerfile:

ADD $filename ./
echo ls # Not showing file

docker run -e filename='/path/to/file.extension'

like image 495
user2349115 Avatar asked Oct 02 '16 21:10

user2349115


2 Answers

Try using a volume instead:

$ echo "hello world" > somefile.txt
$ docker run -it --rm -v $PWD/somefile.txt:/data/somefile.txt alpine cat /data/somefile.txt
hello world

The Dockerfile lists the actions that occur when you run a "docker build". It's not possible to pass in an environment variable at run-time, because, at that point, the image is already built :-)

like image 178
Mark O'Connor Avatar answered Sep 28 '22 09:09

Mark O'Connor


ADD is run during compile (build) time. When you run docker exec -e that is after the container has been built.

You cannot add dynamic files because it's compiled. The previous command about volumes is correct because you can provide those files ad-hoc during exec and have your application pick them up.

like image 32
Marc Young Avatar answered Sep 28 '22 08:09

Marc Young