简体   繁体   English

在 docker 和 dockerfile 中安装 mongodb 驱动程序

[英]Install mongodb driver in docker with dockerfile

I have a mongodb docker container.我有一个 mongodb docker 容器。 I need another docker container which will have php and apache installed.我需要另一个 docker 容器,它将安装 php 和 apache。 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.我想从此容器运行 php 脚本并将一些数据发送到 mongodb 容器以将数据保存在 mongodb 数据库中。 So i need to install mongodb driver in the php-apache container.所以我需要在 php-apache 容器中安装 mongodb 驱动程序。

To do that, i have created following dockerfile:为此,我创建了以下 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.它首先构建 php-apache docker 映像。 Then it should install mongodb driver.然后它应该安装 mongodb 驱动程序。

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? dockerfile有什么问题吗?

What went wrong here?这里出了什么问题? Is there anything wrong in the dockerfile? dockerfile有什么问题吗?

There are at least three things wrong with your Dockerfile IMO.您的 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.第一个与您的问题没有直接关系,但是您正在创建太多层(每个RUN命令一个),以实现向图像添加驱动程序这样简单的事情。 You should put all this in a single layer (ie a single RUN command) and cleanup after yourself at the end to keep the layer footprint small.您应该将所有这些放在一个层中(即单个RUN命令)并在最后自己清理以保持层占用空间小。

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.正如您在 output 中看到的那样, apt-get以交互模式启动并要求确认。 The docker build process can't handle that and therefore aborts the command causing the build to fail. docker 构建过程无法处理该问题,因此会中止导致构建失败的命令。 To overcome this, apt-get has a -y option to answer 'yes' to all prompts by default.为了克服这个问题, apt-get有一个-y选项,默认情况下对所有提示回答“是”。

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;最后一个是在您将 mongo 驱动程序添加到php.ini的行中:您正在使用单个 gt; 将 echo output 重定向到您的文件; sign ( > ), hence your are replacing the full content of the file you just copied.符号 ( > ),因此您正在替换刚刚复制的文件的全部内容。 You must use a double gt;您必须使用双 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)下面的 Dockerfile 应该可以解决上述问题(在没有源副本的情况下进行了测试

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. &&表示法允许在单个 docker RUN命令中一个接一个地运行所有命令,从而产生单个中间容器,因此是单个层。
  • -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. -y --no-install-recommends options to apt-get ask apt to not go 交互式(到处回答是)并只安装需要的软件包,而不是推荐的软件包。
  • 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.最后两条指令apt-get cleann && rm -rf /var/lib/apt/lists/*删除通过运行 apt 生成的所有缓存,以便层保持尽可能小。

put this at the beginning of your Docker file, it is important to have it before installing all other extensions, otherwise it fails.将它放在 Docker 文件的开头,在安装所有其他扩展之前拥有它很重要,否则它会失败。

FROM php:7.3-cli-buster
RUN apt-get update -y && apt-get upgrade -y;
RUN 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.这就是让 mongodb FROM php:7.3-cli-buster运行所需的全部内容可能它也适用于其他版本 - fpm、apache 等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM