Movatterモバイル変換


[0]ホーム

URL:


TryMCP servers to extend agent mode in VS Code!

Dismiss this update

Push Django images to a registry

In this tutorial, you take a container image of a Python Django app you built locally and deploy it to Azure Container Registry (ACR) or Docker Hub.

Create a container registry

Create a container registry to push images to. For more information about how to authenticate to and work with registries, seeUsing container registries.

Make sure that the registry endpoint you created is visible underRegistries in theContainer Explorer of VS Code:

Container Explorer in VS Code showing registries

Settings for Django apps

  1. In your Django project'ssettings.py file, modify theALLOWED_HOSTS list to include the root URL to which you intend to deploy the app. For example, the following code assumes deployment to an Azure App Service (azurewebsites.net) named "vsdocs-django-sample-container":

    ALLOWED_HOSTS = [    # Example host name only; customize to your specific host    "vsdocs-django-sample-container.azurewebsites.net"]

    Without this entry, you'll see a "DisallowedHost" message after deployment that instructs you to add the website domain toALLOWED_HOSTS. This will require you to rebuild, push, and redeploy the image once again.

  2. On theCommand Palette (⇧⌘P (Windows, LinuxCtrl+Shift+P)), selectContainers Images: Build Image... to rebuild image with new settings.

    Tip: If you want to test your image in production on multiple hosting services, you can simply input"*" in ALLOWED_HOSTS.

Push the image to a registry

OnceALLOWED_HOSTS have been declared, the next step is to push your Django image to a container registry:

  1. Open theCommand Palette (⇧⌘P (Windows, LinuxCtrl+Shift+P)) and selectContainer Images: Push....

  2. Choose the image you just built to push into the registry.

  3. Choose the registry you created to push into. This will help with correctly tagging the image.

    Select a registry

  4. Once a registry and full tag have been chosen, the image will be pushed. Upload progress will appear in theTerminal window.

  5. Once completed, expand theRegistries >Azure (orDockerHub) node in theContainer Explorer, then expand the registry and image name to see the exact image. (You may need to refresh theContainer Explorer.)

    The built app image in the Azure Container Registry

Tip: The first time you push an image, you will see that VS Code uploads each layer the image is comprised of. Subsequent push operations, however, will only update layers starting from the first that has been changed. Since you app code is usually what changes most often, this is typically why app code is copied in the final lines of a Dockerfile. To see this inner loop in action, make a small change to your code, rebuild the image, and then push again to the registry.

Now that you've pushed your image to a registry, you're ready to deploy it to any container-ready cloud service. For details on deploying to Azure App Service, seeDeploy a container.

1/17/2023

[8]ページ先頭

©2009-2025 Movatter.jp