PHP bcmath extension doesn't get installed in Dockerfile

I have run into a strange problem where bcmath PHP extension doesn’t get installed in Dockerfile but later if I go into my container then I can install it manually. But the problem is if I restart the container all changes are lost.

My Dockerfile

FROM php:7.2-fpm


WORKDIR /var/www


RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl

RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd
RUN docker-php-ext-install bcmath


EXPOSE 9000
CMD ["php-fpm"]

By the output of php -m I see that bcmath extension did not get installed.

But I can install it manually like this

$ docker-compose exec php bash

$ docker-php-ext-install bcmath

$ kill -s USR2 1

Now bcmath extension is installed and works. But when I restart the container it’s gone again. Why didn’t it install it from Dockerfile I don’t understand… What am I doing wrong?

I just noticed that if I build my Dockerfile without docker-compose (docker build) then bcmath gets installed. But if I use Docker compose then it doesn’t get installed. Not sure what is going on…

Edit:
I finally found the problem. I removed all containers and all images and re-built everything and now everything installed correctly. So the solution was to rebuild the PHP image.