]> Untitled Git - lemmy.git/commitdiff
Various build optimizations
authorFelix Ableitner <me@nutomic.com>
Fri, 12 Jun 2020 13:29:50 +0000 (15:29 +0200)
committerFelix Ableitner <me@nutomic.com>
Fri, 12 Jun 2020 19:45:23 +0000 (21:45 +0200)
12 files changed:
docker/dev/Dockerfile
docker/dev/docker_update.sh
docker/dev/test_deploy.sh
docker/prod/Dockerfile [new file with mode: 0644]
docker/prod/Dockerfile.aarch64 [moved from docker/dev/Dockerfile.aarch64 with 96% similarity]
docker/prod/Dockerfile.armv7hf [moved from docker/dev/Dockerfile.armv7hf with 94% similarity]
docker/prod/Dockerfile.libc [moved from docker/dev/Dockerfile.libc with 96% similarity]
docker/prod/deploy.sh [moved from docker/dev/deploy.sh with 96% similarity]
docs/src/contributing_docker_development.md
docs/src/contributing_local_development.md
server/Cargo.lock
server/Cargo.toml

index d9ffc2f3855c92c32979fff3d173923a25b7e0fe..fbec253670666b2cb37bcad6eb1d0ccb4f2f964f 100644 (file)
@@ -21,17 +21,13 @@ COPY server/Cargo.toml server/Cargo.lock ./
 RUN sudo chown -R rust:rust .
 RUN mkdir -p ./src/bin \
   && echo 'fn main() { println!("Dummy") }' > ./src/bin/main.rs 
-RUN cargo build --release
+RUN cargo build
 RUN rm -f ./target/x86_64-unknown-linux-musl/release/deps/lemmy_server*
 COPY server/src ./src/
 COPY server/migrations ./migrations/
 
-# Build for release
-RUN cargo build --frozen --release
-
-# Get diesel-cli on there just in case
-# RUN cargo install diesel_cli --no-default-features --features postgres
-
+# Build for debug
+RUN cargo build
 
 FROM ekidd/rust-musl-builder:1.42.0-openssl11 as docs
 WORKDIR /app
@@ -47,7 +43,7 @@ RUN apk add libpq
 
 # Copy resources
 COPY server/config/defaults.hjson /config/defaults.hjson
-COPY --from=rust /app/server/target/x86_64-unknown-linux-musl/release/lemmy_server /app/lemmy
+COPY --from=rust /app/server/target/x86_64-unknown-linux-musl/debug/lemmy_server /app/lemmy
 COPY --from=docs /app/docs/book/ /app/dist/documentation/
 COPY --from=node /app/ui/dist /app/dist
 
index 9d0f45429952c5b44ba073fc3c725f5e6d262862..21e92ba88f811a6a8035815b80fc714a02731d07 100755 (executable)
@@ -1,2 +1,6 @@
 #!/bin/sh
+set -e
+
+export COMPOSE_DOCKER_CLI_BUILD=1
+export DOCKER_BUILDKIT=1
 docker-compose up -d --no-deps --build
index 1ae91f6541899c259e598410af3f13b9884058a5..ce85f60c6f23f9e03beafd6361993ddc99a6a5ec 100755 (executable)
@@ -1,4 +1,8 @@
 #!/bin/sh
+set -e
+
+export COMPOSE_DOCKER_CLI_BUILD=1
+export DOCKER_BUILDKIT=1
 
 # Rebuilding dev docker
 docker-compose build
