0

Is there a way to set the endpoint of the container instance as an environment variable?

I've looked everywhere, but haven't found any tutorials nor posts about it. I'm guessing it has to launch before the endpoint is created, but can I at least use some command to construct it inside of the image?

3
  • You can set environment variables in Azure Container Instances, but the endpoint of the container instance is not directly available at the time of container creation. However, you can construct the endpoint URL within your application code or script after the container has started. Another way you can try since the endpoint isn’t known until after the container is deployed, is to dynamically retrieve it either through Azure CLI, a startup script, or post-deployment scripts Commented Oct 17, 2024 at 3:19
  • About constructing the URL: if this is the FQDN for example thednsname.hbh3g4ly23g4yh3h.westus.azurecontainer.io, I can pass thednsname and westus.azurecontainer.io as environment variables since they're known, but how do I get or reconstruct the unique identifier hbh3g4ly23g4yh3h? (I should probably also mention that this is for a webhook in Python which needs to send out the endpoint to say where the replies have to be sent) Commented Oct 17, 2024 at 4:14
  • To dynamically construct the full FQDN, including the unique identifier i.e. hbh3g4ly23g4yh3h, you would need to retrieve it after the container instance is created because this part of the FQDN is automatically generated by Azure and is not known upfront Commented Oct 17, 2024 at 4:22

1 Answer 1

0

Setting the endpoint of a Container Instance as an environment variable is possible, but the endpoint is not immediately available at the time of container creation. Below are two workarounds you can use to achieve this

Workaround 1

you can set environment variables when creating the container instance but construct the actual endpoint URL within your application after the container has started.

When you create your container using CLI, PowerShell, or Portal, you can define environment variables that might include placeholders for the endpoint.

az container create \
  --resource-group arkorg \
  --name arkocontainer \
  --image mcr.microsoft.com/oss/nginx/nginx:1.21.6 \
  --environment-variables ENDPOINT_PLACEHOLDER="http://<placeholder>" \
  --dns-name-label arkocontainer \
  --ports 80

enter image description here

or construct the endpoint in your application Logic i.e. after the container starts, your app could fetch the container’s environment variables and then modify the placeholder to the actual URL.

As discussed in comments, to retrieve the full FQDN of the container after it's been deployed. In your case, the unique identifier (hbh3g4ly23g4yh3h) is part of the FQDN and can be extracted using:

az container show --resource-group arkorg --name arkocontainer --query ipAddress.fqdn --output tsv

enter image description here

Then construct the full URL in your Python Webhook

example-

import os
import subprocess

# Step 1: Retrieve the full FQDN using Azure CLI
fqdn = subprocess.check_output(
    ["az", "container", "show", "--resource-group", "arkorg", "--name", "arkocontainer", "--query", "ipAddress.fqdn", "--output", "tsv"],
    universal_newlines=True
).strip()

# Step 2: Construct the URL
if fqdn:
    protocol = "http"  # Or "https" if using SSL
    full_endpoint = f"{protocol}://{fqdn}"

    # Now use this endpoint for your webhook
    print(f"Webhook Endpoint: {full_endpoint}")
    
    # Assuming this is the URL you need to send out
    send_webhook_to(f"{full_endpoint}/webhook-reply")  # Example function
else:
    print("FQDN could not be retrieved.")

Workaround 2

Dynamically set the endpoint as an environment variable post deployment i.e. first, deploy your container as usual, without setting the endpoint environment variable. After deployment, you can retrieve the container’s endpoint using-

az container show --resource-group arkorg --name arkocontainer --query ipAddress.fqdn --output tsv

enter image description here

Now you can use some sort of startup script within your container to query the endpoint and set it as an environment variable.

Example-

# startup.sh
ENDPOINT=$(az container show --resource-group arkorg --name arkocontainer --query ipAddress.fqdn --output tsv)
export CONTAINER_ENDPOINT=$ENDPOINT

# Run your application
exec "$@"

enter image description here

and in your Dockerfile, set this script to run at startup

ENTRYPOINT ["/path/to/startup.sh"]

Personally, I would suggest Workaround 1 as it is more straightforward if your application can handle constructing the endpoint after startup and can work with placeholders.

MS docs-

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

2 Comments

For Workaround 1: Doesn't it require login to az before the command can be done inside of the container? It wouldn't normally be a problem, but when I do it on my computer it brings up a popup to select an email account, and then asks for input about selecting a subscription (even if I just have 1 subscription). It sounds like that would cause the process to pause and wait for user input inside, but I wouldn't see it since the container has no GUI.
I found that Container Apps are the same thing but solve my problem. But thanks for your help, I'll accept your answer.

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.