一千萬個為什麽

搜索

如何在bitbucket管道中綁定docker容器的端口?



我想使用bitbucket管道和運行某些服務的自定義docker鏡像來測試應用程序。我有一個dev docker鏡像,它運行我需要的所有服務,以便測試通過。

docker run -it -p 3000:3000 -p 6379:6379 -p 8983:8983 my_dockerhub/image
./start_services.sh
bundle exec rspec
# everything passes

我似乎無法找到一種方法來啟動容器與正確的端口綁定,以便測試通過。

  1. I tried using the container directly using this in my bitbucket-pipelines.yml:

    image: 
      name: my_dockerhub/image
      username: $DOCKER_HUB_USERNAME
      password: $DOCKER_HUB_PASSWORD
      email: $DOCKER_HUB_EMAIL
      # bind ports here somehow?
    
    pipelines:
      branches:
        '{master, develop, bitbucket_pipelines}':
          - step:
              name: Test
              script:
                - ./start_services.sh
                - sleep 30
                - bundle exec rspec
    

    But I get the following error at the bundle exec rspec step

    # Errno::ECONNREFUSED:
    #   Connection refused - connect(2) for "127.0.0.1" port 8983
    
  2. I tried running the docker container directly within the bitbucket pipeline

    pipelines:
      branches:
        '{master, develop, bitbucket_pipelines}':
          - step:
              name: Start Docker
              script:
                - docker login -u $DOCKER_HUB_USERNAME -p $DOCKER_HUB_PASSWORD
                - docker run -t -p 3000:3000 -p 6379:6379 -p 8983:8983  my_dockerhub/image
                - ./script/start_services.sh
                - sleep 20
                - bundle exec rspec
    

    But the step froze at the docker run step. I assume this is because I used the -t flag so the command doesn't exit. I was thinking I could possibly use docker exec to send commands to the container without running it but then how would I check the tests have passed?

轉載註明原文: 如何在bitbucket管道中綁定docker容器的端口?

一共有 1 個回答:

嘗試使用 -d detached 標誌運行

docker run -td -p 3000:3000 -p 6379:6379 -p 8983:8983 my_dockerhub/image # here ^ Containers started in detached mode exit when the root process used to run the container exits. This will prevent the step to freeze at docker run stage.

Docker docs -d flag