如何使用 Azure Pipeline 通过 SSH 在远程计算机上部署容器How to deploy a container on remote machine over ssh using Azure Pipeline

How to deploy a container on remote machine over ssh using azure pipeline

提问人:Manish 提问时间:10/5/2023 更新时间:10/5/2023 访问量:58

问:

我正在尝试在代理上将我的应用程序构建为容器映像,然后在部署步骤中,我正在对目标计算机执行 ssh 并运行 docker pull/run 命令以启动容器。它也成功启动,但一段时间后容器正在退出。

我的疑问是它仅在 ssh 会话中运行,一旦会话结束,容器就会关闭。

trigger:
  branches:
    include:
      - master
      - refs/tags/*
      - feature*

pool: Agent_pool

variables:
  - group: azure-creds
  - name: APP_VERSION
    value: $(Build.SourceBranchName)-$(Build.BuildNumber)
  - name: APPLICATION_NAME
    value: myapp
  - name: JAVA_HOME
    value: /usr/lib/jvm/java-11-openjdk-amd64
  - name: DOCKER_CONTAINER_REGISTRY
    value: docker.rtp.orgcloud.com

stages:
  - stage: buildAndTest
    displayName: Building and testing
    jobs:
      - job: buildAndTestJob
        displayName: Building and testing job
        steps:
          - script: |
              export DOCKER_IMAGE="$(DOCKER_CONTAINER_REGISTRY)/$(APPLICATION_NAME):$(APP_VERSION)"
              docker build --build-arg VERSION=$(APP_VERSION) . -t "$DOCKER_IMAGE"
            displayName: Containerize application
          - script: |
              export DOCKER_IMAGE="$(DOCKER_CONTAINER_REGISTRY)/$(APPLICATION_NAME):$(APP_VERSION)"
              docker login -u $(azure_username) -p $(azure_password) $(DOCKER_CONTAINER_REGISTRY)
              docker push "$DOCKER_IMAGE"
            displayName: Push Container Image to Azure Container Registry
  
  - stage: Deploy
    condition: and(succeeded(), or(eq(variables['Build.SourceBranch'], 'refs/heads/master'), startsWith(variables['Build.SourceBranch'], 'refs/tags'), startsWith(variables['Build.SourceBranch'], 'refs/heads/feature')))
    jobs:
      - job: 'RunDeploymentCommandJob'
        displayName: Deploying job
        steps:
          - task: SSH@0
            env:
              ENVIRONMENT: 'ppe'
            inputs:
              sshEndpoint: 'fuelVM'  # The name of the SSH service connection you created
              runOptions: 'inline'
              inline: |
                echo "started executing commands"
                echo "Before Docker image is set to:$DOCKER_IMAGE"
                export DOCKER_IMAGE="$(DOCKER_CONTAINER_REGISTRY)/$(APPLICATION_NAME):$(APP_VERSION)"
                echo "After Docker image is set to:$DOCKER_IMAGE"
                docker login -u $(azure_username) -p $(azure_password) $(DOCKER_CONTAINER_REGISTRY)
                echo "login succeeded"
                echo "Docker image is set to:$DOCKER_IMAGE"
                docker pull $DOCKER_IMAGE
                echo "docker pull completed"
                PREVIOUS_CONTAINER=$(docker ps -aq --filter "name=$APPLICATION_NAME")

                if [ ! -z "$PREVIOUS_CONTAINER" ]; then
                  docker stop "$PREVIOUS_CONTAINER" || true
                  docker rm "$PREVIOUS_CONTAINER" || true
                  echo "stopped previous container"
                fi
                echo "image downloaded, now running containers"
                docker run -d --name $(APPLICATION_NAME) -p 5000:5000 "$DOCKER_IMAGE"
            displayName: 'Deploy to Server'

容器日志如下:

[2023-10-05 07:13:41 +0000] [1] [INFO] Starting gunicorn 21.2.0
[2023-10-05 07:13:41 +0000] [1] [INFO] Listening at: http://0.0.0.0:5000 (1)
[2023-10-05 07:13:41 +0000] [1] [INFO] Using worker: sync
[2023-10-05 07:13:41 +0000] [5] [INFO] Booting worker with pid: 5
[2023-10-05 08:00:46 +0000] [5] [INFO] Worker exiting (pid: 5)
[2023-10-05 08:00:46 +0000] [1] [INFO] Handling signal: term
[2023-10-05 08:00:46 +0000] [1] [ERROR] Worker (pid:5) was sent SIGTERM!
[2023-10-05 08:00:46 +0000] [1] [INFO] Shutting down: Master

如果我错过了任何东西或更好的实现方法,有什么建议吗?

谢谢

docker ssh azure-pipelines 退出

评论

0赞 Daniel Mann 10/6/2023
实现此目的的更好方法是使用容器应用等服务来运行容器,而不是维护整个虚拟机。
0赞 Ceeno Qi-MSFT 10/6/2023
您的管道代理是什么类型?Microsoft 托管代理、VMSS 还是自托管代理?
0赞 Manish 10/6/2023
@CeenoQi-MSFT 自托管代理
0赞 Manish 10/9/2023
@DanielMann目标 VM 不在 Azure 中,而是在我组织的私有云中。没有容器应用。

答: 暂无答案