commit 0d3bf73856176407329cfe8f372e6c71568da0be Author: Eric Kidd Date: Sat Apr 16 10:00:12 2016 -0400 Create initial Dockerfile and README.md This seems to work fine locally. diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d1e28c7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,61 @@ +# Use Debian 8.0 "Jessie" as the base for our Rust musl toolchain. +FROM debian:jessie + +# Make sure we have basic dev tools for building C libraries, and any +# shared library dev packages we'll need, and create a `rust` user in +# whose home directory we'll install the Rust compilers. +# +# Our goal here is to support the musl-libc builds and Cargo builds needed +# for a large selection of the most popular crates. +# +# We also set up a `rust` user by default, in whose account we'll install +# the Rust toolchain. This user has sudo privileges if you need to install +# any more software. +RUN apt-get update && \ + apt-get install -y build-essential sudo git curl file xutils-dev cmake && \ + apt-get clean && rm -rf /var/lib/apt/lists/* && \ + useradd rust --user-group --create-home --shell /bin/bash --groups sudo && \ + echo "%sudo ALL=(ALL:ALL) NOPASSWD:ALL" >> /etc/sudoers + +# Set up our path with all the binary directories we're going to create, +# including those for the musl-gcc toolchain and for our Rust toolchain. +ENV PATH=/home/rust/.cargo/bin:/usr/local/musl/bin:/usr/local/bin:/usr/bin:/bin + +# Build the musl-libc toolchain, which installs itself in /usr/local/musl. +WORKDIR /musl +RUN git clone git://git.musl-libc.org/musl && cd musl && \ + ./configure && make install + +# Build a static library version of OpenSSL using musl-libc. This is +# needed by the popular Rust `hyper` crate. +RUN VERS=1.0.2g && \ + curl -O https://www.openssl.org/source/openssl-$VERS.tar.gz && \ + tar xvzf openssl-$VERS.tar.gz && cd openssl-$VERS && \ + env CC=musl-gcc ./config --prefix=/usr/local/musl && \ + make depend && make && make install +ENV OPENSSL_INCLUDE_DIR=/usr/local/musl/include/ \ + OPENSSL_LIB_DIR=/usr/local/musl/lib/ \ + OPENSSL_STATIC=1 + +# (Please feel free to submit pull requests for musl-libc builds of other C +# libraries needed by the most popular and common Rust crates, to avoid +# everybody needing to build them manually.) + +# Delete our musl-libc build directory. +RUN rm -rf /musl + +# Mount the source code we want to build on /home/rust/src. We do this as +# user `rust`, which will be uid 1000, gid 1000 outside the container. +VOLUME ["/home/rust/src"] +USER rust +WORKDIR /home/rust/src + +# Install our Rust toolchain and the `musl` target. We patch the +# command-line we pass to the installer so that it won't attempt to +# interact with the user or fool around with TTYs. We also set the default +# `--target` to musl so that our users don't need to keep overriding it +# manually. +RUN curl https://sh.rustup.rs -sSf | sed 's,run "$_file" < /dev/tty,run "$_file" -y,' | sh && \ + rustup default stable && \ + rustup target add x86_64-unknown-linux-musl && \ + echo "[build]\ntarget = \"x86_64-unknown-linux-musl\"\n" >> /home/rust/.cargo/config diff --git a/Dockerfile.example b/Dockerfile.example new file mode 100644 index 0000000..2e31016 --- /dev/null +++ b/Dockerfile.example @@ -0,0 +1,15 @@ +# -*- mode: dockerfile -*- +# +# An example Dockerfile showing how to add new static C libraries using +# musl-gcc. + +FROM ekidd/rust-musl-builder:latest + +# Build a static copy of zlib. +RUN VERS=1.2.8 && \ + mkdir -p /home/rust/libs && cd /home/rust/libs && \ + curl -LO http://zlib.net/zlib-$VERS.tar.gz && \ + tar xzf zlib-$VERS.tar.gz && cd zlib-$VERS && \ + CC=musl-gcc ./configure --static --prefix=/usr/local/musl && \ + make && sudo make install && \ + rm -rf /home/rust/libs/zlib-$VERS.tar.gz /home/rust/libs/zlib-$VERS diff --git a/README.md b/README.md new file mode 100644 index 0000000..ca1d559 --- /dev/null +++ b/README.md @@ -0,0 +1,62 @@ +# `rust-musl-builder`: Docker container for easily building static Rust binaries + +Do you want to compile a completely static Rust binary with no external +dependencies? If so, try: + +```sh +alias rust-musl-builder='docker run --rm -it -v "$(pwd)":/home/rust/src ekidd/rust-musl-builder' +rust-musl-builder cargo build --rebase +``` + +This command assumes that `$(pwd)` is readable and writable by uid 1000, +gid 1000. It will output binaries in +`target/x86_64-unknown-linux-musl/release`. At the moment, it doesn't +attempt to cache libraries between builds, so this is best reserved for +making final release builds. + +## Deploying your Rust application + +With a bit of luck, you should be able to just copy your application binary +from `target/x86_64-unknown-linux-musl/release`, and install it directly on +any reasonably modern x86_64 Linux machine. In particular, you should be +able to copy your Rust application into an +[Alpine Linux container][]. + +## How it works + +`rust-musl-builder` uses [musl-libc][], [musl-gcc][], and the new +[rustup][] `target` support. It includes static versions of several +libraries: + +- The standard `musl-libc` libraries. +- OpenSSL, which is needed by many Rust applications. + +## Adding more C libraries + +If you're using Docker crates which require specific C libraries to be +installed, you can create a Dockerfile based on this one, and use +`musl-gcc` to compile the libraries you need. For example: + +```Dockerfile +FROM ekidd/rust-musl-builder:latest + +RUN VERS=1.2.8 && \ + mkdir -p /home/rust/libs && cd /home/rust/libs && \ + curl -LO http://zlib.net/zlib-$VERS.tar.gz && \ + tar xzf zlib-$VERS.tar.gz && cd zlib-$VERS && \ + CC=musl-gcc ./configure --static --prefix=/usr/local/musl && \ + make && sudo make install && \ + rm -rf /home/rust/libs/zlib-$VERS.tar.gz /home/rust/libs/zlib-$VERS +``` + +This usually involves a bit of experimentation for each new library, but it +seems to work well for most simple, standalone libraries. + +If you need an especially common library, please feel free to submit a pull +request adding it to the main `Dockerfile`! We'd like to support popular +Rust crates out of the box. + +[Alpine Linux container]: https://hub.docker.com/_/alpine/ +[musl-libc]: http://www.musl-libc.org/ +[musl-gcc]: http://www.musl-libc.org/how.html +[rustup]: https://www.rustup.rs/