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

Looking for help, can pipelines use a local docker image? (pipelines beginner)

pcsdev August 2, 2023

I have managed to get pipelines to build a docker image (using a local dockerfile) and have saved it as an artifact.

I want to have the a step run a container from the local image and execute some commands in it that test the app that it hosts.  Below is a rough guess at how this might be set up in pipelines, but I'm wondering if pipelines requires the image to come from a registry.

From what Ive read I also need to define the config of the mysql service in a preceding definitions section too, I'm just excluding it below.

Can anyone point me in the right direction?

The goal is to have the app in the container tested while it has access to a mysql db, after which it can be deployed.

Thanks for reading.

pipelines:
branches:
development:
- step:
name: build image
services:
- docker
caches:
- docker
script:
- docker build -t image-name .
- docker save --output tmp-image.docker image-name
artifacts:
- tmp-image.docker
- step:
name: run image
image: ./tmp-image.docker
services:
- mysql
script:
- npm run test

edit:

I also tried uploading the image to AWS ECR and get an error because the error i get when the pipeline runs is:

There is a problem with the format of your docker image name at [pipelines > branches > develop > 2 > step > image > name].

I tried the format listed here:

https://support.atlassian.com/bitbucket-cloud/docs/use-docker-images-as-build-environments/#Private-images-hosted-by-AWS-ECR--EC2-Container-Registry-

this is what I have for the run step:

 - step:
name: RUN IMAGE
image:
name: ${AWS_DEV_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/name-of-ecr-repository:${BITBUCKET_BRANCH}_${BITBUCKET_COMMIT:0:7}
aws:
access-key: $AWS_ACCESS_KEY
secret-key: $AWS_SECRET_KEY
services:
- docker
- mysql
script:
- source set.env
- npm run test

and I get the same error... maybe you cant use variables in the image name?

 

edit#2:

figured out the last issue, it was typo.

Now I'm getting a 403 forbidden from ECR...   the journey continues.

 

edit3:

I fixed the ecr pull 403 error and now the next step (where I try to run the container) fails with no error output at all.

I took the bitbucket pipelines course and I want my $40 back. The databases/services coverage is 8 minutes and doesn't even include a mysql example.  In fact there's no example code provided anywhere as far as I can see, not even in the student guide pdf.  

 

 

2 answers

1 vote
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 4, 2023

Hi @pcsdev and welcome to the community!

Regarding your first question, it is not possible to use as a build container an image that you built in a previous step. The Docker image used as a build container needs to be retrieved from a Docker registry. You can always push the image to a registry and then, in the following step, specify a definition that uses the image from the registry.

Variables are not supported in the image name, we have a feature request about this here: https://jira.atlassian.com/browse/BCLOUD-13014

Regarding the issue you are currently facing, we would need some additional info to look into it. Could you please share the following?

  • The yml file you are currently using (make sure to sanitize any private data, prior to sharing)
  • The output of the command that is failing. In case there is an error next to the build log (right below the build number), it would be useful to have that as well.

Kind regards,
Theodora

pcsdev August 4, 2023

Thanks for the response! 

My yaml file is changing constantly as I'm basically just learning by trial and error at this point. 

Ive noticed that when I make a change that results in a yaml error or have some sort of syntax error I get a suggestion of where the error is.  However if I make some mistakes like attempting to use a yaml anchor (that passes in the BBP validator) in a service, I got no information other than the red error indication.

Please correct me if I'm wrong here- variables defined within the BB repo settings appear to be applied to each steps environment automatically, but are not applied to services such as mysql?  (and like you said cant be used in image names)

Also, can services have script content? (to run at startup of service)

thanks again!

Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 8, 2023

Hi @pcsdev!

The Bitbucket Pipelines Validator doesn't show any errors when YAML anchors are used, I'm afraid that this is a bug with the validator. We have an open bug report about this here: https://jira.atlassian.com/browse/BCLOUD-18299

Regarding variables in services, it is possible to use a variable you configured in repo settings in the variables of a service, for example:

definitions:
mysql:
image: mysql:5.7
variables:
MYSQL_DATABASE: my-db
MYSQL_ROOT_PASSWORD: $password

Is this what you are looking to do?

There is no option to add a script to the service definition, but you can run a script against a service during the build. You could add a command to execute a script in a step's script section in the yml file.

Kind regards,
Theodora

Like pcsdev likes this
0 votes
Caterina Curti
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
October 30, 2024

Hello,

Regarding the variables support in the image name, here is an article and a video I published showing how you can use a variable as the image name.

This solution is based on Dynamic Pipelines and requires a Forge app.

The article shows how to replace an "IMAGE_TAG" value (image: node:$IMAGE_TAG) but would work with no changes when the variable contains the full name of the image.

 

Hope this helps,
Caterina

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS
AUG Leaders

Atlassian Community Events