I’m new to Docker and trying to build a reusable, parameterized container. So, I was wondering what’s the best way to do this? I know this question is a little bit philosophical but please bear with me.
The container I’m trying to build is a PXE server that needs to take two arguments (network ip address and network interface name). For now, I’m aiming for a single container - so I’m not looking for a solution that uses a second container as configuration provider (unless the solution is very simple).
Before continuing, let me define what I mean by “reusable files” below:
A file (
Dockerfile
ordocker-compose.yml
) is reusable if you can parameterize the resulting container(s) without changing the file’s contents.
While reading on the internet and thinking about the problem I came up with the following possibilities:
- Hardcode the parameters in the
Dockerfile
. Easiest solution but the container/Dockerfile is not reusable (if the parameters differ in a different environment). - Use environment variables. This seems to be quite common.
- Use command line parameter passing.
For command line parameter passing I would need to do some conversion of the parameters before passing them to the containerized application. I couldn’t find a way of doing this with a Dockerfile
. So I would need to write and use a wrapper bash script as entrypoint to do the conversion (inside of the container). However, this seems “complicated” to me and doesn’t sound like the Docker way to do things. (Of course, I may be wrong on this.)
Also, the call to docker run
for the container is quite… involved (i.e. it takes a lot of parameters for ports and networks). So I created a docker compose file. With this, the question shifts to:
How to parameterize a docker compose file?
The only solution I found was to hardcode the parameters in the docker compose file. I couldn’t find anything about passing parameters to docker-compose up
.
Base on my findings, my current conclusion is:
Dockerfiles should be reusable. To parameterize a container create/copy a docker compose file. (Thus docker compose files can’t be reusable in this case.)
Would you agree? Or is there something I’m missing?