Home > database >  Avoiding duplicated arguments when running a Docker container
Avoiding duplicated arguments when running a Docker container

Time:03-28

I have a tensorflow training script which I want to run using a Docker container (based on the official TF GPU image). Although everything works just fine, running the container with the script is horribly verbose and ugly. The main problem is that my training script allows the user to specify various directories used during training, for input data, logging, generating output, etc. I don't want to have change what my users are used to, so the container needs to be informed of the location of these user-defined directories, so it can mount them. So I end up with something like this:

docker run \
  -it --rm --gpus all -d \
  --mount type=bind,source=/home/guest/datasets/my-dataset,target=/datasets/my-dataset \
  --mount type=bind,source=/home/guest/my-scripts/config.json,target=/config.json \
  -v /home/guest/my-scripts/logdir:/logdir \
  -v /home/guest/my-scripts/generated:/generated \
  train-image \
  python train.py \
  --data_dir /datasets/my-dataset \
  --gpu 0 \
  --logdir ./logdir \
  --output ./generated \
  --config_file ./config.json \
  --num_epochs 250 \
  --batch_size 128 \
  --checkpoint_every 5 \
  --generate True \
  --resume False

In the above I am mounting a dataset from the host into the container, and also mounting a single config file config.json (which configures the TF model). I specify a logging directory logdir and an output directory generated as volumes. Each of these resources are also passed as parameters to the train.py script.

This is all very ugly, but I can't see another way of doing it. Of course I could put all this in a shell script, and provide command line arguments which set these duplicated values from the outside. But this doesn't seem a nice solution, because if I want to anything else with the container, for example check the logs, I would use the raw docker command.

I suspect this question will likely be tagged as opinion-based, but I've not found a good solution for this that I can recommend to my users.

CodePudding user response:

As user Ron van der Heijden points out, one solution is to use docker-compose in combination with environment variables defined in an .env file. Nice answer.

  • Related