Pages

21/12/2021

[Docker] Include Dockerfile in another Dockerfile

Docker does not support including a Dockerfile in another Dockerfile, luckily the Dockerfile plus project comes to the rescue.

Simply setup your Dockerfile with:

# syntax = edrevo/dockerfile-plus:0.1.0

INCLUDE+ someDockerfile

FROM someimage

You might need to enable Docker buildkit in order to use this extension, simply set the environment variable:

DOCKER_BUILDKIT=1

[Docker] Redirect app log file to container output

When running applications in containers, we might have log files that are relevant to monitor but are not automatically printed with the container output.

A workaround for this is to force the redirect in our Dockerfile by adding a symbolic link:

RUN ln -sf /dev/stdout /path/to/logfile.log 

Can also be applied to /dev/stderr of course


[Docker] Set Java UTF-8 encoding

Sometimes a Java based app running in a container requires UTF8 encoding. Not all available images enable that by default, this can be fixed by adding one line to your Dockerfile setting the JAVA_TOOL_OPTIONS environment variable:

ENV JAVA_TOOL_OPTIONS -Dfile.encoding=UTF8

This also works outside containers of course.

03/12/2021

[Python] Simple command line options and menu

I recently needed to make a script automatable, meaning I had to provide all inputs to it from the CLI command used to execute it, rather than wait for it to prompt me during execution.

Turns out Python has a nice module dedicated to this: argparse

The usage is very simple and will also create the help menu automatically for us:

 import argparse  
   
 parser = argparse.ArgumentParser("myScript")  
 parser.add_argument("-a_flag_input", help="This is a true/false flag", action='store_true')  
 parser.add_argument("-a_string_input", help="This is a string input", type=str)  
 args = parser.parse_args()  
 options = vars(args)  
 print(args)  
   
 if options["a_flag_input"]:  
  print("a_string_input")  

Basically, we define each input parameter along with its type, if the input is a flag (true/false) we can specify an action on it to determine the value it would receive if set.

The inputs will be collected in a Namespace object, which we can convert to dictionary and get easy access to the inputs from the code.