How to run multiple commands in loaded docker image?
Asked by Ivo:

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 baseARG APP_ENV=devENV COMPOSER_ALLOW_SUPERUSER=1WORKDIR /appfolderENV 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/composerRUN mkdir -p -m 0600 /root/.ssh \    && ssh-keyscan bitbucket.org >> /root/.ssh/known_hosts \    && rm -rf /appfolder && mkdir /appfolderCOPY . /appfolderRUN --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/phpIf I run the tests on my local machine:
```

<br />

<br />

If I run the tests on my local machine:

```
/appfolder # vendor/bin/phpphp-cs-fixer  php-parse     phpstan       phpstan.phar  phpunit/app # vendor/bin/phpphp-cs-fixer  php-parse     phpstan       phpstan.phar  phpunit/app # vendor/bin/phpstanNote: 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
```

<br />

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,


Answers:
author: Patrik S
body: Hello [@Ivo](/t5/user/viewprofilepage/user-id/5517588) ,

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](/t5/user/viewprofilepage/user-id/5517588) !

Patrik S

comments: author: Ivo
body: Hello [@Patrik S](/t5/user/viewprofilepage/user-id/4603309) ,  

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:  

<br />

```
- 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
```

<br />

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)
```

<br />

Issue is resolved.   

Kind regards,


author: Patrik S
body: Awesome [@Ivo](/t5/user/viewprofilepage/user-id/5517588) ! 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


Tags: bitbucket-cloud

cloud