foundry-rs / foundry

Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.
https://getfoundry.sh
Apache License 2.0
8.13k stars 1.68k forks source link

Add Alpine support for Foundry binaries #8498

Open susruth opened 1 month ago

susruth commented 1 month ago

Component

Forge, Cast, Anvil, Chisel

Describe the feature you would like

When attempting to run a Docker image that got build using Alpine, I encounter an error indicating that anvil is not found. However, using the same configuration with Ubuntu works as expected. Below are the respective Dockerfile configurations for both Alpine and Ubuntu.

Alpine Dockerfile:

FROM alpine:latest AS builder

WORKDIR /app

RUN apk update && apk add --no-cache wget

ARG TARGETARCH
RUN if [ "$TARGETARCH" = "amd64" ]; then \
    wget https://github.com/foundry-rs/foundry/releases/download/nightly-62cdea8ff9e6efef011f77e295823b5f2dbeb3a1/foundry_nightly_linux_amd64.tar.gz && \
    tar -xzf foundry_nightly_linux_amd64.tar.gz; \
    elif [ "$TARGETARCH" = "arm64" ]; then \
    wget https://github.com/foundry-rs/foundry/releases/download/nightly-62cdea8ff9e6efef011f77e295823b5f2dbeb3a1/foundry_nightly_linux_arm64.tar.gz && \
    tar -xzf foundry_nightly_linux_arm64.tar.gz; \
    else \
    echo "Unsupported architecture"; exit 1; \
    fi

FROM alpine:latest

EXPOSE 8545

WORKDIR /app

RUN apk update && apk add --no-cache curl

COPY --from=builder /app/anvil .
COPY ./cmd.sh .
COPY ./ignition/data/ .

CMD ["./cmd.sh"]

Ubuntu Dockerfile:

FROM ubuntu:latest AS builder

WORKDIR /app

RUN apt-get update && apt-get install -y wget --fix-missing --fix-broken

ARG TARGETARCH
RUN if [ "$TARGETARCH" = "amd64" ]; then \
    wget https://github.com/foundry-rs/foundry/releases/download/nightly-62cdea8ff9e6efef011f77e295823b5f2dbeb3a1/foundry_nightly_linux_amd64.tar.gz && \
    tar -xzf foundry_nightly_linux_amd64.tar.gz; \
    elif [ "$TARGETARCH" = "arm64" ]; then \
    wget https://github.com/foundry-rs/foundry/releases/download/nightly-62cdea8ff9e6efef011f77e295823b5f2dbeb3a1/foundry_nightly_linux_arm64.tar.gz && \
    tar -xzf foundry_nightly_linux_arm64.tar.gz; \
    else \
    echo "Unsupported architecture"; exit 1; \
    fi

FROM ubuntu:latest

EXPOSE 8545

WORKDIR /app

RUN apt-get update && apt-get install -y curl --fix-missing --fix-broken

COPY --from=builder /app/anvil .
COPY ./cmd.sh .
COPY ./ignition/data/ .

CMD ["./cmd.sh"]

External file cmd.sh that might be required is:

#!/bin/sh

./anvil --host 0.0.0.0 --block-time 10 &

sleep 3 

if [ -n "$CHAIN_ID" ]; then
    response=$(curl -s -X POST -H "Content-Type: application/json" --data "{\"jsonrpc\":\"2.0\", \"method\":\"anvil_setChainId\", \"params\":[$CHAIN_ID], \"id\":1}" http://localhost:8545)
    echo "Set chain ID response: $response"
else
    echo "CHAIN_ID is not set, default chain ID is 31337."
fi

if [ "$LIGHT" = "true" ]; then
    state_file="./lightState.json"
else
    state_file="./fullState.json"
fi

response=$(curl -s -X POST -H "Content-Type: application/json" --data-binary @$state_file http://localhost:8545)
echo "Set blockchain state response: $response"

tail -f /dev/null

Error Output

cmd.sh: line 3: ./anvil: not found

Expected Behavior

The Alpine Docker image should build and run successfully, similar to the Ubuntu image.

Additional Information

Additional context

No response

zerosnacks commented 1 month ago

I think this is because Alpine uses MUSL whereas Foundry has a dependency on GLIBC

A possible workaround is using the https://hub.docker.com/r/frolvlad/alpine-glibc/ image (see: https://github.com/foundry-rs/foundry/blob/master/Dockerfile)

gnapoli23 commented 1 week ago

+1 on this, it would be very helpful to have it since it takes quite a bit to compile each crate; is there any kind of limitation behind the lack of musl targets so far? I would like to help with it, having a bit of context would be helpful.