PHP extension are not getting install in Dockerfile

I am trying to run a docker-compose file for Magento 2. But getting stuck in a weird issue regarding the installation of the extension when I am building it via Dockerfile. Hear is my Dockerfile.

FROM php:7.4-fpm

# Install system dependencies
RUN apt-get update && apt-get install -y \
    libxml2-dev \
    libzip-dev \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    libpng-dev \
    libicu-dev \
    libxslt-dev \
    libcurl4-openssl-dev \
    libssl-dev \
    libonig-dev \
    libbz2-dev \
    libmcrypt-dev \
    libpng-dev \
    libgd-dev \
    libpq-dev \
    libxslt1-dev

# Install PHP extensions
RUN docker-php-ext-install \
    bcmath \
    ctype \
    curl \
    dom \
    fileinfo \
    filter \
    gd \
    hash \
    iconv \
    intl \
    json \
    mbstring \
    openssl \
    pcre \
    pdo_mysql \
    simplexml \
    soap \
    sockets \
    sodium \
    tokenizer \
    xmlwriter \
    xsl \
    zip \
    zlib

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# # Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# # Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

WORKDIR /var/www/html

And I am getting an error:
failed to solve: process "/bin/sh -c docker-php-ext-install bcmath ctype curl dom fileinfo filter gd hash iconv intl json mbstring openssl pcre pdo_mysql simplexml soap sockets sodium tokenizer xmlwriter xsl zip zlib" did not complete successfully: exit code: 2

I have tried building it even with PHP-8.3 using php:8.3-fpm-bookworm. But got the same error and have tried installing many other packages so to avoid dependency issues.

I do not want to install extensions in the container one at a time and it will defeat the purpose of using a docker container.

Please suggest what can I do to resolve my issue.

You dont have to install them one by one, but you need to figure out which one is causing the install to fail.
its probably a missing dependency, but for which module?

You need to remove, like, half and see if the error still occurs and then dig deeper from there.

Did you read “How to install more PHP extensions” from the docs?

When building an image you have a flag called --progress. By setting it to plain you’ll get more, extra, information. Can you try and see if you can see any error message ?