Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add musl-dev to alpine images #68

Closed
mamins1376 opened this issue Dec 18, 2020 · 6 comments
Closed

Add musl-dev to alpine images #68

mamins1376 opened this issue Dec 18, 2020 · 6 comments

Comments

@mamins1376
Copy link

Currently when building something that involves compiling C (bat as an example), at the linking stage the following happens, due to missing /usr/lib/crti.o:

   ...
   Compiling clap v2.33.3
   Compiling chrono v0.4.19
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" ...  "-Wl,-Bdynamic" "-lgcc_s" "-lc"
  = note: /usr/lib/gcc/x86_64-alpine-linux-musl/9.3.0/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find crti.o: No such file or directory
          collect2: error: ld returned 1 exit status

error: aborting due to previous error

This can be eliminated simply by adding musl-dev after RUN apk add ....

@sfackler
Copy link
Member

The official alpine images tend to be as small as possible - take go's for example: https://hub.docker.com/_/golang which doesn't even include gcc.

@mamins1376
Copy link
Author

What's the point of including gcc when there are not even std headers? Is it a common scenario to compile no_std-equivalent C/C++ code?

@sfackler
Copy link
Member

rustc uses gcc as its linker.

@mamins1376
Copy link
Author

Ok, got it. I guess RUN apk add --no-cache musl-dev should always be the first line of some Dockerfiles.
Not a big deal though, the trade-off seems appropriate. Thanks for your response.

@wrenix
Copy link

wrenix commented May 20, 2023

Why is that not default in the rust-alpine container preinstalled?

@polarathene
Copy link

Given the discussion at rust-lang/compiler-team#422

Should that change land, the user will fail to build a simple "hello world" program when dynamic linking becomes default with musl targets.

They'd either need to know to specify -C link-self-contained=yes as a rust flag for cargo (additionally with -C target-feature=+crt-static if they want to get static builds back), or to install musl-dev in the container.

Documenting that on DockerHub would be the better route if the alpine image is meant to prioritize size over UX (less of an issue until the image toolchain defaults to dynamic linking), but only if the size increase was notable..


musl-dev adds 11MB uncompressed to the existing 825MB image (1.33% image weight increase)

FROM rust:alpine
RUN apk add --no-cache musl-dev
$ docker build -t rust-musl-dev .

# Barely an increase:
$ docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
rust-musl-dev         latest    0ae623d62975   7 seconds ago   836MB
rust                  alpine    5d20fa327476   2 days ago      825MB
rust                  latest    890a6b209e1c   2 weeks ago     1.5GB
golang                alpine    bb520cee46ae   11 days ago     222MB
golang                latest    4c88d2e04e7d   11 days ago     815MB

$ docker image history rust-musl-dev
IMAGE          CREATED         CREATED BY                                      SIZE      COMMENT
0ae623d62975   2 minutes ago   RUN /bin/sh -c apk add --no-cache musl-dev #…   10.3MB    buildkit.dockerfile.v0
<missing>      2 days ago      /bin/sh -c set -eux;     apkArch="$(apk --pr…   680MB
<missing>      2 days ago      /bin/sh -c #(nop)  ENV RUSTUP_HOME=/usr/loca…   0B
<missing>      4 weeks ago     /bin/sh -c apk add --no-cache         ca-cer…   138MB
<missing>      7 weeks ago     /bin/sh -c #(nop)  CMD ["/bin/sh"]              0B
<missing>      7 weeks ago     /bin/sh -c #(nop) ADD file:756183bba9c7f4593…   7.34MB

Here are those two layers 680MB + 138MB (via dive):

image

image

The small addition of musl-dev for improved UX:

image

We're looking at a modest 1.33% increase in image weight, not a deal breaker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants