Skip to content

Commit

Permalink
ci image dockerfile for building contracts using cargo contract v4.1.1 (
Browse files Browse the repository at this point in the history
#1275)

* ci image dockerfile for building contracts using cargo contract v4.1.1

* make dockerfile use bash script for env init

* set cargo and rust dirs with open permissions

* autoremove deps

* troubleshooting, fix cargo-cache usage
  • Loading branch information
goastler authored Jun 17, 2024
1 parent 1009b9f commit 582fa7f
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
67 changes: 67 additions & 0 deletions docker/images/cargo-contract/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
FROM docker.io/library/debian:stable-slim

SHELL ["/bin/bash", "-c"]

# The version of the toolchain to install
ARG TOOLCHAIN_VERSION=1.77.0

RUN apt-get update && apt-get install -y \
# required for downloading the setup script for rust
curl \
# all deps for building cargo-contract
gcc \
g++ \
pkg-config \
openssl \
libssl-dev

# Set rustup and cargo home dirs
ENV RUSTUP_HOME=/usr/local/rustup
ENV CARGO_HOME=/usr/local/cargo

# Install Rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain ${TOOLCHAIN_VERSION} --profile minimal

# Add cargo to the PATH
ENV PATH=$PATH:$CARGO_HOME/bin:$RUSTUP_HOME

# Install wasm32 target
RUN rustup target add wasm32-unknown-unknown
# Install clippy and rustfmt
RUN rustup component add rust-src
RUN rustup component add clippy
RUN rustup component add rustfmt

# Install cargo-contract deps
RUN cargo install cargo-dylint dylint-link

# The version of cargo-contract to install
ARG CARGO_CONTRACT_VERSION=4.1.1

# Install cargo-contract
RUN cargo install --force --locked --version ${CARGO_CONTRACT_VERSION} cargo-contract

# Cleanup deps for installing cargo-contract (almost everything except gcc, that's needed for contract compilation)
RUN apt-get remove --auto-remove -y curl pkg-config openssl libssl-dev g++ && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Create cache dirs for cargo
ARG CARGO_CACHE=/cargo-cache
RUN mkdir ${CARGO_CACHE}
RUN rm -rf ${CARGO_HOME}/registry
RUN rm -rf ${CARGO_HOME}/git
RUN ln -s ${CARGO_CACHE}/registry ${CARGO_HOME}/registry
RUN ln -s ${CARGO_CACHE}/git ${CARGO_HOME}/git

# set permissions for the cargo-contract binary. This allows the user which invokes the container to read/write/exe to these dirs
RUN chmod -R a+wrx ${CARGO_HOME}
RUN chmod -R a+wrx ${RUSTUP_HOME}
RUN chmod -R a+wrx ${CARGO_CACHE}

# Add the main script
COPY main.sh /main.sh
RUN chmod +x /main.sh

# Set the entrypoint
ENTRYPOINT ["bash", "/main.sh"]
21 changes: 21 additions & 0 deletions docker/images/cargo-contract/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
dockerfile with cargo-contract precompiled into smallest image possible.

## To run

```
cd protocol
docker run --rm -it -u $(id -u):$(id -g) --cpu-quota=-1 -v $PWD:/src -v $PWD/cargo-cache:/usr/local/cargo/registry prosopo/cargo-contract:4.1.1 'cd contracts/captcha && cargo contract build'
```

Explanation:
- `--rm`: remove container after command has finished
- `-it`: setup tty terminal to make ctrl+c work
- `-u`: set the uid and gid of the user running the command (ensuring files are created by _your_ user, not _root_)
- `-v $PWD:/src`: mount the volume for /src. This should be the dir containing your contracts **and** the output dir (i.e. mount the workspace root so the target dir can be seen, as it's higher in the hierarchy than the contracts themselves)
- `-v $PWD/cargo-cache:/usr/local/cargo/registry`: mount a directory which caches the cargo registry. The registry contains compiled code for dependencies of the package, so caching this avoids a lot of recompilation.
- `--cpu-quota=-1`: unlimited cpu, run on all cores. Quicker compilation using multiple cores instead of single by default.
- `prosopo/cargo-contract:4.1.1`: the image name
- `cd protocol/contracts/captcha && cargo contract build`: command to run, adjust as you need. This has cargo, rustup and cargo contract available.

## Troubleshooting
Remove `target`, `cargo-cache` and `Cargo.lock`, then try again.
13 changes: 13 additions & 0 deletions docker/images/cargo-contract/main.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#! /bin/bash

# Stop on error / print each command / stop on unset variable / stop on pipe fail
set -euxo pipefail

mkdir -p /cargo-cache/registry
mkdir -p /cargo-cache/git

# Move to the source directory, which should be a mounted volume
cd /src

# Run the command passed as arguments to the script
eval "$@"

0 comments on commit 582fa7f

Please sign in to comment.