Looking for help, can pipelines use a local docker image? (pipelines beginner)
Asked by pcsdev:

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.


Answers:
author: Theodora Boudale
body: Hi [@pcsdev](/t5/user/viewprofilepage/user-id/5282937) 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

comments: author: pcsdev
body: 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!


author: Theodora Boudale
body: Hi [@pcsdev](/t5/user/viewprofilepage/user-id/5282937)!

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


Tags: bitbucket-cloud

bitbucket-pipelines

cloud