Running a Custom Initialization Script in Azure Container Instance upon Start
Sometimes, you might want to execute a script or a command to initialize your application hosted in Azure Container Instance. For such cases, you should bundle the script in the application container image and use the --command-line parameter of the az container create command to specify the command that should be executed to initialize the container.
Let's explore this tip further with an example. We will use the docker/whalesay container image built by Docker for demo tutorials for this demo. Following is the Dockerfile of the image as mentioned in the documentation of the image:
FROM ubuntu:14.04 # install cowsay, and move the "default.cow" out of the way so we can overwrite it with "Docker.cow" RUN apt-get update && apt-get install -y cowsay --no-install-recommends && rm -rf /var/lib/apt/lists/* \ && mv /usr/share/cowsay/cows/default.cow /usr/share/cowsay/cows/orig-default.cow # "cowsay" installs to /usr/games ENV PATH $PATH:/usr/games COPY docker.cow /usr/share/cowsay/cows/ RUN ln -sv /usr/share/cowsay/cows/docker.cow /usr/share/cowsay/cows/default.cow CMD ["cowsay"]
You can override the CMD instruction to pass a string argument to the cowsay command, which the program prints to the logs.
Use the following command to create an Azure Container Instance that overrides the CMD instruction to print a message and start the container with a custom argument. You can replace the echo command with the invocation of your application initialization script.
az container create -g <RESOURCE GROUP NAME> --name whalesay --image registry.hub.docker.com/docker/whalesay --command-line "sh -c 'echo Hello Serverless!!;cowsay moo;'" --restart-policy Never
Navigate to your container instance on the Azure portal and click on the Containers option in the Settings section. Click on the Logs option to view the logs generated by the application. Following is a screenshot of the logs that my application generated: