Deploy pipeline failed: port is already allocated.
Asked by Jorge Jimenez:

Hi!

<br />

I don't know how to stop the container that is already running through the pipe.  
I try it this way:

*docker stop $(docker ps -a -q --filter name=front\* --format="{{.ID}}") ;*

*docker rm$(docker ps -a -q --filter name=front\* --format="{{.ID}}") ;*

*The pipeline error:*

*docker: Error response from daemon: driver failed programming external connectivity on endpoint thirsty_feynman (3e184c5114a8476b9abbf08ccf79153b350e3b7c424305acecb169): Bind for 0.0.0.0:8082 failed: port is already allocated.*

It works if I try manually inside my docker machine (Stop correctly and then delete the container ).

It doesn't work if I try it from the pipe.  

My Pipe:
> branches:  
> master:  
> - step:  
> name: Deploy to Registry  
> script:  
> - export IMAGE_NAME=angular-image:$BITBUCKET_COMMIT  
> - docker build -t $IMAGE_NAME .  
> - docker login -u $registrytoken -p $registrytoken registry.digitalocean.com  
> - docker tag $IMAGE_NAME registry.digitalocean.com/myregistry/$IMAGE_NAME  
> - docker push registry.digitalocean.com/myregistry/$IMAGE_NAME  
> - docker image ls  
> - echo "$IMAGE_NAME"  
> services:  
> - docker  
>
> - step:  
> name: Deploy to droplet  
> script:  
> - export IMAGE_NAME=angular-image:$BITBUCKET_COMMIT  
> - export IMAGE_NAME_myregistry=registry.digitalocean.com/myregistry/angular-image:$BITBUCKET_COMMIT  
> - export registrytoken=$registrytoken  
> - echo "$registrytoken"  
> - echo "$IMAGE_NAME"  
> - echo "$IMAGE_NAME_myregistry"  
> - pipe: atlassian/ssh-run:0.3.0  
> variables:  
> SERVER: '$SSH_SERVER'  
> SSH_USER: '$SSH_USER'  
> SSH_key: '$registrytoken'  
> COMMAND: \>  
> docker stop $(docker ps -a -q --filter name=front\* --format="{{.ID}}") ;  
> docker rm $(docker ps -a -q --filter name=front\* --format="{{.ID}}") ;  
> docker rmi -f $(docker images "angular-image") ;  
> docker rmi -f $(docker images "registry.digitalocean.com/myregistry/angular-image") ;  
> docker login -u $registrytoken -p $registrytoken registry.digitalocean.com ;  
> docker pull $IMAGE_NAME_myregistry ;  
> docker tag $IMAGE_NAME_myregistry angular-image ;  
> docker run -p $SERVER_PORT:80 -d angular-image ;  
> services:  
> - docker  
> definitions:  
> services:  
> docker:  
> memory: 3072


Answers:
author: Theodora Boudale
body: Hi [@Jorge Jimenez](/t5/user/viewprofilepage/user-id/3574024) ,

My first suggestion would be to add the option **-f** in the **docker rm** command and see if this works.

In case you're still having issues, I would suggest adding the variable **DEBUG: 'true'** in your bitbucket-pipelines.yml file in the definition of the pipe, as follows:

```
- pipe: atlassian/ssh-run:0.3.0
  variables:
    SERVER: '$SSH_SERVER'
    SSH_USER: '$SSH_USER'
    SSH_key: '$registrytoken'
    COMMAND: >
    docker stop $(docker ps -a -q --filter name=front* --format="{{.ID}}") ;
    docker rm $(docker ps -a -q --filter name=front* --format="{{.ID}}") ;
    docker rmi -f $(docker images "angular-image") ;
    docker rmi -f $(docker images "registry.digitalocean.com/myregistry/angular-image") ;
    docker login -u $registrytoken -p $registrytoken registry.digitalocean.com ;
    docker pull $IMAGE_NAME_myregistry ;
    docker tag $IMAGE_NAME_myregistry angular-image ;
    docker run -p $SERVER_PORT:80 -d angular-image ;
    DEBUG: 'true'
```

