Skip to content

Commit 439cf90

Browse files
committed
refactor: dockerfile based on ekidd/rust-musl-builder
1 parent 144af33 commit 439cf90

File tree

26 files changed

+789
-74
lines changed

26 files changed

+789
-74
lines changed

.drone.yml

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,9 @@
11
pipeline:
2-
# PRODUCTION
3-
image-private:
4-
image: plugins/docker
5-
group: production
6-
registry: registry.joseluisq.net
7-
repo: registry.joseluisq.net/rust-linux-darwin-builder
8-
dockerfile: ./Dockerfile
9-
username:
10-
from_secret: registry_username
11-
password:
12-
from_secret: registry_password
13-
auto_tag: true
14-
build_args:
15-
- DOCKER_IMAGE_VERSION=${DRONE_TAG}
16-
when:
17-
event: tag
18-
branch: master
19-
20-
image-public:
2+
publish:
213
image: plugins/docker
224
group: production
235
repo: joseluisq/rust-linux-darwin-builder
24-
dockerfile: ./Dockerfile
6+
dockerfile: ./docker/Dockerfile
257
username:
268
from_secret: dockerhub_username
279
password:
@@ -33,7 +15,7 @@ pipeline:
3315
event: tag
3416
branch: master
3517

36-
notification-slack:
18+
notification:
3719
image: plugins/slack
3820
webhook:
3921
from_secret: slack_webhook

.travis.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
language: rust
2+
3+
services:
4+
- docker
5+
6+
os:
7+
- linux
8+
9+
script:
10+
- bash ./test-image
11+
12+
notifications:
13+
email:
14+
on_success: never
15+
on_failure: always

Dockerfile

Lines changed: 0 additions & 52 deletions
This file was deleted.

Makefile

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
build:
2-
docker build -t joseluisq/rust-linux-darwin-builder:1.40.0 -f Dockerfile .
2+
docker build \
3+
-t joseluisq/rust-linux-darwin-builder:1 \
4+
-f Dockerfile .
35
.PHONY: build
6+
7+
release:
8+
# 2. Update docker files to latest tag
9+
./docker/version.sh $(TAG)
10+
11+
# 3. Commit and push to latest tag
12+
git add docker/Dockerfile
13+
git commit docker/Dockerfile -m "$(TAG)"
14+
git tag $(TAG)
15+
git push origin master
16+
git push origin $(TAG)
17+
.ONESHELL: release

