Add working example and test case for diesel

Fixes #27, or at least works around it.

This deals with the issues mentioned in
https://github.com/sgrif/pq-sys/pull/18, and it relies heavily on ideas
from @golddranks and @clux.
This commit is contained in:
Eric Kidd 2017-10-24 08:40:12 -04:00
parent 4ab9c3461f
commit fcc6921ddb
12 changed files with 354 additions and 66 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
target

View file

@ -19,7 +19,11 @@ RUN apt-get update && \
curl \ curl \
file \ file \
git \ git \
musl-dev \
musl-tools \ musl-tools \
libpq-dev \
libssl-dev \
pkgconf \
sudo \ sudo \
xutils-dev \ xutils-dev \
&& \ && \
@ -58,7 +62,7 @@ RUN echo "Building OpenSSL" && \
VERS=1.0.2l && \ VERS=1.0.2l && \
curl -O https://www.openssl.org/source/openssl-$VERS.tar.gz && \ curl -O https://www.openssl.org/source/openssl-$VERS.tar.gz && \
tar xvzf openssl-$VERS.tar.gz && cd openssl-$VERS && \ tar xvzf openssl-$VERS.tar.gz && cd openssl-$VERS && \
env CC=musl-gcc ./config --prefix=/usr/local/musl && \ env CC=musl-gcc ./Configure no-shared no-zlib -fPIC --prefix=/usr/local/musl linux-x86_64 && \
env C_INCLUDE_PATH=/usr/local/musl/include/ make depend && \ env C_INCLUDE_PATH=/usr/local/musl/include/ make depend && \
make && sudo make install && \ make && sudo make install && \
cd .. && rm -rf openssl-$VERS.tar.gz openssl-$VERS && \ cd .. && rm -rf openssl-$VERS.tar.gz openssl-$VERS && \
@ -76,17 +80,19 @@ RUN echo "Building OpenSSL" && \
tar xzf postgres.tar.gz && \ tar xzf postgres.tar.gz && \
cd postgresql-$VERS && \ cd postgresql-$VERS && \
CC=musl-gcc CPPFLAGS=-I/usr/local/musl/include LDFLAGS=-L/usr/local/musl/lib ./configure --with-openssl --without-readline --prefix=/usr/local/musl && \ CC=musl-gcc CPPFLAGS=-I/usr/local/musl/include LDFLAGS=-L/usr/local/musl/lib ./configure --with-openssl --without-readline --prefix=/usr/local/musl && \
cd src/interfaces/libpq && \ cd src/interfaces/libpq && make all-static-lib && sudo make install-lib-static && cd ../../.. && \
make all-static-lib && sudo make install-lib-static && \ cd src/bin/pg_config && make && sudo make install && cd ../.. && \
cd ../../../.. && rm -rf postgres.tar.gz postgresql-$VERS cd .. && rm -rf postgres.tar.gz postgresql-$VERS
ENV OPENSSL_DIR=/usr/local/musl/ \ ENV OPENSSL_DIR=/usr/local/musl/ \
OPENSSL_INCLUDE_DIR=/usr/local/musl/include/ \ OPENSSL_INCLUDE_DIR=/usr/local/musl/include/ \
DEP_OPENSSL_INCLUDE=/usr/local/musl/include/ \ DEP_OPENSSL_INCLUDE=/usr/local/musl/include/ \
OPENSSL_LIB_DIR=/usr/local/musl/lib/ \ OPENSSL_LIB_DIR=/usr/local/musl/lib/ \
OPENSSL_STATIC=1 \ OPENSSL_STATIC=1 \
PQ_LIB_DIR=/usr/local/musl/lib \ PQ_LIB_STATIC_X86_64_UNKNOWN_LINUX_MUSL=1 \
PQ_LIB_STATIC=1 PG_CONFIG_X86_64_UNKNOWN_LINUX_GNU=/usr/bin/pg_config \
PKG_CONFIG_ALLOW_CROSS=true \
PKG_CONFIG_ALL_STATIC=true
# (Please feel free to submit pull requests for musl-libc builds of other C # (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 # libraries needed by the most popular and common Rust crates, to avoid

View file