and then share the full output of the pipe command here (please make sure to sanitize any private/sensitive info in the output, prior to sharing it), so we can see if there is any extra debugging info in the output that will help understand why this is happening.

Please feel free to let me know if you have any questions.

Kind regards,  
Theodora

comments: author: Jorge Jimenez
body: Good Morning Theodora,

First of all, thanks for responding.

I'm going to copy the output of the pipeline:

```
pipe: atlassian/ssh-run:0.3.011s

+ docker container run \
--volume=/opt/atlassian/pipelines/agent/build:/opt/atlassian/pipelines/agent/build \
--volume=/opt/atlassian/pipelines/agent/ssh:/opt/atlassian/pipelines/agent/ssh:ro \
--volume=/usr/local/bin/docker:/usr/local/bin/docker:ro \
--volume=/opt/atlassian/pipelines/agent/build/.bitbucket/pipelines/generated/pipeline/pipes:/opt/atlassian/pipelines/agent/build/.bitbucket/pipelines/generated/pipeline/pipes \
--volume=/opt/atlassian/pipelines/agent/build/.bitbucket/pipelines/generated/pipeline/pipes/atlassian/ssh-run:/opt/atlassian/pipelines/agent/build/.bitbucket/pipelines/generated/pipeline/pipes/atlassian/ssh-run \
--workdir=$(pwd) \
--label=org.bitbucket.pipelines.system=true \
--env=BITBUCKET_STEP_TRIGGERER_UUID="$BITBUCKET_STEP_TRIGGERER_UUID" \
--env=BITBUCKET_REPO_FULL_NAME="$BITBUCKET_REPO_FULL_NAME" \
--env=BITBUCKET_GIT_HTTP_ORIGIN="$BITBUCKET_GIT_HTTP_ORIGIN" \
--env=BITBUCKET_PROJECT_UUID="$BITBUCKET_PROJECT_UUID" \
--env=BITBUCKET_REPO_IS_PRIVATE="$BITBUCKET_REPO_IS_PRIVATE" \
--env=BITBUCKET_WORKSPACE="$BITBUCKET_WORKSPACE" \
--env=BITBUCKET_REPO_OWNER_UUID="$BITBUCKET_REPO_OWNER_UUID" \
--env=BITBUCKET_BRANCH="$BITBUCKET_BRANCH" \
--env=BITBUCKET_REPO_UUID="$BITBUCKET_REPO_UUID" \
--env=BITBUCKET_PROJECT_KEY="$BITBUCKET_PROJECT_KEY" \
--env=BITBUCKET_REPO_SLUG="$BITBUCKET_REPO_SLUG" \
--env=CI="$CI" \
--env=BITBUCKET_REPO_OWNER="$BITBUCKET_REPO_OWNER" \
--env=BITBUCKET_STEP_RUN_NUMBER="$BITBUCKET_STEP_RUN_NUMBER" \
--env=BITBUCKET_BUILD_NUMBER="$BITBUCKET_BUILD_NUMBER" \
--env=BITBUCKET_GIT_SSH_ORIGIN="$BITBUCKET_GIT_SSH_ORIGIN" \
--env=BITBUCKET_PIPELINE_UUID="$BITBUCKET_PIPELINE_UUID" \
--env=BITBUCKET_COMMIT="$BITBUCKET_COMMIT" \
--env=BITBUCKET_CLONE_DIR="$BITBUCKET_CLONE_DIR" \
--env=PIPELINES_JWT_TOKEN="$PIPELINES_JWT_TOKEN" \
--env=BITBUCKET_STEP_UUID="$BITBUCKET_STEP_UUID" \
--env=BITBUCKET_DOCKER_HOST_INTERNAL="$BITBUCKET_DOCKER_HOST_INTERNAL" \
--env=DOCKER_HOST="tcp://host.docker.internal:2375" \
--env=BITBUCKET_PIPE_SHARED_STORAGE_DIR="/opt/atlassian/pipelines/agent/build/.bitbucket/pipelines/generated/pipeline/pipes" \
--env=BITBUCKET_PIPE_STORAGE_DIR="/opt/atlassian/pipelines/agent/build/.bitbucket/pipelines/generated/pipeline/pipes/atlassian/ssh-run" \
--env=COMMAND="docker stop $(docker ps -a -q --filter name=crmfront* --format=\"{{.ID}}\") ; docker rm -f $(docker ps -a -q --filter name=crmfront* --format=\"{{.ID}}\") ; docker stop $(docker ps -a -q --filter ancestor=angular-crm --format=\"{{.ID}}\") ; docker rm -f $(docker ps -a -q --filter ancestor=angular-crm --format=\"{{.ID}}\") ; docker rmi -f $(docker images \"angular-crm*\") ; docker rmi -f $(docker images \"registry.digitalocean.com/myregistry/angular-crm\") ; docker login -u $containerregistrymyregistrytoken -p $containerregistrymyregistrytoken registry.digitalocean.com ; docker pull $IMAGE_NAME_myregistry ; docker tag $IMAGE_NAME_myregistry angular-crm ; docker run --name crmfront_$BITBUCKET_COMMIT -p 80:80 --add-host=\"my_db-postgresql.ondigitalocean.com:?????????\" -d angular-crm:latest ; DEBUG: 'true'
" \
--env=SERVER="$SSH_SERVER" \
--env=SSH_USER="$SSH_USER" \
--env=SSH_key="$containerregistrymyregistrytoken" \
--add-host="host.docker.internal:$BITBUCKET_DOCKER_HOST_INTERNAL" \
bitbucketpipelines/ssh-run:0.3.0
Unable to find image 'bitbucketpipelines/ssh-run:0.3.0' locally
0.3.0: Pulling from bitbucketpipelines/ssh-run
...
f42830dcc46d: Pull complete
2e8fb81be632: Pull complete
Digest: sha256:34t5g6h2915fd9ade755017cf9224e
Status: Downloaded newer image for bitbucketpipelines/ssh-run:0.3.0
INFO: Executing the pipe...
INFO: Using default ssh key
INFO: Executing command on ???.??.??.??
ssh -A -tt -i /root/.ssh/pipelines_id -o StrictHostKeyChecking=no -p 22 root@???.??.??.?? bash -c 'docker stop ; docker rm -f ; docker stop ; docker rm -f ; docker rmi -f REPOSITORY TAG IMAGE ID CREATED SIZE ; docker rmi -f REPOSITORY TAG IMAGE ID CREATED SIZE ; docker login -u $containerregistrymyregistrytoken -p $containerregistrymyregistrytoken registry.digitalocean.com ; docker pull registry.digitalocean.com/myregistry/angular-crm:d7f6913f64e99c10acb2ab860268fdfb789518e6 ; docker tag registry.digitalocean.com/myregistry/angular-crm:d7f6913f64e99c10acb2ab860268fdfb789518e6 angular-crm ; docker run --name crmfront_d7f6913f64e99c10acb2ab860268fdfb789518e6 -p 80:80 --add-host="my_db-postgresql.ondigitalocean.com:?????????" -d angular-crm:latest ; DEBUG: 'true'
'
"docker stop" requires at least 1 argument.
See 'docker stop --help'.
Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]
Stop one or more running containers
"docker rm" requires at least 1 argument.
See 'docker rm --help'.
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
"docker stop" requires at least 1 argument.
See 'docker stop --help'.
Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]
Stop one or more running containers
"docker rm" requires at least 1 argument.
See 'docker rm --help'.
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
Error response from daemon: invalid reference format: repository name must be lowercase
Error response from daemon: invalid reference format: repository name must be lowercase
Error response from daemon: invalid reference format: repository name must be lowercase
Error response from daemon: invalid reference format: repository name must be lowercase
Error response from daemon: invalid reference format: repository name must be lowercase
Error response from daemon: invalid reference format: repository name must be lowercase
Error response from daemon: invalid reference format: repository name must be lowercase
Error response from daemon: invalid reference format: repository name must be lowercase
Error response from daemon: invalid reference format: repository name must be lowercase
Error response from daemon: invalid reference format: repository name must be lowercase
Error response from daemon: invalid reference format: repository name must be lowercase
Error response from daemon: invalid reference format: repository name must be lowercase
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
d7f6913f64e99c10acb2ab860268fdfb789518e6: Pulling from myregistry/angular-crm
e7c96db7181b: Already exists 
3fb6217217ef: Already exists 
b33afde1ff9e: Pulling fs layer 
b33afde1ff9e: Downloading 106.6kB/10.42MB
b33afde1ff9e: Downloading 7.532MB/10.42MB
b33afde1ff9e: Verifying Checksum 
b33afde1ff9e: Download complete 
b33afde1ff9e: Extracting 131.1kB/10.42MB
b33afde1ff9e: Extracting 262.1kB/10.42MB
...
b33afde1ff9e: Pull complete 
Digest: sha256:d1309af9c0967fa6646694b1071c4757b520b772f279d4e2313adf7c2f167216
Status: Downloaded newer image for registry.digitalocean.com/myregistry/angular-crm:d7f6913f64e99c10acb2ab860268fdfb789518e6
registry.digitalocean.com/myregistry/angular-crm:d7f6913f64e99c10acb2ab860268fdfb789518e6
c886efb82b360bf6d4ea733df0328fe763056391b0af0d8e4442c0f1965b4294
docker: Error response from daemon: driver failed programming external connectivity on endpoint crmfront_d7f6913f64e99c10acb2ab860268fdfb789518e6 (gfgdsgdfg): Bind for 0.0.0.0:80 failed: port is already allocated.
Connection to ???.??.??.?? closed.
bash: DEBUG:: command not found
? Execution failed.
```

