Execute linux EC2 commands from Bitbucket Pipeline


April 2019


4 time


I have a pipeline that is successfully creating a docker image and pushing it to Docker Hub. Now I need to ssh to an EC2 and pull that image along with all the other docker commands (i.e: stop the old container and run the new one).

I created an SSH key in Bitbucket and added the public key to the EC2 instance in ~/.ssh/authrized_keys file.

The following is part of the yml file to execute the commands:

      - ssh [email protected]
      - sudo docker stop $(docker ps -a -q)
      - sudo docker rm $(docker ps -a -q)
      - sudo docker pull $IMAGE_NAME
      - sudo docker run -p 80:8081 -d $IMAGE_NAME

The build output shows the following ssh [email protected]

 sudo docker stop $(docker ps -a -q)

    + sudo docker stop $(docker ps -a -q)
    bash: docker: command not found
    bash: sudo: command not found

I'm not getting an error for the ssh line but not sure why it not accepting docker or sudo commands.

Is this correct way of connecting or is there a better way of ssh to EC2 and execute commands within a pipeline?

0 answers