Boris L
Boris L

Reputation: 191

Azure pipeline: Container port mapping not working inside deployment job

I had a pipeline that started a container server, then tested it using curl:

pool:
  vmImage: ubuntu-latest
steps:
- checkout: self

- task: Docker@2
  displayName: Login to docker registry
  inputs:
    containerRegistry: 'azure-registry-1'
    command: 'login'

- bash: |
    docker run -d --name test-server \
      -p 127.0.0.1:9000:9000/tcp \
      my.azurecr.io/test-server-ubuntu:latest
    
    sleep 5
    curl http://localhost:9000/

Everything worked just fine.

After that I tried to use my custom image base on Ubuntu (with additional packages installed). Since I cannot use

vmImage: my.azurecr.io/ubuntu-with-scripting    # not working

I had to rewrite my pipeline like this:

pool:
  vmImage: ubuntu-latest

resources:
  containers:
  - container: builder
    endpoint: azure-registry-1
    image: my.azurecr.io/ubuntu-with-scripting

jobs:
  - deployment: Release
    environment: debug
    container: builder
    strategy:
      runOnce:
        deploy:
          steps:
          - checkout: self
          - task: Docker@2
            displayName: Login to docker registry
            inputs:
              containerRegistry: 'azure-registry-1'
              command: 'login'

          - bash: |
              docker run -d --name test-server \
                -p 127.0.0.1:9000:9000/tcp \
                my.azurecr.io/test-server-ubuntu:latest
              
              sleep 5
              curl http://localhost:9000/

The problem is that port mapping seems to stop working:

curl: (7) Failed to connect to localhost port 9000: Connection refused

While this works fine:

docker exec test-server curl http://localhost:9000/

I would appreciate any suggestions.

Upvotes: 0

Views: 261

Answers (1)

Alvin Zhao - MSFT
Alvin Zhao - MSFT

Reputation: 6037

According to this document,

When you specify a container in your pipeline, the agent will first fetch and start the container. Then, each step of the job will run inside the container. You can't have nested containers. Containers aren't supported when an agent is already running inside a container.

If you need fine-grained control at the individual step level, step targets allow you to choose container or host for each step.

Per you first YAML pipeline, since you haven't defined or use any container environment, the pipeline agent job landed on the ubuntu-latest agent environment and then all the tasks including docker commands were executed in the agent environment to login ACR and startup a container based on your ACR image; the curl command worked to connect the container app hosted on the agent machine at port 9000.

However, you second pipeline was configured to run the deployment job Release in a container environment container: builder, so as soon as an ubuntu-latest agent was assigned for this pipeline, a Builder container was started based on your image my.azurecr.io/ubuntu-with-scripting and then inside the Builder container, your pipeline created the test-server container.

Upvotes: 0

Related Questions