Skip to content

Commit 3ae8179

Browse files
committed
Experimental: Improve support for USER root
This PR attempts to lay the groundwork to address #96. In particular, we install the Rust toolchain globally by abusing `rustup`. We still preserve the legacy `rust` user but we now encourage derived Dockerfiles to consider `USER root`. This PR also removes ARM support. We want to explore the idea of supporting ARM more reliably using a separate image, but perhaps it would be better to refer those users to one of the Rust cross-compilation toolchains. See #63 for discussion.
1 parent 684c04f commit 3ae8179

File tree

3 files changed

+84
-80
lines changed

3 files changed

+84
-80
lines changed

CHANGELOG.md

+15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66

77
For maximum stablity, use images with tags like `ekidd/rust-musl-builder:1.46.0` or `ekidd/rust-musl-builder:nightly-2020-08-26`. These may occasionally be rebuilt, but only while they're "current", or possibly if they're recent and serious security are discovered in a library.
88

9+
## [UNRELEASED]
10+
11+
This branch contains experimental changes intended to make it easier to support GitHub Actions.
12+
13+
### Changed
14+
15+
- You'll need to use `USER root` and `env RUSTUP_HOME=/opt/rust/rustup CARGO_HOME=/opt/rust/cargo rustup $ARGS` to install any new components.
16+
- `rustup`, `cargo`, and associated tools are all installed in `/opt/rust`, so that they should be available to the users `rust`, `root`, and any other users that get added.
17+
- Some other minor supporting tools like `git-credential-ghtoken` should now be available as `root`, as well.
18+
19+
### Removed
20+
21+
- ARM support has been removed, because it needs to be split into a separate base image. This would also allow us to build OpenSSL, etc., for ARM targets.
22+
- The `rust-docs` component is no longer installed by default.
23+
924
## 2020-09-04
1025

1126
### Added

Dockerfile

+69-54
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@ ARG CARGO_DENY_VERSION=0.7.3
2626
ARG ZLIB_VERSION=1.2.11
2727
ARG POSTGRESQL_VERSION=11.9
2828

