'Install mongodb driver in docker with dockerfile

I have a mongodb docker container. I need another docker container which will have php and apache installed. I want to run a php script from this container and send some data to the mongodb container to save the data in mongodb database. So i need to install mongodb driver in the php-apache container.

To do that, i have created following dockerfile:

FROM php:7.3-apache
COPY src/ /var/www/html
RUN apt-get update
RUN apt-get install openssl libssl-dev libcurl4-openssl-dev
RUN pecl install mongodb
RUN cp /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini
RUN echo "extension=mongodb.so" > /usr/local/etc/php/php.ini
EXPOSE 80

It first builds php-apache docker image. Then it should install mongodb driver.

But when i run the following command

docker build -t my-mongo .

At one point it shows following message and stops the execution:

Need to get 2213 kB of archives.
After this operation, 9593 kB of additional disk space will be used.
Do you want to continue? [Y/n] Abort.
The command '/bin/sh -c apt-get install openssl libssl-dev libcurl4-openssl-dev' returned a non-zero code: 1

What went wrong here? Is there anything wrong in the dockerfile ?



Solution 1:[1]

put this at the beginning of your Docker file, it is important to have it before installing all other extensions, otherwise it fails.

FROM php:7.3-apache
RUN apt-get update -y && apt-get upgrade \
    && pecl install mongodb && docker-php-ext-enable mongodb

That's all what I needed to get mongodb running FROM php:7.3-cli-buster Probably it will work for other versions - fpm, apache etc as well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1