docker - 如何在 ubuntu 上构建基于 alpine 的类似 docker 镜像?

标签 docker ubuntu alpine

我正在尝试重写 Dockerfile ( https://github.com/orangefoil/rcssserver-docker/blob/master/Dockerfile ),以便它使用 alpine 而不是 ubuntu。目标是减小文件大小。
在原始图像中,robocup 足球服务器是使用 g++、flex、bison 等从头开始构建的。

FROM ubuntu:18.04 AS build
ARG VERSION=16.0.0
WORKDIR /root
RUN apt update && \
    apt -y install autoconf bison clang flex libboost-dev libboost-all-dev libc6-dev make wget
RUN wget https://github.com/rcsoccersim/rcssserver/archive/rcssserver-$VERSION.tar.gz && \
    tar xfz rcssserver-$VERSION.tar.gz && \
    cd rcssserver-rcssserver-$VERSION && \
    ./bootstrap && \
    ./configure && \
    make && \
    make install && \
    ldconfig
我试图在 alpine 上做同样的事情,不得不交换一些包裹:
FROM alpine:latest
ARG VERSION=16.0.0
WORKDIR /root
# Add basics first
RUN apk — no-cache update \
    && apk upgrade \
    && apk add autoconf bison clang-dev flex-dev boost-dev make wget automake libtool-dev g++ build-base
RUN wget https://github.com/rcsoccersim/rcssserver/archive/rcssserver-$VERSION.tar.gz
RUN tar xfz rcssserver-$VERSION.tar.gz
RUN cd rcssserver-rcssserver-$VERSION && \
    ./bootstrap && \
    ./configure && \
    make && \
    make install && \
    ldconfig
不幸的是,我的版本还不行。它失败了
/usr/lib/gcc/x86_64-alpine-linux-musl/9.3.0/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find -lrcssclangparser
从我目前发现的情况来看,如果没有安装开发包(参见 ld cannot find an existing library ),可能会发生这种情况,但是我更改为可以找到它们的开发包,但仍然没有运气。
所以,我目前的假设是 ubuntu 安装了一些包,我需要在我的 alpine 镜像中添加这些包。我会排除代码问题,因为 ubuntu 版本有效。
任何想法,可能缺少什么?我也很乐意了解如何自己比较包,但是包命名在 ubuntu 和 alpine 中并不相同,所以我发现很难弄清楚这一点。

最佳答案

您应该使用 multi-stage build 将其分解。 .在您现在正在构建的镜像中,最终镜像包含 C 工具链以及那些 -dev 的所有开发库和头文件。包安装;您不需要任何这些来实际运行构建的应用程序。基本思想是完全按照您现在的方式构建应用程序,但之后是 COPY只有将构建的应用程序放入具有较少依赖项的新镜像中。
这看起来像这样(未经测试):

FROM ubuntu:18.04 AS build
# ... exactly what's in the original question ...

FROM ubuntu:18.04

# Install the shared libraries you need to run the application,
# but not -dev headers or the full C toolchain.  You may need to
# run `ldd` on the built binary to see what exactly it needs.
RUN apt-get update \
 && DEBIAN_FRONTEND=noninteractive \
    apt-get install --assume-yes --no-install-recommends \
      libboost-atomic1.65.1 \
      libboost-chrono1.65.1 \
      # ... more libboost-* libraries as required ...

# Get the built application out of the original image.
# Autoconf's default is to install into /usr/local, and in a
# typical Docker base image nothing else will be installed there.
COPY --from=build /usr/local /usr/local
RUN ldconfig

# Describe how to run a container.
EXPOSE 12345
CMD ["/usr/local/bin/rcssserver"]
与 C 工具链、头文件和构建时库的大小相比,Alpine 和 Ubuntu 镜像之间的差异非常小,而且 Alpine 具有充分记录的库兼容性问题,其最小的 libc 实现。

关于docker - 如何在 ubuntu 上构建基于 alpine 的类似 docker 镜像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64866563/

相关文章:

C 程序在 malloc 行上崩溃 "No source available for "0xb7e88a8 1""(Eclipse)

ruby - Docker Ruby 2.6.6-alpine 是 Ruby-2.6.5-alpine 的两倍

php - 如何在 php-fpm-alpine docker 容器内运行 cron 作业?

git - docker alpine 9 构建期间的 "Host key verification failed"

docker - 从 Dockerfile 构建时图像的 rootfs 不完整

docker - Nexus 3 : "Remote Connection Pending..." for docker hub

docker-compose 主机名和本地解析

node.js - Jade : Unexpected Identifier error on Ubuntu Machine - works on Mac

java - 在 docker 实例上使用 akka 远程处理与 artere 时出错

php - 在 PHP cli 脚本中更改用户的当前目录