Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to run shell script to pull image from dockerhub and run it

Juan Ignacio Benito December 23, 2020

Hi, I am creating a pipeline, and so far I've been able to to create the image and push it to docker hub. 

Now I need to log in to my EC2 instance, pull the image and run it. For that I have a shell script: 

#!bin/bashsudo docker psecho 'Login in to docker'aws ecr get-login-password |  - docker login --username $DOCKER_HUB_USERNAME --password $DOCKER_HUB_PASSWORD
'Fetching latest image'sudo docker pull juanibe/api:latestecho 'Stoping current container'sudo docker stop cont_docker_app_test
echo 'Removing old container'sudo docker rm cont_docker_app_test-old
echo 'Rename stoped container'
sudo docker rename cont_docker_app_test cont_docker_app_test_old
echo 'Starting new container'sudo docker run -d --name cont_docker_app_test -p 443:3333 -p 8001:8001 --link my-mongo-testing:my-mongo-testing juanibe/api:latest

 

And I have this yml file for bitbucket:


image: atlassian/default-image:latest
pipelines: default: - step: services: - docker script: - export IMAGE_NAME=juanibe/vinimayapi:$BITBUCKET_COMMIT - docker build -t $IMAGE_NAME . - docker login --username $DOCKER_HUB_USERNAME --password $DOCKER_HUB_PASSWORD - docker push $IMAGE_NAME

 

Where can I run the script?? 

1 answer

0 votes
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 29, 2020

Hi @Juan Ignacio Benito and welcome to the community.

One option is to commit and push your script to your repository, and then include in the yml file a command to execute this script.

The other option is to add the commands of your script in your yml file.

For both options, you can either do this on the existing step and script in your yml file, or you can add a second step and add the commands there.

Just a note that you'll need to remove 'sudo' from the script/commands. You can't use sudo in Pipelines and you don't need to, as when you are running the image you are root.

If you have any questions, please feel free to let us know!

Kind regards,
Theodora

Juan Ignacio Benito December 29, 2020

Thank you very much Theodora! It worked as expected.

 

I have one question left.

 

I need to add environment variables (the ones that I have in the .env file) for the source code. Is there any way to hide them and use them without commiting the .env file to the repo?

 

Thank you again!

Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 5, 2021

Hi Juan,

You are very welcome, I'm glad to hear that this worked!

Regarding variables, you can add variables either from the Repository settings ( > option Repository variables in the Pipelines section), if you want them to be available for this repo only, or at the workspace level (from workspace Settings > Workspace variables), if you want them to be available in all repos of this workspace.

When you create a variable you'll see a checkbox with the label Secured. If you select that, the variable's value won't be visible (and if you attempt to print it during the Pipelines build, its value still won't be shown).

Is this something that works for you?

Kind regards,
Theodora

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events