This article is contributed. See the original author and article here.

In some scenarios, you might need to preprocess files before they’re used by your application. For instance, you’re deploying a machine learning model that relies on precomputed data files. An Init Container can download, extract, or preprocess these files, ensuring they are ready for the main application container. This approach simplifies the deployment process and ensures that your application always has access to the required data.


 


The below example defines a simple Pod that has an init container which downloads a file from some resource to a file share which is shared between the init and main app container. The main app container is running a php-apache image and serves the landing page using the index.php file downloaded into the shared file space.


 


Initcontainerimage-usecase2.jpg


 


The init container mounts the shared volume at /mydir , and the main application container mounts the shared volume at /var/www/html. The init container runs the following command to download the file and then terminates: wget -O /mydir/index.php http://info.cern.ch


 


Configurations and dockerfile for init container:


 



Astha_3-1695053540906.png


 


 



  • Dockerfile for init which downloads an index.php file under /mydir:


 

FROM busybox:1.28 
WORKDIR / 
ENTRYPOINT ["wget", "-O", "/mydir/index.php", "http://info.cern.ch"]

 


 


 


Configuration for main app container:


 



  • Create main app container mounting file share named init on path /var/www/html:


Astha_5-1695053835179.png


 


 



  • Main app container configuration which uses php-apache image and serves the index.php file from DocumentRoot /var/www/html:


Astha_4-1695053818985.png


 


Output:


 


Astha_2-1695053488081.png


 


 


 


Logs:


 


Astha_1-1695052932354.png


 

Brought to you by Dr. Ware, Microsoft Office 365 Silver Partner, Charleston SC.