0

I have a Python script that uses an Azure Service Bus underneath. It is a simulation, so it communicates through the Service Bus using the Pub Sub and Topics and prints information about some events.

I made a Dockerfile that installs all the requirements, sets the ENV variables from Azure and runs the file that starts the simulation.

When I run the Docker Image locally using docker run <image>, it does nothing until it's done running and then it spits all the logs out.

When I run the Docker image locally using docker run -it <image>, I can see all the logs coming one by one.

I pushed the Image to Azure Container Registry and created a Docker Container Instance with that image. When I run the container and use az container logs <rg> <name> or az container attach <rg> <name>, it does not show logs like it does with the docker run -it command, but spits them out when the simulation is all done.

Is it possible to see the logs in realtime?

What I tried is to run the container instance by pressing 'play' in Azure and then to see the logs in the 'logs' tab. But when the container is running, it says: 'no logs avaiable'. Then I tried the Azure Cloud Shell commands I mentioned above, but it still does not show the Logs.

1 Answer 1

0

Check that you're using a logging library like logging and configure it to output logs to STDOUT or STDERR. You can do this by adding the following lines at the beginning of your script or wherever you configure logging:

    import sys
    import logging
    
    # Configure logging to output to STDOUT
    logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)

Make sure that any logs generated by script are written using the logging module.

Update the Dockerfile to include instructions to copy the Python script and any other necessary files, install dependencies, and set environment variables if needed. Dockerfile:

    FROM python:3.9
    
    # Set working directory
    WORKDIR /app
    
    # Copy requirements file
    COPY requirements.txt .
    
    # Install dependencies
    RUN pip install --no-cache-dir -r requirements.txt
    
    # Copy the Python script
    COPY your_script.py .
    
    # Set environment variables if needed
    ENV VARIABLE_NAME=value
    
    # Command to run the Python script
    CMD ["python", "your_script.py"]

Build the Docker image in the project directory. docker build -t your_image_name .

  • Push the Docker image to the Azure Container Registry using the following command:

docker push your_registry_name.azurecr.io/your_image_name:tag

  • Create an Azure Container Instance using the Docker image you pushed to ACR. You can use the Azure CLI or the Azure portal to create the ACI.
az container create \
      --resource-group your_resource_group \
      --name your_container_name \
      --image your_registry_name.azurecr.io/your_image_name:tag \
      --cpu 1 \
      --memory 1.5 \
      --restart-policy OnFailure \
      --dns-name-label your_dns_name_label \
      --ports 80
  • Once the container is running, you can view the logs in real-time using the following Azure CLI command:
az container logs --resource-group your_resource_group --name your_container_name

Logs: enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.