'How to use a script of a Docker container from CI pipeline

Newbie in Docker & Docker containers over here.

I'm trying to realize how can I run a script which is in the image from my bitbucket-pipeline process.

Some context about where I am and some knowledge

In a Bitbucket-Pipelines step you can add any image to run in that specific step. What I already tried and works without problem for example is get an image like alpine:node so I can run npm commands in my pipeline script:

definitions:
    steps:
        - step: &runNodeCommands
              image: alpine/node
              name: "Node commands"
              script:
                  - npm --version

pipelines:
    branches:
        master:
            - step: *runNodeCommands

This means that each push on master branch will run a build where using the alpine/node image we can run npm commands like npm --version and install packages.

What I've done

Now I'm working with a custom container where I'm installing a few node packages (like eslint) to run commands. I.E. eslint file1.js file2.js

Great!

What I'm trying but don't know how to

I've a local bash script awesomeScript.sh with some input params in my repository. So my bitbucket-pipelines.yml file looks like:

definitions:
    steps:
        - step: &runCommands
              image: my-user/my-container-with-eslint
              name: "Running awesome script"
              script:
                  - ./awesomeScript.sh -a $PARAM1 -e $PARAM2

pipelines:
    branches:
        master:
            - step: *runCommands

I'm using the same awesomeScript.sh in different repositories and I want to move that functionality inside my Docker container and get rid of that script in the repository

How can I build my Dockerfile to be able to run that script "anywhere" where I use the docker image?

PS:

I've been thinking in build a node_module, installing the module in the Docker Image like the eslint module... but I would like to know if this is possible

Thanks!



Solution 1:[1]

If you copy awesomeScript.sh to the my-container-with-eslint Docker image then you should be able to use it without needing the script in each repository.

Somewhere in the Dockerfile for my-container-with-eslint you can copy the script file into the image:

COPY awesomeScript.sh /usr/local/bin/

Then in Bitbucket-Pipelines:

definitions:
    steps:
        - step: &runCommands
              image: my-user/my-container-with-eslint
              name: "Running awesome script"
              script:
                  - awesomeScript -a $PARAM1 -e $PARAM2

pipelines:
    branches:
        master:
            - step: *runCommands

Solution 2:[2]

As peterevans said, If you copy the script to your docker image, then you should be able to use it without needing the script in each repository.

In your Dockerfile add the following line:

COPY awesomeScript.sh /usr/local/bin/ # you may use ADD too

In Bitbucket-Pipelines:

pipelines:
    branches:
        master:
            - step: 
                  image: <your user name>/<image name>
                  name: "Run script from the image"
                  script:
                      - awesomeScript -a $PARAM1 -e $PARAM2

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 peterevans
Solution 2 Mostafa Wael