diff --git a/docker/prod/Dockerfile b/docker/prod/Dockerfile
new file mode 100644 (file)
index 0000000..b9eec45
--- /dev/null
@@ -0,0 +1,58 @@
+FROM node:10-jessie as node
+
+WORKDIR /app/ui
+
+# Cache deps
+COPY ui/package.json ui/yarn.lock ./
+RUN yarn install --pure-lockfile
+
+# Build 
+COPY ui /app/ui
+RUN yarn build
+
+FROM ekidd/rust-musl-builder:1.42.0-openssl11 as rust
+
+# Cache deps
+WORKDIR /app
+RUN sudo chown -R rust:rust .
+RUN USER=root cargo new server
+WORKDIR /app/server
+COPY server/Cargo.toml server/Cargo.lock ./
+RUN sudo chown -R rust:rust .
+RUN mkdir -p ./src/bin \
+  && echo 'fn main() { println!("Dummy") }' > ./src/bin/main.rs 
+RUN cargo build --release
+RUN rm -f ./target/x86_64-unknown-linux-musl/release/deps/lemmy_server*
+COPY server/src ./src/
+COPY server/migrations ./migrations/
+
+# Build for release
+RUN cargo build --frozen --release
+
+# reduce binary size
+RUN strip /app/server/target/x86_64-unknown-linux-musl/release/lemmy_server
+
+FROM ekidd/rust-musl-builder:1.42.0-openssl11 as docs
+WORKDIR /app
+COPY docs ./docs
+RUN sudo chown -R rust:rust .
+RUN mdbook build docs/
+
+
+FROM alpine:3.10
+
+# Install libpq for postgres
+RUN apk add libpq
+
+# Copy resources
+COPY server/config/defaults.hjson /config/defaults.hjson
+COPY --from=rust /app/server/target/x86_64-unknown-linux-musl/release/lemmy_server /app/lemmy
+COPY --from=docs /app/docs/book/ /app/dist/documentation/
+COPY --from=node /app/ui/dist /app/dist
+
+RUN addgroup -g 1000 lemmy
+RUN adduser -D -s /bin/sh -u 1000 -G lemmy lemmy
+RUN chown lemmy:lemmy /app/lemmy
+USER lemmy
+EXPOSE 8536
+CMD ["/app/lemmy"]
similarity index 96%
rename from docker/dev/Dockerfile.aarch64
rename to docker/prod/Dockerfile.aarch64
index 9636a5907a959b64b5c628833ba9935112adf512..7a1c833fc30b0b1c25a9e688baba43e4ab2db5bd 100644 (file)
@@ -40,10 +40,11 @@ COPY server/src ./src/
 COPY server/migrations ./migrations/
 RUN rm -f ./target/release/deps/lemmy_server* ; rm -f ./target/debug/deps/lemmy_server*
 
-
 # build for release
 RUN cargo build --frozen --release
-# RUN cargo build --frozen
+
+# reduce binary size
+RUN strip /app/server/target/release/lemmy_server
 
 # Get diesel-cli on there just in case
 # RUN cargo install diesel_cli --no-default-features --features postgres
similarity index 94%
rename from docker/dev/Dockerfile.armv7hf
rename to docker/prod/Dockerfile.armv7hf
index c2c9084c1326fca89a798c78fe2d8cd7da60b8f6..efd937a3cd70a93490df6e46d7ddd7f33b5f58dc 100644 (file)
@@ -43,8 +43,10 @@ RUN rm -f ./target/release/deps/lemmy_server* ; rm -f ./target/debug/deps/lemmy_
 
 
 # build for release
-#RUN cargo build --frozen --release
-RUN cargo build --frozen
+RUN cargo build --frozen --release
+
+# reduce binary size
+RUN strip /app/server/target/debug/lemmy_server
 
 # Get diesel-cli on there just in case
 # RUN cargo install diesel_cli --no-default-features --features postgres
similarity index 96%
rename from docker/dev/Dockerfile.libc
rename to docker/prod/Dockerfile.libc
index 6348342fd7afd4a0cdf60be57dfa410bedf7d222..9c7502827e56fa03e5dadc246eeb0dcf945a4f14 100644 (file)
@@ -43,7 +43,9 @@ COPY server/migrations ./migrations/
 # workaround for https://github.com/rust-lang/rust/issues/62896
 #RUN RUSTFLAGS='-Ccodegen-units=1' cargo build --release
 RUN cargo build --release --frozen
-#RUN cargo build --frozen
+
+# reduce binary size
+RUN strip /app/server/target/release/lemmy_server
 
 # Get diesel-cli on there just in case
 # RUN cargo install diesel_cli --no-default-features --features postgres
similarity index 96%
rename from docker/dev/deploy.sh
rename to docker/prod/deploy.sh
index 09b21b0b7aa35a4525b23e4fed348b36a7f1ea74..0ad47bcd809c153ae7cdc93e0dbca9df69e00fa7 100755 (executable)
@@ -1,4 +1,5 @@
 #!/bin/sh
+set -e
 git checkout master
 
 # Import translations
@@ -20,7 +21,7 @@ git add "server/src/version.rs"
 echo $new_tag > "ansible/VERSION"
 git add "ansible/VERSION"
 
