如何在没有构建工具的情况下为Ruby项目构建Docker映像? [英] How do I build a Docker image for a Ruby project without build tools?

查看:118
本文介绍了如何在没有构建工具的情况下为Ruby项目构建Docker映像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Ruby项目构建Docker映像。问题是项目有一些需要构建本机扩展的gem依赖项。我的理解是,我有几个选择:

I'm trying to build a Docker image for a Ruby project. The problem is the project has some gem dependencies that need to build native extensions. My understanding is that I have a couple of choices:


  1. 从已经安装了构建工具的基础图像开始。

  2. 使用没有构建工具的基本映像,然后在运行 bundle install 之前,在Dockerfile中安装构建工具作为步骤。

  3. 预先编译主机上的本地扩展程序,供应gem,然后将生成的包复制到图像中。

  1. Start with a base image that already has build tools installed.
  2. Use a base image with no build tools, install build tools as a step in the Dockerfile before running bundle install.
  3. Precompile the native extensions on the host, vendorize the gem, and simply copy the resulting bundle into the image.

1& ; 2似乎要求生成的映像包含构建本机扩展所需的构建工具。为了安全起见,我试图避免这种情况。 3是麻烦的,但可以做的,并且会完成我想要的。

1 & 2 seem to require that the resulting image contains the build tools needed to build the native extensions. I'm trying to avoid that scenario for security reasons. 3 is cumbersome, but doable, and would accomplish what I want.

有没有什么选择我失踪或我误会了什么?

Are there any options I'm missing or am I misunderstanding something?

推荐答案

我一直使用选项3,目标是最终得到只有我需要运行的图像 (不编译)

I use option 3 all the time, the goal being to end up with an image which has only what I need to run (not to compile)

例如,这里我首先构建和安装Apache ,然后将生成的映像作为我的(修补和重新编译)Apache设置的基本映像。

For example, here I build and install Apache first, before using the resulting image as a base image for my (patched and recompiled) Apache setup.

Build:

if [ "$(docker images -q apache.deb 2> /dev/null)" = "" ]; then
  docker build -t apache.deb -f Dockerfile.build . || exit 1
fi

Dockerfile.build 声明一个包含生成的Apache重新编译(在deb文件中)的卷

The Dockerfile.build declares a volume which contains the resulting Apache recompiled (in a deb file)

RUN checkinstall --pkgname=apache2-4 --pkgversion="2.4.10" --backup=no --deldoc=yes --fstrans=no --default
RUN mkdir $HOME/deb && mv *.deb $HOME/deb
VOLUME /root/deb

安装: p>

Installation:

if [ "$(docker images -q apache.inst 2> /dev/null)" = "" ]; then
    docker inspect apache.deb.cont > /dev/null 2>&1 || docker run -d -t --name=apache.deb.cont apache.deb
    docker inspect apache.inst.cont > /dev/null 2>&1 || docker run -u root -it --name=apache.inst.cont --volumes-from apache.deb.cont --entrypoint "/bin/sh" openldap -c "dpkg -i /root/deb/apache2-4_2.4.10-1_amd64.deb"
    docker commit apache.inst.cont apache.inst
    docker rm apache.deb.cont apache.inst.cont
fi

这里我使用另一个图像(在我的caseopenldap)中安装deb作为基本图像:

Here I install the deb using another image (in my case 'openldap') as a base image:

docker run -u root -it --name=apache.inst.cont --volumes-from apache.deb.cont --entrypoint "/bin/sh" openldap -c "dpkg -i /root/deb/apache2-4_2.4.10-1_amd64.deb"
docker commit apache.inst.cont apache.inst

最后我有一个从我刚刚提交的图像开始的常规Dockerfile

FROM apache.inst:latest






PS mith 建立最小Docker图像的评论中指出对于Rails App ,请 Jari Kolehmainen

对于Ruby的应用程序,您可以轻松地删除构建所需的部分:


psmith points out in the comments to Building Minimal Docker Image for Rails App from Jari Kolehmainen.
For a ruby application, you can remove the part needed for the build easily with:

bundle install --without development test && \
apk del build-dependencies

由于需要ruby才能运行这个应用程序,在这种情况下效果很好。

Since ruby is needed to run the application anyway, that works great in this case.

我的情况,我还需要一个单独的图像来构建,如 gcc 不需要运行Apache(它是相当大的,有多个依赖关系,其中一些需要Apache在运行时,有些不是...)

I my case, I still need a separate image for building, as gcc is not needed to run Apache (and it is quite large, comes with multiple dependencies, some of them needed by Apache at runtime, some not...)

这篇关于如何在没有构建工具的情况下为Ruby项目构建Docker映像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