From fcc6921ddb8b38a592d73c611395bd92a2c68cfb Mon Sep 17 00:00:00 2001 From: Eric Kidd Date: Tue, 24 Oct 2017 08:40:12 -0400 Subject: [PATCH] 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. --- .gitignore | 1 + Dockerfile | 18 +- README.md | 78 +++--- examples/adding-a-library/.dockerignore | 1 + .../adding-a-library/Dockerfile | 0 examples/using-diesel/.dockerignore | 1 + examples/using-diesel/Cargo.lock | 233 ++++++++++++++++++ examples/using-diesel/Cargo.toml | 12 + .../using-diesel/Dockerfile | 15 +- examples/using-diesel/docker-compose.yml | 12 + examples/using-diesel/src/main.rs | 43 ++++ test-image | 6 +- 12 files changed, 354 insertions(+), 66 deletions(-) create mode 100644 .gitignore create mode 100644 examples/adding-a-library/.dockerignore rename Dockerfile.example => examples/adding-a-library/Dockerfile (100%) create mode 100644 examples/using-diesel/.dockerignore create mode 100644 examples/using-diesel/Cargo.lock create mode 100644 examples/using-diesel/Cargo.toml rename Dockerfile.multistage-example => examples/using-diesel/Dockerfile (57%) create mode 100644 examples/using-diesel/docker-compose.yml create mode 100644 examples/using-diesel/src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target diff --git a/Dockerfile b/Dockerfile index d181016..0b1ec00 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,7 +19,11 @@ RUN apt-get update && \ curl \ file \ git \ + musl-dev \ musl-tools \ + libpq-dev \ + libssl-dev \ + pkgconf \ sudo \ xutils-dev \ && \ @@ -58,7 +62,7 @@ RUN echo "Building OpenSSL" && \ VERS=1.0.2l && \ 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 && \ + 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 && \ make && sudo make install && \ cd .. && rm -rf openssl-$VERS.tar.gz openssl-$VERS && \ @@ -76,17 +80,19 @@ RUN echo "Building OpenSSL" && \ tar xzf postgres.tar.gz && \ 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 && \ - cd src/interfaces/libpq && \ - make all-static-lib && sudo make install-lib-static && \ - cd ../../../.. && rm -rf postgres.tar.gz postgresql-$VERS + cd src/interfaces/libpq && make all-static-lib && sudo make install-lib-static && cd ../../.. && \ + cd src/bin/pg_config && make && sudo make install && cd ../.. && \ + cd .. && rm -rf postgres.tar.gz postgresql-$VERS ENV OPENSSL_DIR=/usr/local/musl/ \ OPENSSL_INCLUDE_DIR=/usr/local/musl/include/ \ DEP_OPENSSL_INCLUDE=/usr/local/musl/include/ \ OPENSSL_LIB_DIR=/usr/local/musl/lib/ \ OPENSSL_STATIC=1 \ - PQ_LIB_DIR=/usr/local/musl/lib \ - PQ_LIB_STATIC=1 + PQ_LIB_STATIC_X86_64_UNKNOWN_LINUX_MUSL=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 # libraries needed by the most popular and common Rust crates, to avoid diff --git a/README.md b/README.md index 52d5c28..b36ad4f 100644 --- a/README.md +++ b/README.md @@ -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/) -Do you want to compile a completely static Rust binary with no external -dependencies? If so, try: +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 --release ``` -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. +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 @@ -22,13 +17,11 @@ With a bit of luck, you should be able to just copy your application binary from ## How it works -`rust-musl-builder` uses [musl-libc][], [musl-gcc][], and the new -[rustup][] `target` support. It includes static versions of several -libraries: +`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. -- `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`. 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 These instructions are inspired by [rust-cross][]. -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: +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: ```yaml language: rust @@ -78,13 +85,9 @@ deploy: tags: true ``` -Next, copy [`build-release`](./examples/build-release) into your project -and run `chmod +x build-release`. +Next, copy [`build-release`](./examples/build-release) into your project and run `chmod +x build-release`. -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. +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. 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 -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 -[`Dockerfile.multistage-example`](./Dockerfile.multistage-example). +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). [multistage]: https://docs.docker.com/engine/userguide/eng-image/multistage-build/ [Alpine Linux]: https://alpinelinux.org/ ## 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: +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. -```Dockerfile -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. +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 -After modifying the image, run `./test-image` to make sure that everything -works. +After modifying the image, run `./test-image` to make sure that everything works.xs ## 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. - [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 diff --git a/examples/adding-a-library/.dockerignore b/examples/adding-a-library/.dockerignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/examples/adding-a-library/.dockerignore @@ -0,0 +1 @@ +target diff --git a/Dockerfile.example b/examples/adding-a-library/Dockerfile similarity index 100% rename from Dockerfile.example rename to examples/adding-a-library/Dockerfile diff --git a/examples/using-diesel/.dockerignore b/examples/using-diesel/.dockerignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/examples/using-diesel/.dockerignore @@ -0,0 +1 @@ +target diff --git a/examples/using-diesel/Cargo.lock b/examples/using-diesel/Cargo.lock new file mode 100644 index 0000000..9ba5935 --- /dev/null +++ b/examples/using-diesel/Cargo.lock @@ -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)" = "" +"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" diff --git a/examples/using-diesel/Cargo.toml b/examples/using-diesel/Cargo.toml new file mode 100644 index 0000000..9d95c3b --- /dev/null +++ b/examples/using-diesel/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "using-diesel" +version = "0.1.0" +authors = ["Eric Kidd "] + +[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' } diff --git a/Dockerfile.multistage-example b/examples/using-diesel/Dockerfile similarity index 57% rename from Dockerfile.multistage-example rename to examples/using-diesel/Dockerfile index ccf2275..dc3406a 100644 --- a/Dockerfile.multistage-example +++ b/examples/using-diesel/Dockerfile @@ -6,20 +6,19 @@ # Our first FROM statement declares the build environment. FROM ekidd/rust-musl-builder AS builder -# You would normally add your source code to /home/rust/src like this. -# ADD . ./ -# RUN sudo chmod -R . +# Add our source code. +ADD . ./ -# ...but we're going to just create a new app instead for demo purposes. -RUN cd .. && rm -r src && USER=rust cargo new --vcs none --bin --name hello src +# Fix permissions on source code. +RUN sudo chown -R rust:rust /home/rust # Build our application. 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 RUN apk --no-cache add ca-certificates 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/ -CMD /usr/local/bin/hello +CMD /usr/local/bin/using-diesel diff --git a/examples/using-diesel/docker-compose.yml b/examples/using-diesel/docker-compose.yml new file mode 100644 index 0000000..584d6a5 --- /dev/null +++ b/examples/using-diesel/docker-compose.yml @@ -0,0 +1,12 @@ +version: "2.0" + +services: + app: + build: . + volumes: + - cargo:/home/rust/.cargo + - target:/home/rust/src/target + +volumes: + cargo: {} + target: {} diff --git a/examples/using-diesel/src/main.rs b/examples/using-diesel/src/main.rs new file mode 100644 index 0000000..8e25a24 --- /dev/null +++ b/examples/using-diesel/src/main.rs @@ -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::(&conn) + .expect("could not load users"); + for row in rows { + println!("{:?}", row); + } + } else { + println!("No DATABASE_URL set, so doing nothing") + } +} diff --git a/test-image b/test-image index c4f1496..6bdd4d2 100755 --- a/test-image +++ b/test-image @@ -7,11 +7,11 @@ set -euo pipefail docker build -t ekidd/rust-musl-builder . # 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. -docker build -t rust-multistage-example -f Dockerfile.multistage-example . -docker run --rm rust-multistage-example +docker build -t rust-musl-builder-using-diesel examples/using-diesel +docker run --rm rust-musl-builder-using-diesel # Make sure we can build a static executable. docker run --rm ekidd/rust-musl-builder bash -c "