-cd docker/dev || exit
+cd docker/prod || exit
 
 # Changing the docker-compose prod
 sed -i "s/dessalines\/lemmy:.*/dessalines\/lemmy:$new_tag/" ../prod/docker-compose.yml
@@ -32,6 +33,9 @@ git add ../../ansible/templates/docker-compose.yml
 git commit -m"Version $new_tag"
 git tag $new_tag
 
+export COMPOSE_DOCKER_CLI_BUILD=1
+export DOCKER_BUILDKIT=1
+
 # Rebuilding docker
 docker-compose build
 docker tag dev_lemmy:latest dessalines/lemmy:x64-$new_tag
index 092398219ba34ac29d6755bdb7fad4dd42c4f183..afa05107281c355775cd99903539736422f43cb1 100644 (file)
@@ -3,11 +3,22 @@
 ## Running
 
 ```bash
+sudo apt install git docker-compose
 git clone https://github.com/LemmyNet/lemmy
 cd lemmy/docker/dev
-./docker_update.sh # This builds and runs it, updating for your changes
+sudo docker-compose up --no-deps --build
 ```
 
 and go to http://localhost:8536.
 
-Note that compile times when changing `Cargo.toml` are relatively long with Docker, because builds can't be incrementally cached. If this is a problem for you, you should use [Local Development](contributing_local_development.md).
+To speed up the Docker compile, add the following to `/etc/docker/daemon.json` and restart Docker.
+```
+{
+  "features": {
+    "buildkit": true
+  }
+}
+```
+
+If the build is still too slow, you will have to use a
+[local build](contributing_local_development.md) instead.
index f801caf329df03ff8f12cb75d45eca4d80b9d384..066386f50747eac553368c0ec998e7e7ca77a98f 100644 (file)
@@ -56,6 +56,12 @@ Then open [localhost:4444](http://localhost:4444) in your browser. It will auto-
 any frontend files. For backend coding, you will have to rerun `cargo run`. You can use
 `cargo check` as a faster way to find compilation errors.
 
+To speed up incremental builds, you can add the following to `~/.cargo/config`:
+```
+[target.x86_64-unknown-linux-gnu]
+rustflags = ["-Clink-arg=-fuse-ld=lld"]
+```
+
 Note that this setup doesn't include image uploads or link previews (provided by pict-rs and
 iframely respectively). If you want to test those, you should use the
 [Docker development](contributing_docker_development.md).
index 6d6eb944a3a0402e1234dc5670b39ed17f9cc16b..f91b665b89a2f28f6c42ff56e1f9960e67c38446 100644 (file)
@@ -413,7 +413,6 @@ name = "attohttpc"
 version = "0.14.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
- "flate2 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)",
  "http 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
  "rustls 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2201,7 +2200,6 @@ name = "serde_json"
 version = "1.0.52"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
- "indexmap 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
  "itoa 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
  "ryu 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "serde 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)",
index 44ba152357ae0759ac481dd9b44ab411ee6bc84a..43933e5318225890ad4e3c657b1232fac1e6cb12 100644 (file)
@@ -4,16 +4,19 @@ version = "0.0.1"
 authors = ["Dessalines <tyhou13@gmx.com>"]
 edition = "2018"
 
+[profile.release]
+lto = true
+
 [dependencies]
 diesel = { version = "1.4.2", features = ["postgres","chrono", "r2d2", "64-column-tables"] }
 diesel_migrations = "1.4.0"
 dotenv = "0.15.0"
 bcrypt = "0.7.0"
 activitypub = "0.2.0"
-chrono = { version = "0.4.7", features = ["serde"] }
+chrono = "0.4.7"
 failure = "0.1.8"
-serde_json = { version = "1.0.52", features = ["preserve_order"]}
-serde = { version = "1.0.105", features = ["derive"] }
+serde_json = "1.0.52"
+serde = "1.0.105"
 actix = "0.9.0"
 actix-web = "2.0.0"
 actix-files = "0.2.1"
@@ -34,7 +37,7 @@ rss = "1.9.0"
 htmlescape = "0.3.1"
 config = {version = "0.10.1", default-features = false, features = ["hjson"] }
 percent-encoding = "2.1.0"
-attohttpc = { version = "0.14.0", default-features = false, features = ["tls-rustls", "compress"] }
+attohttpc = { version = "0.14.0", default-features = false, features = ["tls-rustls"] }
 comrak = "0.7"
 tokio = "0.2.20"
 futures = "0.3.4"