@ -2,19 +2,14 @@
[![Docker Image](https://img.shields.io/docker/pulls/ekidd/rust-musl-builder.svg?maxAge=2592000)](https://hub.docker.com/r/ekidd/rust-musl-builder/) [![Docker Image](https://img.shields.io/docker/pulls/ekidd/rust-musl-builder.svg?maxAge=2592000)](https://hub.docker.com/r/ekidd/rust-musl-builder/)
Do you want to compile a completely static Rust binary with no external Do you want to compile a completely static Rust binary with no external dependencies? If so, try:
dependencies? If so, try:
```sh ```sh
alias rust-musl-builder='docker run --rm -it -v "$(pwd)":/home/rust/src ekidd/rust-musl-builder' alias rust-musl-builder='docker run --rm -it -v "$(pwd)":/home/rust/src ekidd/rust-musl-builder'
rust-musl-builder cargo build --release rust-musl-builder cargo build --release
``` ```
This command assumes that `$(pwd)` is readable and writable by uid 1000, 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.
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 ## Deploying your Rust application
@ -22,13 +17,11 @@ With a bit of luck, you should be able to just copy your application binary from
## How it works ## How it works
`rust-musl-builder` uses [musl-libc][], [musl-gcc][], and the new `rust-musl-builder` uses [musl-libc][], [musl-gcc][], and the new [rustup][] `target` support. It includes static versions of several libraries:
[rustup][] `target` support. It includes static versions of several
libraries:
- The standard `musl-libc` libraries. - The standard `musl-libc` libraries.
- OpenSSL, which is needed by many Rust applications. - OpenSSL, which is needed by many Rust applications.
- `libpq`, which is needed for applications that use `diesel` with PostgreSQL. - `libpq`, which is needed for applications that use `diesel` with PostgreSQL. Note that this may be broken under Rust 1.21.0 and later (see https://github.com/emk/rust-musl-builder/issues/27).
- `libz`, which is needed by `libpq`. - `libz`, which is needed by `libpq`.
This library also sets up the environment variables needed to compile popular Rust crates using these libraries. This library also sets up the environment variables needed to compile popular Rust crates using these libraries.
@ -46,14 +39,28 @@ fn main() {
} }
``` ```
## Making Diesel work
In addition to setting up OpenSSL, you'll need to add the following lines to your `Cargo.toml`:
```toml
[dependencies]
# This is needed to make sure that Cargo statically links against
# `libssl`. This should happen automatically, but it doesn't.
openssl-sys = "0.9"
[patch.crates-io]
# This is needed to handle cross-compilation of libpq.
pq-sys = { git = 'https://github.com/golddranks/pq-sys' }
```
See [this PR](https://github.com/sgrif/pq-sys/pull/18) for a discussion of the issues involved in cross-compiling `diesel` and `diesel_codegen`.
## Making static releases with Travis CI and GitHub ## Making static releases with Travis CI and GitHub
These instructions are inspired by [rust-cross][]. These instructions are inspired by [rust-cross][].
First, read the [Travis CI: GitHub Releases Uploading][uploading] page, and First, read the [Travis CI: GitHub Releases Uploading][uploading] page, and run `travis setup releases` as instructed. Then add the following lines to your existing `.travis.yml` file, replacing `myapp` with the name of your package:
run `travis setup releases` as instructed. Then add the following lines to
your existing `.travis.yml` file, replacing `myapp` with the name of your
package:
```yaml ```yaml
language: rust language: rust
@ -78,13 +85,9 @@ deploy:
tags: true tags: true
``` ```
Next, copy [`build-release`](./examples/build-release) into your project Next, copy [`build-release`](./examples/build-release) into your project and run `chmod +x build-release`.
and run `chmod +x build-release`.
When you push a new tag to your project, `build-release` will automatically When you push a new tag to your project, `build-release` will automatically build new Linux binaries using `rust-musl-builder`, and new Mac binaries with Cargo, and it will upload both to the GitHub releases page for your repository.
build new Linux binaries using `rust-musl-builder`, and new Mac binaries
with Cargo, and it will upload both to the GitHub releases page for your
repository.
For a working example, see [faradayio/cage][cage]. For a working example, see [faradayio/cage][cage].
@ -94,49 +97,26 @@ For a working example, see [faradayio/cage][cage].
## Making tiny Docker images with Alpine Linux and Rust binaries ## Making tiny Docker images with Alpine Linux and Rust binaries
Docker now supports [multistage builds][multistage], which make it easy to Docker now supports [multistage builds][multistage], which make it easy to build your Rust application with `rust-musl-builder` and deploy it using [Alpine Linux][]. For a working example, see [`examples/using-diesel/Dockerfile`](./examples/using-diesel/Dockerfile).
build your Rust application with `rust-musl-builder` and deploy it using
[Alpine Linux][]. For a working example, see
[`Dockerfile.multistage-example`](./Dockerfile.multistage-example).
[multistage]: https://docs.docker.com/engine/userguide/eng-image/multistage-build/ [multistage]: https://docs.docker.com/engine/userguide/eng-image/multistage-build/
[Alpine Linux]: https://alpinelinux.org/ [Alpine Linux]: https://alpinelinux.org/
## Adding more C libraries ## Adding more C libraries
If you're using Docker crates which require specific C libraries to be 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 an example, see [`examples/adding-a-library/Dockerfile`](./examples/adding-a-library/Dockerfile). This usually involves a bit of experimentation for each new library, but it seems to work well for most simple, standalone libraries.
installed, you can create a `Dockerfile` based on this one, and use
`musl-gcc` to compile the libraries you need. For example:
```Dockerfile 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.
FROM ekidd/rust-musl-builder
# EXAMPLE ONLY! libz is already included.
RUN VERS=1.2.11 && \
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 && \
cd .. && rm -rf zlib-$VERS.tar.gz 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.
## Development notes ## Development notes
After modifying the image, run `./test-image` to make sure that everything After modifying the image, run `./test-image` to make sure that everything works.xs
works.
## Other ways to build portable Rust binaries ## Other ways to build portable Rust binaries
- [messense/rust-musl-cross](https://github.com/messense/rust-musl-cross) shows how to build binaries for many different architectures. - [messense/rust-musl-cross](https://github.com/messense/rust-musl-cross) shows how to build binaries for many different architectures.
- [japaric/rust-cross](https://github.com/japaric/rust-cross) has extensive instructions on how to cross-compile Rust applications. - [japaric/rust-cross](https://github.com/japaric/rust-cross) has extensive instructions on how to cross-compile Rust applications.
- [clux/muslrust](https://github.com/clux/muslrust) also supports libcurl.
## License ## License

View file

@ -0,0 +1 @@
target

View file

@ -0,0 +1 @@
target

233
examples/using-diesel/Cargo.lock generated Normal file
View file

@ -0,0 +1,233 @@
[root]
name = "using-diesel"
version = "0.1.0"
dependencies = [
"diesel 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)",
"diesel_codegen 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)",
"openssl-sys 0.9.20 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "aho-corasick"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "bitflags"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "byteorder"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "cc"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "derive-error-chain"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "diesel"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
"byteorder 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"pq-sys 0.4.4 (git+https://github.com/golddranks/pq-sys)",
]
[[package]]
name = "diesel_codegen"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"diesel 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)",
"diesel_infer_schema 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)",
"dotenv 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "diesel_infer_schema"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"diesel 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "dotenv"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"derive-error-chain 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
"error-chain 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "error-chain"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "lazy_static"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.2.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "memchr"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.32 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "openssl-sys"
version = "0.9.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cc 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.32 (registry+https://github.com/rust-lang/crates.io-index)",
"pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
"vcpkg 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "pkg-config"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "pq-sys"
version = "0.4.4"
source = "git+https://github.com/golddranks/pq-sys#e1d26acf6eabdd40d071d5ce8c9d52b2199960b8"
dependencies = [
"vcpkg 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "quote"
version = "0.3.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "regex"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"thread_local 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex-syntax"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "syn"
version = "0.11.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
"synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "synom"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "thread_local"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
"unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "unicode-xid"
version = "0.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "unreachable"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "utf8-ranges"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "vcpkg"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "void"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "500909c4f87a9e52355b26626d890833e9e1d53ac566db76c36faa984b889699"
"checksum bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4efd02e230a02e18f92fc2735f44597385ed02ad8f831e7c1c1156ee5e1ab3a5"
"checksum byteorder 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ff81738b726f5d099632ceaffe7fb65b90212e8dce59d518729e7e8634032d3d"
"checksum cc 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ef4019bdb99c0c1ddd56c12c2f507c174d729c6915eca6bd9d27c42f3d93b0f4"
"checksum derive-error-chain 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3c9ca9ade651388daad7c993f005d0d20c4f6fe78c1cdc93e95f161c6f5ede4a"
"checksum diesel 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)" = "304226fa7a3982b0405f6bb95dd9c10c3e2000709f194038a60ec2c277150951"
"checksum diesel_codegen 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)" = "18a42ca5c9b660add51d58bc5a50a87123380e1e458069c5504528a851ed7384"
"checksum diesel_infer_schema 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bf1957ff5cd3b04772e43c162c2f69c2aa918080ff9b020276792d236be8be52"
"checksum dotenv 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d6f0e2bb24d163428d8031d3ebd2d2bd903ad933205a97d0f18c7c1aade380f3"
"checksum error-chain 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d9435d864e017c3c6afeac1654189b06cdb491cf2ff73dbf0d73b0f292f42ff8"
"checksum lazy_static 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c9e5e58fa1a4c3b915a561a78a22ee0cac6ab97dca2504428bc1cb074375f8d5"
"checksum libc 0.2.32 (registry+https://github.com/rust-lang/crates.io-index)" = "56cce3130fd040c28df6f495c8492e5ec5808fb4c9093c310df02b0c8f030148"
"checksum memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1dbccc0e46f1ea47b9f17e6d67c5a96bd27030519c519c9c91327e31275a47b4"
"checksum openssl-sys 0.9.20 (registry+https://github.com/rust-lang/crates.io-index)" = "0ad395f1cee51b64a8d07cc8063498dc7554db62d5f3ca87a67f4eed2791d0c8"
"checksum pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "3a8b4c6b8165cd1a1cd4b9b120978131389f64bdaf456435caa41e630edba903"
"checksum pq-sys 0.4.4 (git+https://github.com/golddranks/pq-sys)" = "<none>"
"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
"checksum regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1731164734096285ec2a5ec7fea5248ae2f5485b3feeb0115af4fda2183b2d1b"
"checksum regex-syntax 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad890a5eef7953f55427c50575c680c42841653abd2b028b68cd223d157f62db"
"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6"
"checksum thread_local 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "1697c4b57aeeb7a536b647165a2825faddffb1d3bad386d507709bd51a90bb14"
"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc"
"checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56"
"checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122"
"checksum vcpkg 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9e0a7d8bed3178a8fb112199d466eeca9ed09a14ba8ad67718179b4fd5487d0b"
"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"

View file

@ -0,0 +1,12 @@
[package]
name = "using-diesel"
version = "0.1.0"
authors = ["Eric Kidd <git@randomhacks.net>"]
[dependencies]
diesel = { version = "0.16", features = ["postgres"] }
diesel_codegen = { version = "0.16", features = ["postgres"] }
openssl-sys = "*"
[patch.crates-io]
pq-sys = { git = 'https://github.com/golddranks/pq-sys' }

View file

@ -6,20 +6,19 @@
# Our first FROM statement declares the build environment. # Our first FROM statement declares the build environment.
FROM ekidd/rust-musl-builder AS builder FROM ekidd/rust-musl-builder AS builder
# You would normally add your source code to /home/rust/src like this. # Add our source code.
# ADD . ./ ADD . ./
# RUN sudo chmod -R .
# ...but we're going to just create a new app instead for demo purposes. # Fix permissions on source code.
RUN cd .. && rm -r src && USER=rust cargo new --vcs none --bin --name hello src RUN sudo chown -R rust:rust /home/rust
# Build our application. # Build our application.
RUN cargo build --release RUN cargo build --release
# Now, we need to build our _real_ Docker container, copying in `hello`. # Now, we need to build our _real_ Docker container, copying in `using-diesel`.
FROM alpine:latest FROM alpine:latest
RUN apk --no-cache add ca-certificates RUN apk --no-cache add ca-certificates
COPY --from=builder \ COPY --from=builder \
/home/rust/src/target/x86_64-unknown-linux-musl/release/hello \ /home/rust/src/target/x86_64-unknown-linux-musl/release/using-diesel \
/usr/local/bin/ /usr/local/bin/
CMD /usr/local/bin/hello CMD /usr/local/bin/using-diesel

View file

@ -0,0 +1,12 @@
version: "2.0"
services:
app:
build: .
volumes:
- cargo:/home/rust/.cargo
- target:/home/rust/src/target
volumes:
cargo: {}
target: {}

View file

@ -0,0 +1,43 @@
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate diesel_codegen;
extern crate openssl_sys;
use diesel::pg::PgConnection;
use diesel::prelude::*;
use std::env;
table! {
users (id) {
id -> Integer,
name -> Text,
}
}
#[derive(Debug, Insertable, Queryable)]
#[table_name="users"]
struct User {
id: i32,
name: String,
}
fn main() {
println!("Hello, world!");
// Only run our database example if we have a database. Otherwise, we just
// want to make sure everything links correctly.
if let Ok(url) = env::var("DATABASE_URL") {
let conn = PgConnection::establish(&url)
.expect("could not connect to site");
let rows = users::table
.limit(5)
.load::<User>(&conn)
.expect("could not load users");
for row in rows {
println!("{:?}", row);
}
} else {
println!("No DATABASE_URL set, so doing nothing")
}
}

View file

@ -7,11 +7,11 @@ set -euo pipefail
docker build -t ekidd/rust-musl-builder . docker build -t ekidd/rust-musl-builder .
# Make sure we can build our example derived container. # Make sure we can build our example derived container.
docker build -t rust-musl-zlib -f Dockerfile.example . docker build -t rust-musl-zlib examples/adding-a-library
# Make sure we can build a multi-stage container. # Make sure we can build a multi-stage container.
docker build -t rust-multistage-example -f Dockerfile.multistage-example . docker build -t rust-musl-builder-using-diesel examples/using-diesel
docker run --rm rust-multistage-example docker run --rm rust-musl-builder-using-diesel
# Make sure we can build a static executable. # Make sure we can build a static executable.
docker run --rm ekidd/rust-musl-builder bash -c " docker run --rm ekidd/rust-musl-builder bash -c "