Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

alex_storm's avatar

How to build docker image for gitlab container registry?

Hello! I creating application with gitlab package registry(dockerhub but local in gitlab).

I created my docker image for laravel only for testins env and next step deploy my app to servers.

But. when I testing deployment jobs I get error:


Composer could not find a composer.json file in /builds/

Docker image for testing:


FROM php:8-fpm-alpine

WORKDIR /var/www

RUN apk update \
    && apk add \
    $PHPIZE_DEPS \
    git \
    zlib-dev \
    libzip-dev \
    zip && \
    rm -rf /var/lib/apt/lists

RUN pecl install  igbinary \
    && docker-php-ext-enable igbinary  \
    && pecl install msgpack \
    && docker-php-ext-enable msgpack  \
    && docker-php-ext-install bcmath pcntl sockets pdo_mysql zip

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

Gitlab-ci.yml


image: $CI_REPOSITORY/gitlab-ci

stages:
    - composer
    - build
    - syntax
    - phpunit

cache:
    paths:
        - vendor/
        - .env

composer:
    stage: composer
    script:
        - composer install --prefer-dist --no-ansi --no-interaction --no-progress --no-scripts
    only:
        changes:
            - composer.json

# Prepare project
build:
    stage: build
    script:
        - cp .env.testing .env
        - php artisan key:generate
    only:
        changes:
            - .env.testing

syntax:
    stage: syntax
    script:
        - composer dump-autoload
        - vendor/bin/phpcs --error-severity=1 --warning-severity=8 --extensions=php
    only:
        changes:
            - "**/*.php"

phpunit:
    stage: phpunit
    script:
        - vendor/bin/phpunit --bootstrap vendor/autoload.php --configuration phpunit.xml --testsuite Unit
    only:
        changes:
            - composer.json
            - app/**/*
            - config/**/*
            - tests/**/*

So, I wanted to ask, does the application code really need to be stored in the image, or can it be copied there during after pushing to gitlab?

Do I need make something like this:


COPY . /var/www

Or mb this?


docker run --name gitlab-test-ci -v .:/var/www -dp 80:80 gitlab-test-ci

I mean how can I fix error in my application and how can make correctly settings for gitlab package registry?

Thanks

0 likes
7 replies
Yamis1745's avatar

@alex_storm this error means that your Gitlab-ci.yml file is assuming that your composer.json file is should be in your /builds/ directory, and so throws an error when it can't find the file there.

Composer could not find a composer.json file in /builds/

It seems your Gitlab-ci.yml file resides inside the /builds/ directory, and your composer.json file resides in the root directory, hence you need to change path reference to the composer.json file so that your Gitlab-ci.yml file will know where to find it. Solution: Where ever you are referencing the composer.json file inside your Gitlab-ci.yml file, be sure to declare the path like this:

../composer.json

I believe that's all you need to do.

alex_storm's avatar

@Yamis1745 Thank you for your answer. I understand the problem and understand that it is caused by the lack of files in the image. The question is how to get the correct files in the image for further flow.

Yamis1745's avatar

@alex_storm even if you get all your files into the image, the error will still remain as your yaml file is referencing a composer.json file that does not reside in the same directory as it does.

Now to get all the files into the image, simply add the COPY files command into your Dockerfile so that the files are copied from your local directory over to the /var/www path in the image.:

...
...
COPY . /var/www
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

Hope that helps

alex_storm's avatar

@Yamis1745 Hi. no) I did a copy for temp solution. For one or two developers it's ok, but when will be work a team and will be more branches its bad solution.

So, I searching another solution and do exexperiments

Please or to participate in this conversation.