29-
# Make sure we have basic dev tools for building C libraries. Our goal
30-
# here is to support the musl-libc builds and Cargo builds needed for a
31-
# large selection of the most popular crates.
29+
# Make sure we have basic dev tools for building C libraries. Our goal here is
30+
# to support the musl-libc builds and Cargo builds needed for a large selection
31+
# of the most popular crates.
3232
#
33-
# We also set up a `rust` user by default, in whose account we'll install
34-
# the Rust toolchain. This user has sudo privileges if you need to install
35-
# any more software.
33+
# We also set up a `rust` user by default. This user has sudo privileges if you
34+
# need to install any more software.
3635
#
3736
# `mdbook` is the standard Rust tool for making searchable HTML manuals.
3837
RUN apt-get update && \
@@ -52,7 +51,6 @@ RUN apt-get update && \
5251
pkgconf \
5352
sudo \
5453
xutils-dev \
55-
gcc-multilib-arm-linux-gnueabihf \
5654
&& \
5755
apt-get clean && rm -rf /var/lib/apt/lists/* && \
5856
useradd rust --user-group --create-home --shell /bin/bash --groups sudo && \
@@ -70,35 +68,7 @@ RUN apt-get update && \
7068
rm -rf cargo-deny-$CARGO_DENY_VERSION-x86_64-unknown-linux-musl cargo-deny-$CARGO_DENY_VERSION-x86_64-unknown-linux-musl.tar.gz
7169

7270
# Static linking for C++ code
73-
RUN sudo ln -s "/usr/bin/g++" "/usr/bin/musl-g++"
74-
75-
# Allow sudo without a password.
76-
ADD sudoers /etc/sudoers.d/nopasswd
77-
78-
# Run all further code as user `rust`, and create our working directories
79-
# as the appropriate user.
80-
USER rust
81-
RUN mkdir -p /home/rust/libs /home/rust/src
82-
83-
# Set up our path with all our binary directories, including those for the
84-
# musl-gcc toolchain and for our Rust toolchain.
85-
ENV PATH=/home/rust/.cargo/bin:/usr/local/musl/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
86-
87-
# Install our Rust toolchain and the `musl` target. We patch the
88-
# command-line we pass to the installer so that it won't attempt to
89-
# interact with the user or fool around with TTYs. We also set the default
90-
# `--target` to musl so that our users don't need to keep overriding it
91-
# manually.
92-
RUN curl https://sh.rustup.rs -sSf | \
93-
sh -s -- -y --default-toolchain $TOOLCHAIN && \
94-
rustup target add x86_64-unknown-linux-musl && \
95-
rustup target add armv7-unknown-linux-musleabihf
96-
ADD cargo-config.toml /home/rust/.cargo/config
97-
98-
# Set up a `git credentials` helper for using GH_USER and GH_TOKEN to access
99-
# private repositories if desired.
100-
ADD git-credential-ghtoken /usr/local/bin/ghtoken
101-
RUN git config --global credential.https://github.com.helper ghtoken
71+
RUN ln -s "/usr/bin/g++" "/usr/bin/musl-g++"
10272

10373
# Build a static library version of OpenSSL using musl-libc. This is needed by
10474
# the popular Rust `hyper` crate.
@@ -109,10 +79,10 @@ RUN git config --global credential.https://github.com.helper ghtoken
10979
# happen. There may be "sanitized" header
11080
RUN echo "Building OpenSSL" && \
11181
ls /usr/include/linux && \
112-
sudo mkdir -p /usr/local/musl/include && \
113-
sudo ln -s /usr/include/linux /usr/local/musl/include/linux && \
114-
sudo ln -s /usr/include/x86_64-linux-gnu/asm /usr/local/musl/include/asm && \
115-
sudo ln -s /usr/include/asm-generic /usr/local/musl/include/asm-generic && \
82+
mkdir -p /usr/local/musl/include && \
83+
ln -s /usr/include/linux /usr/local/musl/include/linux && \
84+
ln -s /usr/include/x86_64-linux-gnu/asm /usr/local/musl/include/asm && \
85+
ln -s /usr/include/asm-generic /usr/local/musl/include/asm-generic && \
11686
cd /tmp && \
11787
short_version="$(echo "$OPENSSL_VERSION" | sed s'/[a-z]$//' )" && \
11888
curl -fLO "https://www.openssl.org/source/openssl-$OPENSSL_VERSION.tar.gz" || \
@@ -121,27 +91,63 @@ RUN echo "Building OpenSSL" && \
12191
env CC=musl-gcc ./Configure no-shared no-zlib -fPIC --prefix=/usr/local/musl -DOPENSSL_NO_SECURE_MEMORY linux-x86_64 && \
12292
env C_INCLUDE_PATH=/usr/local/musl/include/ make depend && \
12393
env C_INCLUDE_PATH=/usr/local/musl/include/ make && \
124-
sudo make install && \
125-
sudo rm /usr/local/musl/include/linux /usr/local/musl/include/asm /usr/local/musl/include/asm-generic && \
94+
make install && \
95+
rm /usr/local/musl/include/linux /usr/local/musl/include/asm /usr/local/musl/include/asm-generic && \
12696
rm -r /tmp/*
12797

12898
RUN echo "Building zlib" && \
12999
cd /tmp && \
130100
curl -fLO "http://zlib.net/zlib-$ZLIB_VERSION.tar.gz" && \
131101
tar xzf "zlib-$ZLIB_VERSION.tar.gz" && cd "zlib-$ZLIB_VERSION" && \
132102
CC=musl-gcc ./configure --static --prefix=/usr/local/musl && \
133-
make && sudo make install && \
103+
make && make install && \
134104
rm -r /tmp/*
135105

136106
RUN echo "Building libpq" && \
137107
cd /tmp && \
138108
curl -fLO "https://ftp.postgresql.org/pub/source/v$POSTGRESQL_VERSION/postgresql-$POSTGRESQL_VERSION.tar.gz" && \
139109
tar xzf "postgresql-$POSTGRESQL_VERSION.tar.gz" && cd "postgresql-$POSTGRESQL_VERSION" && \
140110
CC=musl-gcc CPPFLAGS=-I/usr/local/musl/include LDFLAGS=-L/usr/local/musl/lib ./configure --with-openssl --without-readline --prefix=/usr/local/musl && \
141-
cd src/interfaces/libpq && make all-static-lib && sudo make install-lib-static && \
142-
cd ../../bin/pg_config && make && sudo make install && \
111+
cd src/interfaces/libpq && make all-static-lib && make install-lib-static && \
112+
cd ../../bin/pg_config && make && make install && \
143113
rm -r /tmp/*
144114

115+
# (Please feel free to submit pull requests for musl-libc builds of other C
116+
# libraries needed by the most popular and common Rust crates, to avoid
117+
# everybody needing to build them manually.)
118+
119+
# Install a `git credentials` helper for using GH_USER and GH_TOKEN to access
120+
# private repositories if desired. We make sure this is configured for root,
121+
# here, and for the `rust` user below.
122+
ADD git-credential-ghtoken /usr/local/bin/ghtoken
123+
RUN git config --global credential.https://github.com.helper ghtoken
124+
125+
# Set up our path with all our binary directories, including those for the
126+
# musl-gcc toolchain and for our Rust toolchain.
127+
#
128+
# We use the instructions at https://github.com/rust-lang/rustup/issues/2383
129+
# to install the rustup toolchain as root.
130+
ENV RUSTUP_HOME=/opt/rust/rustup \
131+
PATH=/home/rust/.cargo/bin:/opt/rust/cargo/bin:/usr/local/musl/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
132+
133+
# Install our Rust toolchain and the `musl` target. We patch the
134+
# command-line we pass to the installer so that it won't attempt to
135+
# interact with the user or fool around with TTYs. We also set the default
136+
# `--target` to musl so that our users don't need to keep overriding it
137+
# manually.
138+
RUN curl https://sh.rustup.rs -sSf | \
139+
env CARGO_HOME=/opt/rust/cargo \
140+
sh -s -- -y --default-toolchain $TOOLCHAIN --profile minimal --no-modify-path && \
141+
env CARGO_HOME=/opt/rust/cargo \
142+
rustup component add rustfmt && \
143+
env CARGO_HOME=/opt/rust/cargo \
144+
rustup component add clippy && \
145+
env CARGO_HOME=/opt/rust/cargo \
146+
rustup target add x86_64-unknown-linux-musl
147+
ADD cargo-config.toml /opt/rust/cargo/config
148+
149+
# Set up our environment variables so that we cross-compile using musl-libc by
150+
# default.
145151
ENV X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_DIR=/usr/local/musl/ \
146152
X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_STATIC=1 \
147153
PQ_LIB_STATIC_X86_64_UNKNOWN_LINUX_MUSL=1 \
@@ -151,19 +157,28 @@ ENV X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_DIR=/usr/local/musl/ \
151157
LIBZ_SYS_STATIC=1 \
152158
TARGET=musl
153159

154-
# (Please feel free to submit pull requests for musl-libc builds of other C
155-
# libraries needed by the most popular and common Rust crates, to avoid
156-
# everybody needing to build them manually.)
157-
158160
# Install some useful Rust tools from source. This will use the static linking
159161
# toolchain, but that should be OK.
160162
#
161163
# We include cargo-audit for compatibility with earlier versions of this image,
162-
# but cargo-deny provides a super-set of cargo-audit's features.
163-
RUN cargo install -f cargo-audit && \
164-
cargo install -f cargo-deb && \
165-
cargo install -f mdbook-graphviz && \
166-
rm -rf /home/rust/.cargo/registry/
164+
# but cargo-deny provides a superset of cargo-audit's features.
165+
RUN env CARGO_HOME=/opt/rust/cargo cargo install -f cargo-audit && \
166+
env CARGO_HOME=/opt/rust/cargo cargo install -f cargo-deb && \
167+
env CARGO_HOME=/opt/rust/cargo cargo install -f mdbook-graphviz && \
168+
rm -rf /opt/rust/cargo/registry/
169+
170+
# Allow sudo without a password.
171+
ADD sudoers /etc/sudoers.d/nopasswd
172+
173+
# Run all further code as user `rust`, create our working directories, install
174+
# our config file, and set up our credential helper.
175+
#
176+
# You should be able to switch back to `USER root` from another `Dockerfile`
177+
# using this image if you need to do so.
178+
USER rust
179+
RUN mkdir -p /home/rust/libs /home/rust/src /home/rust/.cargo && \
180+
ln -s /opt/rust/cargo/config /home/rust/.cargo/config && \
181+
git config --global credential.https://github.com.helper ghtoken
167182

168183
# Expect our source code to live in /home/rust/src. We'll run the build as
169184
# user `rust`, which will be uid 1000, gid 1000 outside the container.

test-image

-26
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,6 @@ docker run --rm rust-musl-builder-using-diesel
1515

1616
echo "==== Verifying static linking"
1717

18-
# Make sure we can build a static executable.
19-
docker run --rm ekidd/rust-musl-builder bash -c "
20-
set -euo pipefail
21-
export USER=rust
22-
cargo new --vcs none --bin testme
23-
cd testme
24-
25-
echo -e '--- Test case for x86_64:'
26-
cargo build
27-
echo 'ldd says:'
28-
if ldd target/x86_64-unknown-linux-musl/debug/testme; then
29-
echo '[FAIL] Executable is not static!' 1>&2
30-
exit 1
31-
fi
32-
echo -e '[PASS] x86_64 binary is statically linked.\n'
33-
34-
echo -e '--- Test case for ARMhf:'
35-
cargo build --target=armv7-unknown-linux-musleabihf
36-
echo 'ldd says:'
37-
if ldd target/armv7-unknown-linux-musleabihf/debug/testme; then
38-
echo '[FAIL] Executable is not static!' 1>&2
39-
exit 1
40-
fi
41-
echo -e '[PASS] ARMhf binary is statically linked.\n'
42-
"
43-
4418
# Make sure we can build a static executable using `sqlx`.
4519
docker build -t rust-musl-builder-using-sqlx examples/using-sqlx
4620
docker run --rm rust-musl-builder-using-sqlx sh -c "

0 commit comments

Comments
 (0)