The debug command did not work in my pipeline. And the -f doesn't seem like it either (I've checked it on the machine itself as well).

Any other idea? Have I put the DEBUG in the wrong way?

Thanks in advance


author: Theodora Boudale
body: Hi [@Jorge Jimenez](/t5/user/viewprofilepage/user-id/3574024) ,

Thanks for posting the full output of the pipe.

I reached out to the development team about this, the issue here may have to do with the argument of the **docker stop** and **docker rm** commands

```
$(docker ps -a -q --filter name=front* --format="{{.ID}}")
```

being evaluated in the Bitbucket container and not in the target host. Since in the Bitbucket container the filter doesn't match, the stop and rm commands are running against an empty argument. Therefore, no container is stopped or removed.

We can see in the output:

```
docker stop ; docker rm -f ; docker stop ; docker rm -f ;
```

If you escape **$()** in the docker stop and docker rm commands, or put it in single quotes, this should skip evaluation on Bitbucket side and give the raw value to the pipe to then evaluate to its environment.

Could you give this a try and let me know how it goes?

Kind regards,  
Theodora


author: Jorge Jimenez
body: Good afternoon,

I have solved it in another way I have put the docker commands into a simple script. (cleanenviroment.sh)

```

- pipe: atlassian / ssh-run: 0.3.0
variables:
SERVER: '$ SSH_SERVER'
SSH_USER: '$ SSH_USER'
SSH_key: '$ containerregistrytoken'
COMMAND:>
bash ./cleanenviromentlogin.sh
docker login -u $registrytoken -p $registrytoken registry.digitalocean.com ;
docker pull $IMAGE_NAME_myregistry ;
docker tag $IMAGE_NAME_myregistry angular-image ;
docker run -p $SERVER_PORT:80 -d angular-image ;

--------------------------------------------------------------------------
cleanenviroment.sh

#! / bin / bash
docker rm -f $ (docker ps -a -q --filter name = back* --format = "{{. ID}}")
docker rm -f $ (docker ps -a -q --filter ancestor = spring* --format = "{{. ID}}")
docker rmi -f $ (docker image ls "spring*")
docker rmi -f $ (docker image ls "registry.digitalocean.com/myreg/spring*")
```

And to finish, could you tell me how to set debug mode? It didn't work for me  

```
bash: DEBUG:: command not found
```

Thanks!


Tags: bitbucket-cloud

bitbucket-pipelines

cloud

digital-ocean

docker

ssh-run