Skip to content

Commit 1b7948d

Browse files
committed
Use ubuntu 20.04
Also updates various dependencies and tools. Further changes (squashed commits): * Use COPY over ADD If you copy local files, then COPY is preferred. ADD should only be used in specifc circumstances like fetching files from URLs or unpacking archives. * Use a more reliable testing for ldd * Use openssl 1.1.1m, move cargo tools
1 parent 5911b88 commit 1b7948d

File tree

4 files changed

+79
-54
lines changed

4 files changed

+79
-54
lines changed

Dockerfile

Lines changed: 61 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
# Use Ubuntu 18.04 LTS as our base image.
2-
FROM ubuntu:18.04
1+
# syntax=docker/dockerfile:1
2+
3+
# Use Ubuntu 20.04 LTS as our base image.
4+
FROM ubuntu:20.04
35

46
# The Rust toolchain to use when building our image. Set by `hooks/build`.
57
ARG TOOLCHAIN=stable
@@ -10,6 +12,10 @@ ARG TOOLCHAIN=stable
1012
#
1113
# ALSO UPDATE hooks/build!
1214
ARG OPENSSL_VERSION=1.1.1m
15+
# Needs evaluation;
16+
# fails for libpq with:
17+
# "configure: error: library 'crypto' is required for OpenSSL"
18+
# ARG OPENSSL_VERSION=3.0.0
1319

