Search code examples
dockerbitbucketdigital-oceanbitbucket-pipelines

BitBucket Pipeline cannot find container after ssh into DigitalOcean Droplet


Here is my code

- step:
    name: SSH to Digital Ocean and update docker image
    script:
      - head ~/.ssh/config
      - ssh -i ~/.ssh/config root@XXX.XXX.XXX.XXX
      - docker ps
      - docker rm -f gvcontainer
      - docker image rm -f myrepo/myimage:tag
      - docker pull myrepo/myimage:tag
      - docker run --name gvcontainer -p 12345:80 -d=true --restart=always myrepo/myimage:tag
    services:
      - docker

Here I can see that the Pipeline ssh into my DO droplet successfully, but for some reason (I guess, it was too quick to type the "docker ps"; it should to wait a few seconds, but I just don't know how to postpone the operation) it could not find the container.

So I manually ssh into my droplet and checked, the gvcontainer is there.

enter image description here

Why might be the reasons for this?


Solution

  • The commands listed after your SSH session are not being run on the remote system - they're being run in Pipelines. Since the Pipelines container doesn't have a gvcontainer to remove, it returns that error.

    You have several options, one of which I outlined in answering your other question (pass the commands as arguments to SSH, as in ssh -i /path/to/key user@host "command1 && command2"). Another option would be to put a script on the droplet that does all the things you want, and have Pipelines execute it via SSH (ssh -i /path/to/key user@host "./do-all-the-things.sh").