CosmWasm / optimizer

Dockerfile and script to deterministically produce the smallest possible Wasm for your Rust contract
Apache License 2.0
123 stars 61 forks source link

Use builder pattern to reduce image sizes #54

Closed harryscholes closed 3 years ago

harryscholes commented 3 years ago

Builds on top of #53 to use the builder pattern. Here, we do general work in a temporary builder stage and copy only the required files into the rust-optimizer and workspace-optimizer images. Anything that doesn't get copied into the final images is thrown away. This reduces image sizes by ~10% to 900MB. Note that rust:1.54.0-alpine is 786MB before we add any wasm specific stuff, so only adding ~100MB is quite good.

❯ docker image ls
REPOSITORY                     TAG             IMAGE ID       CREATED         SIZE
cosmwasm/rust-optimizer        0.12.1          4f80eb3b2ad0   3 hours ago     902MB
cosmwasm/workspace-optimizer   0.12.1          b7bb3c9473e4   3 hours ago     924MB
cosmwasm/workspace-optimizer   0.12.0          b9f4ed28322e   4 hours ago     1.03GB
cosmwasm/rust-optimizer        0.12.0          517921e0c25b   4 hours ago     1.06GB

The way the Dockerfiles were written before meant that files that were removed in separate RUN commands were still present in other layers, e.g.

https://github.com/CosmWasm/rust-optimizer/blob/5a519d85ec6a69fe46b7864607b0092267ade209/Dockerfile#L20-L23

The 0.12.0 and 0.12.1 containers produce identical wasm files:

❯ docker run --rm -v "$(pwd)":/code \
  --mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \
  --mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
  cosmwasm/rust-optimizer:0.12.0
...
❯ cat artifacts/checksums.txt
a25ad28b91754a99cfeadcb8f7f6f0570a118378cbd284938620089b4ada5268  terraswap_factory.wasm
f0a4828cd0df981d451e8544cc4d5f530b5e47c6c487856c0e9ced907da11d0e  terraswap_pair.wasm
d68f8b74597e9f374b48aad44ea7746203de4eee3b7cb9f9dde605ca43a21aa5  terraswap_router.wasm
86c1ed459c9030844adbbbae1793e05371672547ce387b33feb2c78100a796dd  terraswap_token.wasm
❯ docker run --rm -v "$(pwd)":/code \
  --mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \
  --mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
  cosmwasm/rust-optimizer:0.12.1-rc1
...
❯ cat artifacts/checksums.txt
a25ad28b91754a99cfeadcb8f7f6f0570a118378cbd284938620089b4ada5268  terraswap_factory.wasm
f0a4828cd0df981d451e8544cc4d5f530b5e47c6c487856c0e9ced907da11d0e  terraswap_pair.wasm
d68f8b74597e9f374b48aad44ea7746203de4eee3b7cb9f9dde605ca43a21aa5  terraswap_router.wasm
86c1ed459c9030844adbbbae1793e05371672547ce387b33feb2c78100a796dd  terraswap_token.wasm
webmaster128 commented 3 years ago

Tagges and released as 0.12.1