<p>Try running with <code>-d detached</code> flag</p>

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

<p><a href="https://docs.docker.com/engine/reference/run/#detached--d" rel="nofollow noreferrer">Docker docs -d flag</a></p>

CC BY-SA 4.0
2018-05-15T09:36:34.010Z
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```
ConorSheehan1
CC BY-SA 4.0
2018-05-13T12:59:17.150Z
2018-05-13T12:59:17.150Z
ANTARA
<p>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.</p>

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

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

<ol>
<li><p>I tried using the container directly using this in my bitbucket-pipelines.yml:</p>

<pre><code>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
</code></pre>

<p>But I get the following error at the <code>bundle exec rspec</code> step</p>

<pre><code># Errno::ECONNREFUSED:
#   Connection refused - connect(2) for "127.0.0.1" port 8983
</code></pre></li>
<li><p>I tried running the docker container directly within the bitbucket pipeline</p>

<pre><code>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
</code></pre>

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

CC BY-SA 3.0
2018-04-13T14:28:10.757Z
`-t` to prevent that the container exits? Did you run it locally?
030
CC BY-SA 3.0
2018-04-13T14:52:36.623Z
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```
ConorSheehan1
CC BY-SA 3.0
2018-04-13T16:44:17.773Z
`-t, --tty                     Allocate a pseudo-TTY`
030
<docker><bitbucket><bitbucket-pipelines>
How to bind ports for docker container in bitbucket pipelines?
ConorSheehan1
# How to bind ports for docker container in bitbucket pipelines?

(Asked by: username_0 on 2018-04-13)

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?

**Tags:** docker, bitbucket, bitbucket-pipelines

> **Comments:**
> **username_1**: `-t` to prevent that the container exits? Did you run it locally?  
> **username_0**: 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```  
> **username_1**: `-t, --tty                     Allocate a pseudo-TTY`  

---

## 1 answers

### Answer by username_2 on 2018-05-13. Score: 4 | Accepted answer

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](https://docs.docker.com/engine/reference/run/#detached--d)

> **Comments:**
> **username_0**: 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```  

---

