Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse file in Dockerfile

Is it possible to parse a file present in the build context to gather information from it? For example it would be nice to do smth like this:

EXPOSE `cat config.conf | <more commands to get the port to expose>`

The config file belongs to the application being containerized, and there are some ports that have to be configurable, and this also implies that it has to be somehow accessible from the Dockerfile.

Any ways to accomplish this?

like image 945
kupsef Avatar asked Nov 20 '25 18:11

kupsef


1 Answers

If you really need to produce Dockerfile based on template, I think there is plenty of templating tools, depending on your host platform. It does not make sense to task Docker with it. A simplest one would be

cfg_port = cat config.conf | grep ... | ...
sed -e "s/CFG_PORTNUMBER/$cfg_port/g" /path/to/Docker_templatefile > /path/to/Dockerfile

That said,

EXPOSE in Dockerfile is just a hint. It instructs docker to setup environment variables during container linking in destination container - see UserGuide: "Environment Variables" section.

So, make sure EXPOSE based on some template and resulting separate image for each port configuration is what you really need. EXPOSE really is just a hint.

Much easier and powerful way to do things like this, in most cases, is to specify ports during container creation:

docker run --expose=$cfg_port -ti myimage bash

In most cases, I would recommend not to create Dockerfile at all. Instead read and try different run options with docker official images until you are absolutely sure why you need your own image. You only want separate image per software stack that absolutely needs to run in the same container.

And, of course, check out if docker-compose already does what you need.

like image 70
Alex Pakka Avatar answered Nov 23 '25 07:11

Alex Pakka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!