1420
# Versions for other dependencies. Here are the places to check for new
1521
# releases:
@@ -38,8 +44,8 @@ ARG POSTGRESQL_VERSION=11.14
3844
#
3945
# We also set up a `rust` user by default. This user has sudo privileges if you
4046
# need to install any more software.
41-
RUN apt-get update && \
42-
export DEBIAN_FRONTEND=noninteractive && \
47+
RUN export DEBIAN_FRONTEND=noninteractive && \
48+
apt-get update && \
4349
apt-get install -yq \
4450
build-essential \
4551
cmake \
@@ -61,32 +67,6 @@ RUN apt-get update && \
6167
apt-get clean && rm -rf /var/lib/apt/lists/* && \
6268
useradd rust --user-group --create-home --shell /bin/bash --groups sudo
6369

64-
# - `mdbook` is the standard Rust tool for making searchable HTML manuals.
65-
# - `mdbook-graphviz` allows using inline GraphViz drawing commands to add illustrations.
66-
# - `cargo-about` generates a giant license file for all dependencies.
67-
# - `cargo-audit` checks for security vulnerabilities. We include it for backwards compat.
68-
# - `cargo-deny` does everything `cargo-audit` does, plus check licenses & many other things.
69-
RUN curl -fLO https://github.com/rust-lang-nursery/mdBook/releases/download/v$MDBOOK_VERSION/mdbook-v$MDBOOK_VERSION-x86_64-unknown-linux-gnu.tar.gz && \
70-
tar xf mdbook-v$MDBOOK_VERSION-x86_64-unknown-linux-gnu.tar.gz && \
71-
mv mdbook /usr/local/bin/ && \
72-
rm -f mdbook-v$MDBOOK_VERSION-x86_64-unknown-linux-gnu.tar.gz && \
73-
curl -fLO https://github.com/dylanowen/mdbook-graphviz/releases/download/v$MDBOOK_GRAPHVIZ_VERSION/mdbook-graphviz_v${MDBOOK_GRAPHVIZ_VERSION}_x86_64-unknown-linux-musl.zip && \
74-
unzip mdbook-graphviz_v${MDBOOK_GRAPHVIZ_VERSION}_x86_64-unknown-linux-musl.zip && \
75-
mv mdbook-graphviz /usr/local/bin/ && \
76-
rm -f mdbook-graphviz_v${MDBOOK_GRAPHVIZ_VERSION}_x86_64-unknown-linux-musl.zip && \
77-
curl -fLO https://github.com/EmbarkStudios/cargo-about/releases/download/$CARGO_ABOUT_VERSION/cargo-about-$CARGO_ABOUT_VERSION-x86_64-unknown-linux-musl.tar.gz && \
78-
tar xf cargo-about-$CARGO_ABOUT_VERSION-x86_64-unknown-linux-musl.tar.gz && \
79-
mv cargo-about-$CARGO_ABOUT_VERSION-x86_64-unknown-linux-musl/cargo-about /usr/local/bin/ && \
80-
rm -rf cargo-about-$CARGO_ABOUT_VERSION-x86_64-unknown-linux-musl.tar.gz cargo-about-$CARGO_ABOUT_VERSION-x86_64-unknown-linux-musl && \
81-
curl -fLO https://github.com/rustsec/rustsec/releases/download/cargo-audit%2Fv${CARGO_AUDIT_VERSION}/cargo-audit-x86_64-unknown-linux-gnu-v${CARGO_AUDIT_VERSION}.tgz && \
82-
tar xf cargo-audit-x86_64-unknown-linux-gnu-v${CARGO_AUDIT_VERSION}.tgz && \
83-
cp cargo-audit-x86_64-unknown-linux-gnu-v${CARGO_AUDIT_VERSION}/cargo-audit /usr/local/bin/ && \
84-
rm -rf cargo-audit-x86_64-unknown-linux-gnu-v${CARGO_AUDIT_VERSION}.tgz cargo-audit-x86_64-unknown-linux-gnu-v${CARGO_AUDIT_VERSION} && \
85-
curl -fLO https://github.com/EmbarkStudios/cargo-deny/releases/download/$CARGO_DENY_VERSION/cargo-deny-$CARGO_DENY_VERSION-x86_64-unknown-linux-musl.tar.gz && \
86-
tar xf cargo-deny-$CARGO_DENY_VERSION-x86_64-unknown-linux-musl.tar.gz && \
87-
mv cargo-deny-$CARGO_DENY_VERSION-x86_64-unknown-linux-musl/cargo-deny /usr/local/bin/ && \
88-
rm -rf cargo-deny-$CARGO_DENY_VERSION-x86_64-unknown-linux-musl cargo-deny-$CARGO_DENY_VERSION-x86_64-unknown-linux-musl.tar.gz
89-
9070
# Static linking for C++ code
9171
RUN ln -s "/usr/bin/g++" "/usr/bin/musl-g++"
9272

@@ -97,36 +77,36 @@ RUN ln -s "/usr/bin/g++" "/usr/bin/musl-g++"
9777
# necessarily the right ones) in an effort to compile OpenSSL 1.1's "engine"
9878
# component. It's possible that this will cause bizarre and terrible things to
9979
# happen. There may be "sanitized" header
100-
RUN echo "Building OpenSSL" && \
80+
RUN echo "Building OpenSSL ${OPENSSL_VERSION}" && \
10181
ls /usr/include/linux && \
10282
mkdir -p /usr/local/musl/include && \
10383
ln -s /usr/include/linux /usr/local/musl/include/linux && \
10484
ln -s /usr/include/x86_64-linux-gnu/asm /usr/local/musl/include/asm && \
10585
ln -s /usr/include/asm-generic /usr/local/musl/include/asm-generic && \
10686
cd /tmp && \
107-
short_version="$(echo "$OPENSSL_VERSION" | sed s'/[a-z]$//' )" && \
108-
curl -fLO "https://www.openssl.org/source/openssl-$OPENSSL_VERSION.tar.gz" || \
87+
short_version="$(echo "${OPENSSL_VERSION}" | sed s'/[a-z]$//' )" && \
88+
curl -fLO "https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz" || \
10989
curl -fLO "https://www.openssl.org/source/old/$short_version/openssl-$OPENSSL_VERSION.tar.gz" && \
110-
tar xvzf "openssl-$OPENSSL_VERSION.tar.gz" && cd "openssl-$OPENSSL_VERSION" && \
90+
tar xvzf "openssl-${OPENSSL_VERSION}.tar.gz" && cd "openssl-${OPENSSL_VERSION}" && \
11191
env CC=musl-gcc ./Configure no-shared no-zlib -fPIC --prefix=/usr/local/musl -DOPENSSL_NO_SECURE_MEMORY linux-x86_64 && \
11292
env C_INCLUDE_PATH=/usr/local/musl/include/ make depend && \
11393
env C_INCLUDE_PATH=/usr/local/musl/include/ make && \
11494
make install && \
11595
rm /usr/local/musl/include/linux /usr/local/musl/include/asm /usr/local/musl/include/asm-generic && \
11696
rm -r /tmp/*
11797

118-
RUN echo "Building zlib" && \
98+
RUN echo "Building zlib ${ZLIB_VERSION}" && \
11999
cd /tmp && \
120-
curl -fLO "http://zlib.net/zlib-$ZLIB_VERSION.tar.gz" && \
121-
tar xzf "zlib-$ZLIB_VERSION.tar.gz" && cd "zlib-$ZLIB_VERSION" && \
100+
curl -fLO "http://zlib.net/zlib-${ZLIB_VERSION}.tar.gz" && \
101+
tar xzf "zlib-${ZLIB_VERSION}.tar.gz" && cd "zlib-${ZLIB_VERSION}" && \
122102
CC=musl-gcc ./configure --static --prefix=/usr/local/musl && \
123103
make && make install && \
124104
rm -r /tmp/*
125105

126-
RUN echo "Building libpq" && \
106+
RUN echo "Building libpq ${POSTGRESQL_VERSION}" && \
127107
cd /tmp && \
128-
curl -fLO "https://ftp.postgresql.org/pub/source/v$POSTGRESQL_VERSION/postgresql-$POSTGRESQL_VERSION.tar.gz" && \
129-
tar xzf "postgresql-$POSTGRESQL_VERSION.tar.gz" && cd "postgresql-$POSTGRESQL_VERSION" && \
108+
curl -fLO "https://ftp.postgresql.org/pub/source/v${POSTGRESQL_VERSION}/postgresql-${POSTGRESQL_VERSION}.tar.gz" && \
109+
tar xzf "postgresql-${POSTGRESQL_VERSION}.tar.gz" && cd "postgresql-${POSTGRESQL_VERSION}" && \
130110
CC=musl-gcc CPPFLAGS=-I/usr/local/musl/include LDFLAGS=-L/usr/local/musl/lib ./configure --with-openssl --without-readline --prefix=/usr/local/musl && \
131111
cd src/interfaces/libpq && make all-static-lib && make install-lib-static && \
132112
cd ../../bin/pg_config && make && make install && \
@@ -139,7 +119,7 @@ RUN echo "Building libpq" && \
139119
# Install a `git credentials` helper for using GH_USER and GH_TOKEN to access
140120
# private repositories if desired. We make sure this is configured for root,
141121
# here, and for the `rust` user below.
142-
ADD git-credential-ghtoken /usr/local/bin/ghtoken
122+
COPY git-credential-ghtoken /usr/local/bin/ghtoken
143123
RUN git config --global credential.https://github.com.helper ghtoken
144124

145125
# Set up our path with all our binary directories, including those for the
@@ -155,16 +135,23 @@ ENV RUSTUP_HOME=/opt/rust/rustup \
155135
# interact with the user or fool around with TTYs. We also set the default
156136
# `--target` to musl so that our users don't need to keep overriding it
157137
# manually.
158-
RUN curl https://sh.rustup.rs -sSf | \
138+
RUN echo "Installing Rust (${TOOLCHAIN} toolchain)" && \
139+
curl https://sh.rustup.rs -sSf | \
159140
env CARGO_HOME=/opt/rust/cargo \
160-
sh -s -- -y --default-toolchain $TOOLCHAIN --profile minimal --no-modify-path && \
141+
sh -s -- -y --default-toolchain ${TOOLCHAIN} --profile minimal --no-modify-path && \
161142
env CARGO_HOME=/opt/rust/cargo \
162143
rustup component add rustfmt && \
163144
env CARGO_HOME=/opt/rust/cargo \
164145
rustup component add clippy && \
165146
env CARGO_HOME=/opt/rust/cargo \
166147
rustup target add x86_64-unknown-linux-musl
167-
ADD cargo-config.toml /opt/rust/cargo/config
148+
# if we also install other target's build dependencies (like tools, libs, and linkers)
149+
# rustup target add x86_64-unknown-linux-musl \
150+
# rustup target add aarch64-unknown-linux-musl \
151+
# rustup target add armv7-unknown-linux-musleabihf \
152+
# rustup target add armv7-unknown-linux-musleabi
153+
154+
COPY cargo-config.toml /opt/rust/cargo/config
168155

169156
# Set up our environment variables so that we cross-compile using musl-libc by
170157
# default.
@@ -181,12 +168,38 @@ ENV X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_DIR=/usr/local/musl/ \
181168
# slow down image builds). This will use the static linking toolchain, but that
182169
# should be OK.
183170
#
184-
# - `cargo-deb` builds Debian packages.
185-
RUN env CARGO_HOME=/opt/rust/cargo cargo install -f cargo-deb && \
186-
rm -rf /opt/rust/cargo/registry/
171+
# - `cargo-deb` is a helper command which automatically creates binary Debian packages (.deb) from Cargo projects.
172+
# - `mdbook` is the standard Rust tool for making searchable HTML manuals.
173+
# - `mdbook-graphviz` allows using inline GraphViz drawing commands to add illustrations.
174+
# - `cargo-about` generates a giant license file for all dependencies.
175+
# - `cargo-audit` checks for security vulnerabilities. We include it for backwards compat.
176+
# - `cargo-deny` does everything `cargo-audit` does, plus check licenses & many other things.
177+
RUN echo "Installing cargo tools" && \
178+
env CARGO_HOME=/opt/rust/cargo cargo install -f cargo-deb && \
179+
rm -rf /opt/rust/cargo/registry/ && \
180+
curl -fLO https://github.com/rust-lang-nursery/mdBook/releases/download/v${MDBOOK_VERSION}/mdbook-v${MDBOOK_VERSION}-x86_64-unknown-linux-gnu.tar.gz && \
181+
tar xf mdbook-v${MDBOOK_VERSION}-x86_64-unknown-linux-gnu.tar.gz && \
182+
mv mdbook /usr/local/bin/ && \
183+
rm -f mdbook-v${MDBOOK_VERSION}-x86_64-unknown-linux-gnu.tar.gz && \
184+
curl -fLO https://github.com/dylanowen/mdbook-graphviz/releases/download/v${MDBOOK_GRAPHVIZ_VERSION}/mdbook-graphviz_v${MDBOOK_GRAPHVIZ_VERSION}_x86_64-unknown-linux-musl.zip && \
185+
unzip mdbook-graphviz_v${MDBOOK_GRAPHVIZ_VERSION}_x86_64-unknown-linux-musl.zip && \
186+
mv mdbook-graphviz /usr/local/bin/ && \
187+
rm -f mdbook-graphviz_v${MDBOOK_GRAPHVIZ_VERSION}_x86_64-unknown-linux-musl.zip && \
188+
curl -fLO https://github.com/EmbarkStudios/cargo-about/releases/download/${CARGO_ABOUT_VERSION}/cargo-about-${CARGO_ABOUT_VERSION}-x86_64-unknown-linux-musl.tar.gz && \
189+
tar xf cargo-about-${CARGO_ABOUT_VERSION}-x86_64-unknown-linux-musl.tar.gz && \
190+
mv cargo-about-${CARGO_ABOUT_VERSION}-x86_64-unknown-linux-musl/cargo-about /usr/local/bin/ && \
191+
rm -rf cargo-about-${CARGO_ABOUT_VERSION}-x86_64-unknown-linux-musl.tar.gz cargo-about-${CARGO_ABOUT_VERSION}-x86_64-unknown-linux-musl && \
192+
curl -fLO https://github.com/rustsec/rustsec/releases/download/cargo-audit%2Fv${CARGO_AUDIT_VERSION}/cargo-audit-x86_64-unknown-linux-gnu-v${CARGO_AUDIT_VERSION}.tgz && \
193+
tar xf cargo-audit-x86_64-unknown-linux-gnu-v${CARGO_AUDIT_VERSION}.tgz && \
194+
cp cargo-audit-x86_64-unknown-linux-gnu-v${CARGO_AUDIT_VERSION}/cargo-audit /usr/local/bin/ && \
195+
rm -rf cargo-audit-x86_64-unknown-linux-gnu-v${CARGO_AUDIT_VERSION}.tgz cargo-audit-x86_64-unknown-linux-gnu-v${CARGO_AUDIT_VERSION} && \
196+
curl -fLO https://github.com/EmbarkStudios/cargo-deny/releases/download/${CARGO_DENY_VERSION}/cargo-deny-${CARGO_DENY_VERSION}-x86_64-unknown-linux-musl.tar.gz && \
197+
tar xf cargo-deny-${CARGO_DENY_VERSION}-x86_64-unknown-linux-musl.tar.gz && \
198+
mv cargo-deny-${CARGO_DENY_VERSION}-x86_64-unknown-linux-musl/cargo-deny /usr/local/bin/ && \
199+
rm -rf cargo-deny-${CARGO_DENY_VERSION}-x86_64-unknown-linux-musl cargo-deny-${CARGO_DENY_VERSION}-x86_64-unknown-linux-musl.tar.gz
187200

188201
# Allow sudo without a password.
189-
ADD sudoers /etc/sudoers.d/nopasswd
202+
COPY sudoers /etc/sudoers.d/nopasswd
190203

191204
# Run all further code as user `rust`, create our working directories, install
192205
# our config file, and set up our credential helper.

examples/linking-with-git2/Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,11 @@ ADD --chown=rust:rust . ./
1111

1212
# Build our application.
1313
RUN cargo build
14+
15+
# Now, we need to build our _real_ Docker container, copying in `linking-with-git2`.
16+
FROM debian:stretch-slim
17+
RUN apt-get update && apt-get install -y ca-certificates
18+
COPY --from=builder \
19+
/home/rust/src/target/x86_64-unknown-linux-musl/debug/linking-with-git2 \
20+
/usr/local/bin/
21+
CMD /usr/local/bin/linking-with-git2

examples/using-sqlx/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ ADD --chown=rust:rust . ./
1717
RUN cargo build --release
1818

1919
# Now, we need to build our _real_ Docker container, copying in `using-sqlx`.
20-
FROM alpine:latest
21-
RUN apk --no-cache add ca-certificates
20+
FROM debian:stretch-slim
21+
RUN apt-get update && apt-get install -y ca-certificates
2222
COPY --from=builder \
2323
/home/rust/src/target/x86_64-unknown-linux-musl/release/using-sqlx \
2424
/usr/local/bin/

test-image

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ echo "==== Verifying static linking"
1717

1818
# Make sure we can build a static executable using `sqlx`.
1919
docker build -t rust-musl-builder-using-sqlx examples/using-sqlx
20-
docker run --rm rust-musl-builder-using-sqlx sh -c "
20+
docker run --rm rust-musl-builder-using-sqlx bash -c "
2121
set -euo pipefail
2222
2323
echo -e '--- Test case for sqlx:'
2424
echo 'ldd says:'
25-
if ldd /usr/local/bin/using-sqlx; then
25+
lddresult=\$(ldd /usr/local/bin/using-sqlx)
26+
echo \$lddresult
27+
if [[ "\$lddresult" != *statically\ linked* ]]; then
2628
echo '[FAIL] Executable is not static!' 1>&2
2729
exit 1
2830
fi
@@ -33,14 +35,16 @@ echo -e '[PASS] using-sqlx binary is statically linked.\n'
3335
docker build -t rust-musl-builder-linking-with-git2 examples/linking-with-git2
3436
docker run --rm rust-musl-builder-linking-with-git2 bash -c "
3537
set -euo pipefail
36-
cd /home/rust/src
3738
3839
echo -e '--- Test case for libgit2:'
3940
echo 'ldd says:'
40-
if ldd target/x86_64-unknown-linux-musl/debug/linking-with-git2; then
41+
lddresult=\$(ldd /usr/local/bin/linking-with-git2)
42+
echo \$lddresult
43+
if [[ "\$lddresult" != *statically\ linked* ]]; then
4144
echo '[FAIL] Executable is not static!' 1>&2
4245
exit 1
4346
fi
47+
4448
echo -e '[PASS] libgit2 binary is statically linked.\n'
4549
"
4650

0 commit comments

Comments
 (0)