简体   繁体   English

让 postgres docker 容器在 azure 管道作业中运行

[英]Keep postgres docker container in azure pipeline job running

I'm rather new to Azure and currently playing around with the pipelines.我对 Azure 比较陌生,目前正在使用管道。 My goal is to run a postgres alpine docker container in the background, so I can perform tests through my python backend.我的目标是在后台运行一个 postgres alpine docker 容器,这样我就可以通过我的 python 后端执行测试。

This is my pipeline config这是我的管道配置

trigger:
  - main
pool: 
  vmImage: ubuntu-latest
variables:
  POSTGRE_CONNECTION_STRING: postgresql+psycopg2://postgres:passw0rd@localhost/postgres
resources:
  containers:
    - container: postgres
      image: postgres:13.6-alpine
      trigger: true
      env:
        POSTGRES_PASSWORD: passw0rd
      ports:
        - 1433:1433
      options: --name postgres
stages:
  - stage: QA
    jobs:
      - job: test
        services:
          postgres: postgres
        steps:
          - task: UsePythonVersion@0
            inputs:
              versionSpec: $(PYTHON_VERSION)
          - task: Cache@2
            inputs:
              key: '"$(PYTHON_VERSION)" | "$(Agent.OS)" | requirements.txt'
              path: $(PYTHON_VENV)
              cacheHitVar: 'PYTHON_CACHE_RESTORED'
          - task: CmdLine@2
            displayName: Wait for db to start
            inputs:
              script: |
                sleep 5
          - script: |
              python -m venv .venv
            displayName: create virtual environment
            condition: eq(variables.PYTHON_CACHE_RESTORED, 'false')
          - script: |
              source .venv/bin/activate
              python -m pip install --upgrade pip
              pip install -r requirements.txt
            displayName: pip install
            condition: eq(variables.PYTHON_CACHE_RESTORED, 'false')
          - script: |
              source .venv/bin/activate
              python -m pytest --junitxml=test-results.xml --cov=app --cov-report=xml tests
            displayName: run pytest
          - task: PublishTestResults@2
            condition: succeededOrFailed()
            inputs:
              testResultsFormat: 'JUnit'
              testResultsFiles: 'test-results.xml'
              testRunTitle: 'Publish FastAPI test results'
          - task: PublishCodeCoverageResults@1
            inputs:
              codeCoverageTool: 'Cobertura'
              summaryFileLocation: 'coverage.xml'  

But the pipeline always fails at the step "Initialize Containers", giving this error: Error response from daemon: Container <containerID> is not running as if it was just shutting down because there is nothing to do.但管道总是在“初始化容器”步骤失败,出现以下错误: Error response from daemon: Container <containerID> is not running ,就好像它刚刚关闭一样,因为无事可做。 Which seems right, but I don't know how to keep it running until my tests are done, the backend just runs pytest against the database.这似乎是对的,但在我的测试完成之前我不知道如何让它运行,后端只是针对数据库运行 pytest。 I also tried adding that resource as container using the container property, but then the pipeline crashes at the same step, saying that the container was just running less than a second.我还尝试使用container属性将该资源添加为容器,但随后管道在同一步骤中崩溃,说容器仅运行不到一秒。

I'm thankful for any ideas!我很感谢任何想法!

I'm suspicious that your container is not stopping because of "there is nothing to do", the postgres image is configured in a way to act as a service.我怀疑您的容器不会因为“无事可做”而停止,postgres 图像被配置为充当服务。 Your container is probably stopping because of an error.您的容器可能由于错误而停止。

I'm sure there is something to improve: you have to add the PGPORT env var to your container and set to 1433 because that port is not the default port for the postgres docker image, so opening that port on your container like you are doing with ports is not doing too much in this case.我确信有一些需要改进的地方:您必须将PGPORT env var 添加到您的容器并设置为1433 ,因为该端口不是 postgres docker 映像的默认端口,因此像您一样在容器上打开该端口正在做在这种情况下,使用ports并没有做太多。

Also, your trigger: true property would mean that you are expecting updates on the official DockerHub repository for postgres and in case of a new image release, run your pipeline.此外,您的trigger: true属性意味着您希望在官方 DockerHub 存储库中更新 postgres,如果有新的图像发布,请运行您的管道。 I think that does not makes too much sense, you should remove it, just in case, although this is marginal problem from the perspective of your question.我认为这没有太大意义,您应该删除它,以防万一,尽管从您的问题的角度来看这是边缘问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM