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

How to run multiple commands in loaded docker image?

Ivo
Contributor
July 10, 2024

Hi guys ,

I have Bitbucket pipeline with 3 steps. In the 1st step the pipeline build dockerimage with php 8.2 on Alpine 3.20 and save it. In the 2nd step docker load the custom image, volume and then I need to run composer install and some tests.

My code is below:

 

  steps:
- step: &build
    name: Docker Build
    runs-on:
      - self.hosted
      - linux.arm64
      - apps 
    script:
      - DOCKER_BUILDKIT=1 docker build -f Dockerfile -t url/to/our/image/repo .
      - docker save --output tmp-image.docker url/to/our/image/repo
    artifacts:
      - tmp-image.docker
    services:
      - docker

- step: &test
    name: Composer Tests
    runs-on:
      - self.hosted
      - linux.arm64
      - apps 
    script:
      - *setEnv
      - docker load --input ./tmp-image.docker
      - docker images
      - docker run -v ${BITBUCKET_CLONE_DIR}:/appfolder
       url/to/out/image/repo composer install && ./vendor/bin/phpunit 
    services:
      - docker

 

This is my Dockerfile:

 

FROM url/to/our/image/repo:8.2.20-fpm-alpine as base

ARG APP_ENV=dev

ENV COMPOSER_ALLOW_SUPERUSER=1

WORKDIR /appfolder

ENV PATH="$PATH:/usr/bin/php"

RUN sed -i "s|listen = 9000|listen = /var/run/php/fpm.sock\nlisten.mode = 0666|" /usr/local/etc/php-fpm.d/zz-docker.conf \
    && mkdir -p /var/run/php/

COPY --from=composer/composer /usr/bin/composer /usr/local/bin/composer
RUN mkdir -p -m 0600 /root/.ssh \
    && ssh-keyscan bitbucket.org >> /root/.ssh/known_hosts \
    && rm -rf /appfolder && mkdir /appfolder

COPY . /appfolder

RUN --mount=type=ssh,id=key set -xe \
    && if [ "$APP_ENV" = "prod" ]; then export ARGS="--no-dev"; fi \
    && composer install --prefer-dist --no-scripts --no-progress --no-interaction $ARGS \
    && composer dump-autoload --classmap-authoritative \
    &&  rm -rf /root/.ssh/ \
    && php -d memory_limit=256M

 

Some more information:

 

/appfolder # echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/php
If I run the tests on my local machine:



If I run the tests on my local machine:

 

/appfolder # vendor/bin/php
php-cs-fixer  php-parse     phpstan       phpstan.phar  phpunit
/app # vendor/bin/php
php-cs-fixer  php-parse     phpstan       phpstan.phar  phpunit
/app # vendor/bin/phpstan
Note: Using configuration file /appfolder/phpstan.dist.neon.
 6/6 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 

Below is the error from step 2 in the pipeline from the second command from the test:

/usr/bin/env: ‘php’: No such file or directory


I already tried with the following paths for the tests "./vendor/bin/phpunit", "vendor/bin/phpunit", "php vendor/bin/phpunit", "php ./vendor/bin/phpunit", "/usr/bin/php vendor/bin/phpunit".

What I'm doing wrong?

Regards,

1 answer

1 accepted

0 votes
Answer accepted
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 11, 2024

Hello @Ivo ,

thank you for reaching out to Community!

I suspect the issue is happening because you're using a slightly wrong syntax when trying to execute multiple commands as part of the docker run in your second step.

To be able to run multiple commands in a single docker run, you can make use of /bin/bash -c and separate the commands with a semicolon like in the following example:

docker run ubuntu:latest /bin/bash -c "command1 ; command2"

Could you try updating your step to use that syntax and let us know how it goes?

Thank you, @Ivo !

Patrik S

Ivo
Contributor
July 11, 2024

Hello @Patrik S ,

I struggled with this issue for the last couple of days and I can't believe the issue was related to the syntax, not php path, missing package or so on.  Thank you very much! Below is the corrected part of the step with the proper syntax:


- step: &test
    name: Composer Tests
    runs-on:
      - self.hosted
      - linux.arm64
      - apps 
    script:
      - docker load --input ./tmp-image.docker
      - docker images
      - docker run -v ${BITBUCKET_CLONE_DIR}:/appfolder
       url/to/out/image/repo /bin/bash -c "composer install ; ./vendor/bin/phpunit"
services:
- docker


As a result now I have the right execution of composer phpunit test:

Executing script cache:clear [OK]

Executing script assets:install public [OK]

PHPUnit 9.6.19 by Sebastian Bergmann and contributors.

Testing

............................................................... 63 / 168 ( 37%)

............................................................... 126 / 168 ( 75%)

.......................................... 168 / 168 (100%)

Time: 00:17.855, Memory: 116.50 MB

OK (168 tests, 447 assertions)

 
Issue is resolved. 

Kind regards,

Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 11, 2024

Awesome @Ivo ! Happy to have been of some help and to know your pipeline is succeeding after that change :)

If you ever need help in the future, feel free to reach out!

Have a nice day!

Patrik S

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events