Setting up Promtail in Application Container

I’m staring to deploy more applications to Azure cloud services (App Service, Container Apps, Container Instances, etc). With these services, you don’t have access to the infrastructure to install a promtail agent or setup a promtail container.

My idea was to setup promtail inside my application container and start the process in the background before I start the application. And then configure promtail to maybe collect logs from stdout and stderr? Does anyone know if this is possible? Or maybe a better way to ship logs from inside my application containers?

Interested in shipping logs to Loki from Azure Container Apps as well, wondering what possible solutions/best practices would be. Are there any solutions? Thanks!

It all depends, to be honest. Is your application actually logging to stdout/stderr?

In any case, setting up promtail inside your container should be a reasonable approach. You’ll likely want to configure it to slurp up the journal (if any), any log files in /var/log, as well as any additional logs your application might scatter across the file system in addition to stdout/stderr.

My application is a Python application and is logging to stdout and stderr (with print statements), but I’m not really sure how to configure promtail to collect those logs. They do show up when you run docker logs, and they live in the /var/lib/docker//-json.log file, but obviously they are outside the container and not accessible via promtail inside the container.

My only idea would involve updating all my applications to 1. change any print statements to log to a file also, and 2. catch every exception and log that to a file and print it. Which I was hoping I wouldn’t need to do. I was hoping there would be a way to either configure promtail to collect the same logs that docker collects (i’m guessing stderr and stdout), or maybe redirect stderr and stdout to a file inside the container and point promtail to that file? But so far my attempts have been unsuccessful.

I have been attempting to get promtail to work in a container app. I have two containers running, one with promtail and one with my dotnet application.

In order to allow promtail to be able to read the logs from the other container, my train of thought was to create a shared Ephemeral storage. You can do this in the Azure portal, just ensure that you give both containers the same ‘Volume name’ and ‘Mount path’. Then you can update your app to write the stdout logs to that location and promtail to read from it.

Not sure if this is the correct approach as there seems to be no docs on it, but thought it may help.

My issue is that I have no idea where my app is currently putting those logs at the moment (i know it is logging as I can see the logs in the log stream). When I try to put path for the stdout I don’t see it get created where i’m expecting.

While I think this will work, there is a much easier way. In your promtail container; add the following volume mounts:

      - /var/lib/docker/containers:/var/lib/docker/containers:ro
      - /var/run/docker.sock:/var/run/docker.sock

Then make your promtail container depend on the container you want to get the logs from. Now use the docker_sd_configs section in your promtail config to point at your docker socket, et voilá - you’re done!