Azure Devops install Python package from Azure Artifacts inside Docker

How can I install a Python package published in Azure Artifacts as part of my Azure Pipeline Docker task automatically?

We could use the PipAuthenticate task to populates the PIP_EXTRA_INDEX_URL environment variable:

It authenticates with your artifacts feed and per the docs, will store the location of a config file that can be used to connect in the PYPIRC_PATH environment variable.

Then pass it in the build arg:

arguments: --build-arg INDEX_URL=$(PIP_EXTRA_INDEX_URL)

You could check this document Consuming Azure Pipelines Python artifact feeds in Docker for some more details.

Hope this helps.


To add to the accepted answer, here is a somewhat more complete code example:

azure-pipelines.yml

- task: PipAuthenticate@1
  inputs:
    artifactFeeds: 'my_artifacts_feed'
    # 'onlyAddExtraIndex' populates PIP_EXTRA_INDEX_URL env variable
    onlyAddExtraIndex: True

- task: Docker@2
  displayName: 'Build Docker Image'
  inputs:
    command: build
    dockerfile: $(dockerfilePath)
    tags: |
      $(tag)
    arguments: --build-arg INDEX_URL=$(PIP_EXTRA_INDEX_URL)

Dockerfile

FROM python:3.8-buster

# add an URL that PIP automatically searches (e.g., Azure Artifact Store URL)
ARG INDEX_URL
ENV PIP_EXTRA_INDEX_URL=$INDEX_URL

COPY requirements.txt
RUN pip install -r requirements.txt