docker/tmpl.Dockerfile

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
# NOTE: Most of Dockerfile and related files were borrowed from
2+
# https://hub.docker.com/r/ekidd/rust-musl-builder
3+
4+
# Use Ubuntu 18.04 LTS as our base image.
5+
FROM ubuntu:$UBUNTU_VERSION
6+
7+
LABEL maintainer="Jose Quintana <joseluisq.net>"
8+
9+
# The Rust toolchain to use when building our image. Set by `hooks/build`.
10+
ARG TOOLCHAIN=stable
11+
12+
# The OpenSSL version to use. We parameterize this because many Rust
13+
# projects will fail to build with 1.1.
14+
ARG OPENSSL_VERSION=1.0.2r
15+
16+
# Make sure we have basic dev tools for building C libraries. Our goal
17+
# here is to support the musl-libc builds and Cargo builds needed for a
18+
# large selection of the most popular crates.
19+
#
20+
# We also set up a `rust` user by default, in whose account we'll install
21+
# the Rust toolchain. This user has sudo privileges if you need to install
22+
# any more software.
23+
#
24+
# `mdbook` is the standard Rust tool for making searchable HTML manuals.
25+
RUN apt-get update && \
26+
apt-get install -y \
27+
build-essential \
28+
cmake \
29+
curl \
30+
file \
31+
git \
32+
musl-dev \
33+
musl-tools \
34+
libpq-dev \
35+
libsqlite-dev \
36+
libssl-dev \
37+
linux-libc-dev \
38+
pkgconf \
39+
sudo \
40+
xutils-dev \
41+
gcc-multilib-arm-linux-gnueabihf \
42+
clang \
43+
zlib1g-dev \
44+
libmpc-dev \
45+
libmpfr-dev \
46+
libgmp-dev \
47+
libxml2-dev \
48+
nano \
49+
&& \
50+
apt-get clean && rm -rf /var/lib/apt/lists/* && \
51+
useradd rust --user-group --create-home --shell /bin/bash --groups sudo && \
52+
MDBOOK_VERSION=0.2.1 && \
53+
curl -LO https://github.com/rust-lang-nursery/mdBook/releases/download/v$MDBOOK_VERSION/mdbook-v$MDBOOK_VERSION-x86_64-unknown-linux-musl.tar.gz && \
54+
tar xf mdbook-v$MDBOOK_VERSION-x86_64-unknown-linux-musl.tar.gz && \
55+
mv mdbook /usr/local/bin/ && \
56+
rm -f mdbook-v$MDBOOK_VERSION-x86_64-unknown-linux-musl.tar.gz
57+
58+
# Static linking for C++ code
59+
RUN sudo ln -s "/usr/bin/g++" "/usr/bin/musl-g++"
60+
61+
# Allow sudo without a password.
62+
ADD sudoers /etc/sudoers.d/nopasswd
63+
64+
# Run all further code as user `rust`, and create our working directories
65+
# as the appropriate user.
66+
USER rust
67+
RUN mkdir -p /home/rust/libs /home/rust/src
68+
69+
# Set up our path with all our binary directories, including those for the
70+
# musl-gcc toolchain and for our Rust toolchain.
71+
ENV PATH=/home/rust/.cargo/bin:/usr/local/musl/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
72+
73+
# Install our Rust toolchain and the `musl` target. We patch the
74+
# command-line we pass to the installer so that it won't attempt to
75+
# interact with the user or fool around with TTYs. We also set the default
76+
# `--target` to musl so that our users don't need to keep overriding it
77+
# manually.
78+
RUN curl https://sh.rustup.rs -sSf | \
79+
sh -s -- -y --default-toolchain $TOOLCHAIN && \
80+
rustup target add x86_64-unknown-linux-musl && \
81+
rustup target add armv7-unknown-linux-musleabihf && \
82+
rustup target add x86_64-apple-darwin
83+
ADD cargo-config.toml /home/rust/.cargo/config
84+
85+
# Set up a `git credentials` helper for using GH_USER and GH_TOKEN to access
86+
# private repositories if desired.
87+
ADD git-credential-ghtoken /usr/local/bin
88+
RUN git config --global credential.https://github.com.helper ghtoken
89+
90+
# Build a static library version of OpenSSL using musl-libc. This is needed by
91+
# the popular Rust `hyper` crate.
92+
#
93+
# We point /usr/local/musl/include/linux at some Linux kernel headers (not
94+
# necessarily the right ones) in an effort to compile OpenSSL 1.1's "engine"
95+
# component. It's possible that this will cause bizarre and terrible things to
96+
# happen. There may be "sanitized" header
97+
RUN echo "Building OpenSSL..." && \
98+
ls /usr/include/linux && \
99+
sudo mkdir -p /usr/local/musl/include && \
100+
sudo ln -s /usr/include/linux /usr/local/musl/include/linux && \
101+
sudo ln -s /usr/include/x86_64-linux-gnu/asm /usr/local/musl/include/asm && \
102+
sudo ln -s /usr/include/asm-generic /usr/local/musl/include/asm-generic && \
103+
cd /tmp && \
104+
curl -LO "https://www.openssl.org/source/openssl-$OPENSSL_VERSION.tar.gz" && \
105+
tar xvzf "openssl-$OPENSSL_VERSION.tar.gz" && cd "openssl-$OPENSSL_VERSION" && \
106+
env CC=musl-gcc ./Configure no-shared no-zlib -fPIC --prefix=/usr/local/musl -DOPENSSL_NO_SECURE_MEMORY linux-x86_64 && \
107+
env C_INCLUDE_PATH=/usr/local/musl/include/ make depend && \
108+
env C_INCLUDE_PATH=/usr/local/musl/include/ make && \
109+
sudo make install && \
110+
sudo rm /usr/local/musl/include/linux /usr/local/musl/include/asm /usr/local/musl/include/asm-generic && \
111+
rm -r /tmp/*
112+
113+
RUN echo "Building zlib..." && \
114+
cd /tmp && \
115+
ZLIB_VERSION=1.2.11 && \
116+
curl -LO "http://zlib.net/zlib-$ZLIB_VERSION.tar.gz" && \
117+
tar xzf "zlib-$ZLIB_VERSION.tar.gz" && cd "zlib-$ZLIB_VERSION" && \
118+
CC=musl-gcc ./configure --static --prefix=/usr/local/musl && \
119+
make && sudo make install && \
120+
rm -r /tmp/*
121+
122+
RUN echo "Building libpq..." && \
123+
cd /tmp && \
124+
POSTGRESQL_VERSION=11.2 && \
125+
curl -LO "https://ftp.postgresql.org/pub/source/v$POSTGRESQL_VERSION/postgresql-$POSTGRESQL_VERSION.tar.gz" && \
126+
tar xzf "postgresql-$POSTGRESQL_VERSION.tar.gz" && cd "postgresql-$POSTGRESQL_VERSION" && \
127+
CC=musl-gcc CPPFLAGS=-I/usr/local/musl/include LDFLAGS=-L/usr/local/musl/lib ./configure --with-openssl --without-readline --prefix=/usr/local/musl && \
128+
cd src/interfaces/libpq && make all-static-lib && sudo make install-lib-static && \
129+
cd ../../bin/pg_config && make && sudo make install && \
130+
rm -r /tmp/*
131+
132+
ENV OPENSSL_DIR=/usr/local/musl/ \
133+
OPENSSL_INCLUDE_DIR=/usr/local/musl/include/ \
134+
DEP_OPENSSL_INCLUDE=/usr/local/musl/include/ \
135+
OPENSSL_LIB_DIR=/usr/local/musl/lib/ \
136+
OPENSSL_STATIC=1 \
137+
PQ_LIB_STATIC_X86_64_UNKNOWN_LINUX_MUSL=1 \
138+
PG_CONFIG_X86_64_UNKNOWN_LINUX_GNU=/usr/bin/pg_config \
139+
PKG_CONFIG_ALLOW_CROSS=true \
140+
PKG_CONFIG_ALL_STATIC=true \
141+
LIBZ_SYS_STATIC=1 \
142+
TARGET=musl
143+
144+
# (Please feel free to submit pull requests for musl-libc builds of other C
145+
# libraries needed by the most popular and common Rust crates, to avoid
146+
# everybody needing to build them manually.)
147+
148+
ENV OSXCROSS_SDK_VERSION 10.11
149+
150+
RUN echo "Building osxcross..." && \
151+
cd /home/rust && \
152+
git clone --depth 1 https://github.com/tpoechtrager/osxcross && \
153+
cd osxcross && \
154+
curl -L -o ./tarballs/MacOSX${OSXCROSS_SDK_VERSION}.sdk.tar.xz \
155+
https://s3.amazonaws.com/andrew-osx-sdks/MacOSX${OSXCROSS_SDK_VERSION}.sdk.tar.xz && \
156+
env UNATTENDED=yes OSX_VERSION_MIN=10.7 ./build.sh && \
157+
rm -rf ./tarballs/MacOSX${OSXCROSS_SDK_VERSION}.sdk.tar.xz && \
158+
rm -rf /tmp/*
159+
160+
ENV PATH $PATH:/home/rust/osxcross/target/bin
161+
162+
# Install some useful Rust tools from source. This will use the static linking
163+
# toolchain, but that should be OK.
164+
#
165+
# We include cargo-audit for compatibility with earlier versions of this image,
166+
# but cargo-deny provides a super-set of cargo-audit's features.
167+
RUN cargo install -f cargo-audit && \
168+
cargo install -f cargo-deny && \
169+
rm -rf /home/rust/.cargo/registry/
170+
171+
# Expect our source code to live in /home/rust/src. We'll run the build as
172+
# user `rust`, which will be uid 1000, gid 1000 outside the container.
173+
WORKDIR /home/rust/src
174+
175+
CMD ["bash"]
176+
177+
# Metadata
178+
LABEL org.opencontainers.image.vendor="Jose Quintana" \
179+
org.opencontainers.image.url="registry.joseluisq.net/rust-oscross-builder" \
180+
org.opencontainers.image.title="Rust Linux / Darwin Builder" \
181+
org.opencontainers.image.description="Use same Docker image for compiling Rust programs for Linux (musl libc) & macOS (osxcross)." \
182+
org.opencontainers.image.version="$VERSION" \
183+
org.opencontainers.image.documentation="registry.joseluisq.net/rust-oscross-builder"

docker/version.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/sh
2+
3+
set -e
4+
set -u
5+
6+
LATEST_TAG=$1
7+
BASE_PATH="$(pwd)/docker"
8+
9+
if [ $# -eq 0 ]; then
10+
echo "Usage: ./version.sh <tag or branch>"
11+
exit
12+
fi
13+
14+
export VERSION=$LATEST_TAG
15+
export UBUNTU_VERSION=18.04
16+
17+
if [ ! -d "$BASE_PATH" ]; then
18+
echo "Directory no found for \"${BASE_PATH}\""
19+
exit 1
20+
fi
21+
22+
echo "Generating Dockerfile for Ubuntu Linux v$UBUNTU_VERSION x86_64"
23+
24+
rm -rf "${BASE_PATH}/Dockerfile"
25+
26+
envsubst \$UBUNTU_VERSION,\$VERSION <"${BASE_PATH}/tmpl.Dockerfile" >"${BASE_PATH}/Dockerfile"
27+
28+
echo "Dockerfile $VERSION were created successfully!"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- mode: dockerfile -*-
2+
#
3+
# An example Dockerfile showing how to add new static C libraries using
4+
# musl-gcc.
5+
6+
FROM ekidd/rust-musl-builder
7+
8+
# Build a static copy of zlib.
9+
#
10+
# EXAMPLE ONLY! libz is already included.
11+
RUN VERS=1.2.11 && \
12+
cd /home/rust/libs && \
13+
curl -LO http://zlib.net/zlib-$VERS.tar.gz && \
14+
tar xzf zlib-$VERS.tar.gz && cd zlib-$VERS && \
15+
CC=musl-gcc ./configure --static --prefix=/usr/local/musl && \
16+
make && sudo make install && \
17+
cd .. && rm -rf zlib-$VERS.tar.gz zlib-$VERS

examples/build-release

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
#
3+
# Usage: ./build-release <PROJECT> ${TRAVIS_TAG}-${TRAVIS_OS_NAME}
4+
#
5+
# The latest version of this script is available at
6+
# https://github.com/emk/rust-musl-builder/blob/master/examples/build-release
7+
#
8+
# Called by `.travis.yml` to build release binaries. We use
9+
# ekidd/rust-musl-builder to make the Linux binaries so that we can run
10+
# them unchanged on any distro, including tiny distros like Alpine (which
11+
# is heavily used for Docker containers). Other platforms get regular
12+
# binaries, which will generally be dynamically linked against libc.
13+
#
14+
# If you have a platform which supports static linking of libc, and this
15+
# would be generally useful, please feel free to submit patches.
16+
17+
set -euo pipefail
18+
19+
case `uname -s` in
20+
Linux)
21+
echo "Building static binaries using ekidd/rust-musl-builder"
22+
docker build -t build-"$1"-image .
23+
docker run -it --name build-"$1" build-"$1"-image
24+
docker cp build-"$1":/home/rust/src/target/x86_64-unknown-linux-musl/release/"$1" "$1"
25+
docker rm build-"$1"
26+
docker rmi build-"$1"-image
27+
zip "$1"-"$2".zip "$1"
28+
;;
29+
*)
30+
echo "Building standard release binaries"
31+
cargo build --release
32+
zip -j "$1"-"$2".zip target/release/"$1"
33+
;;
34+
esac

0 commit comments

Comments
 (0)