skip to Main Content

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 ?

2

Answers


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

    There are at least three things wrong with your Dockerfile IMO.

    The first one is not in direct relation to your problem, but you are creating way too many layers (one for each RUN command) for something as simple as adding a driver to your image. You should put all this in a single layer (i.e. a single RUN command) and cleanup after yourself at the end to keep the layer footprint small.

    Now the core of your real problem. As you can see in your output, apt-get is launched in interactive mode and asks for a confirmation. The docker build process can’t handle that and therefore aborts the command causing the build to fail. To overcome this, apt-get has a -y option to answer ‘yes’ to all prompts by default.

    The last one is in the line where you add the mongo driver to php.ini: you are redirecting echo output to your file with a single gt; sign (>), hence you are replacing the full content of the file you just copied. You must use a double gt; sign (>>) for content to be appended.

    The following Dockerfile should fix the problems above (tested without the copy of sources + cp of your own php.ini file since I don’t have them)

    FROM php:7.3-apache
    COPY src/ /var/www/html
    RUN apt-get update 
        && apt-get install -y --no-install-recommends openssl libssl-dev libcurl4-openssl-dev 
        && pecl install mongodb 
        && cp /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini 
        && echo "extension=mongodb.so" >> /usr/local/etc/php/php.ini 
        && apt-get clean 
        && rm -rf /var/lib/apt/lists/*
    EXPOSE 80
    

    Some explanation:

    • The && notation allows to run all commands one after the other in a single docker RUN command resulting in a single intermediate container, thus a single layer.
    • -y --no-install-recommends options to apt-get ask apt to not go interactive (answer yes everywhere) and to install only needed packages, not the recommended ones.
    • The two last instruction apt-get cleann && rm -rf /var/lib/apt/lists/* remove all caches made by running apt so that the layer stays as small as possible. (see apt-get chapter in docker best practice)
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search