HiveBrain v1.2.0
Get Started
← Back to all entries
snippetdockerMinor

How to bind ports for docker container in bitbucket pipelines?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
portscontainerpipelinesdockerbitbucketforhowbind

Problem

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.

-
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


-
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?

Solution

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

Context

StackExchange DevOps Q#3876, answer score: 4

Revisions (0)

No revisions yet.