6

I'd like to test an application using bitbucket pipelines with a custom docker image running some services. I have a dev docker image which runs all the services I need in order for tests to pass.

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

I can't seem to find a way to start the container with the right ports bound so the tests to pass.

  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?

3
  • -t to prevent that the container exits? Did you run it locally?
    – 030
    Apr 13, 2018 at 14:28
  • Yeah it runs fine locally. I used the -t flag in my second example, but the pipeline freezes when I run docker run -t -p 3000:3000 -p 6379:6379 -p 8983:8983 my_dockerhub/image Apr 13, 2018 at 14:52
  • -t, --tty Allocate a pseudo-TTY
    – 030
    Apr 13, 2018 at 16:44

1 Answer 1

4

Try running with -d detached flag

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

1
  • 1
    Thanks, this was almost it! Bitbucket pipelines doesn't support tty so i needed detached mode with stdin open. The thing that finally got it working was docker run -id -p 3000:3000 -p 6379:6379 -p 8983:8983 -v $PWD:$work_dir $my_image May 15, 2018 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.