Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: alpine based docker image #981

Merged
merged 3 commits into from
Mar 21, 2022
Merged

Conversation

tynes
Copy link
Contributor

@tynes tynes commented Mar 18, 2022

Motivation

I have tasks that need to run in the cloud where bringing up a K8s pod
that I can attach to with cast would be nice. A docker image is required
for this and I'm sure other people would want a docker image so putting
the dockerfile here makes sense.

Solution

This commit creates a docker image that includes
both cast and forge.

It builds off of #914.

The image comes out to 26.4MB.

/ # du -h /usr/local/bin/forge
12.8M   /usr/local/bin/forge
/ # du -h /usr/local/bin/cast
7.1M    /usr/local/bin/cast

Example usage:

$ docker run --rm --entrypoint cast foundry:latest block --rpc-url https://mainnet.optimism.io latest
$ docker run --rm foundry:latest 'cast block --rpc-url https://mainnet.optimism.io latest'

Co-authored-by: Abdul Rabbani


One question is whether or not the container should include bash - some people might want to mount in bash scripts to run. I personally think yes but did not include bash in the image as to keep it minimal, will update if there is consensus around this.

@onbjerg onbjerg added the L-ignore Log: ignore PR in changelog label Mar 18, 2022
Copy link
Member

@onbjerg onbjerg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can further shrink the image if you run strip /usr/src/foundry/target/release/cast and strip /usr/src/foundry/target/release/forge

Also if this builds off of #914 let's merge into that branch instead :)

This commit creates a docker image that includes
both `cast` and `forge`.

It builds off of foundry-rs#914.

The image comes out to 26.4MB.

```bash
/ # du -h /usr/local/bin/forge
12.8M   /usr/local/bin/forge
/ # du -h /usr/local/bin/cast
7.1M    /usr/local/bin/cast
```

Example usage:

```bash
$ docker run --rm --entrypoint cast foundry:latest block --rpc-url https://mainnet.optimism.io latest
$ docker run --rm foundry:latest 'cast block --rpc-url https://mainnet.optimism.io latest'
```

Co-authored-by: Abdul Rabbani
@tynes
Copy link
Contributor Author

tynes commented Mar 19, 2022

You can further shrink the image if you run strip /usr/src/foundry/target/release/cast and strip /usr/src/foundry/target/release/forge

Also if this builds off of #914 let's merge into that branch instead :)

Good looks on using strip, that significantly made the image smaller.

I opened a PR that doesn't merge into #914 because the author said they don't have time to contribute more, see https://github.com/gakonst/foundry/pull/914/files#r830395267. If you are strongly opinionated on me opening this PR against their repo, I can.

Dockerfile Outdated
WORKDIR /usr/src/foundry
COPY . .

ENV RUSTFLAGS="-C target-cpu=native"
Copy link
Contributor

@dmfxyz dmfxyz Mar 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will targeting native cpu instruction set require a multi-architecture docker build when this image becomes part of a CI/CD pipeline?

@dmfxyz
Copy link
Contributor

dmfxyz commented Mar 19, 2022

@tynes running into the classic alpine solc issues when trying to compile with forge in the image. Are you seeing this? Could just be an issue on my local.

$> docker build -t ercc721 .
Sending build context to Docker daemon  26.03MB
Step 1/3 : from foundry-alpine-tynes
 ---> 25502cb6ddcd
Step 2/3 : COPY . .
 ---> ec207d41b853
Step 3/3 : RUN forge test -vv
 ---> Running in 021765ffee03
Error:
   0: "/root/.svm/0.8.13/solc-0.8.13": No such file or directory (os error 2)

Location:
   cli/src/cmd/mod.rs:96

Backtrace omitted.
Run with RUST_BACKTRACE=1 environment variable to display it.
Run with RUST_BACKTRACE=full to include source snippets.
The command '/bin/sh -c forge test -vv' returned a non-zero code: 1
$>

@tynes
Copy link
Contributor Author

tynes commented Mar 19, 2022

@tynes running into the classic alpine solc issues when trying to compile with forge in the image. Are you seeing this? Could just be an issue on my local.

$> docker build -t ercc721 .
Sending build context to Docker daemon  26.03MB
Step 1/3 : from foundry-alpine-tynes
 ---> 25502cb6ddcd
Step 2/3 : COPY . .
 ---> ec207d41b853
Step 3/3 : RUN forge test -vv
 ---> Running in 021765ffee03
Error:
   0: "/root/.svm/0.8.13/solc-0.8.13": No such file or directory (os error 2)

Location:
   cli/src/cmd/mod.rs:96

Backtrace omitted.
Run with RUST_BACKTRACE=1 environment variable to display it.
Run with RUST_BACKTRACE=full to include source snippets.
The command '/bin/sh -c forge test -vv' returned a non-zero code: 1
$>

Thanks for pointing this out. It turns out that /lib64/ld-linux-x86-64.so.2 didn't exist, which can be included with the package libc6-compat

/project # file ~/.svm/0.8.13/solc-0.8.13
/root/.svm/0.8.13/solc-0.8.13: ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=acd57a71be05049f965860e3311f82154d39c5ea, stripped

After installing that package, a new error happens

/project # ~/.svm/0.8.13/solc-0.8.13
Error loading shared library ld-linux-x86-64.so.2: No such file or directory (needed by /root/.svm/0.8.13/solc-0.8.13)
Error relocating /root/.svm/0.8.13/solc-0.8.13: __vsnprintf_chk: symbol not found
Error relocating /root/.svm/0.8.13/solc-0.8.13: __memmove_chk: symbol not found
Error relocating /root/.svm/0.8.13/solc-0.8.13: __memcpy_chk: symbol not found
Error relocating /root/.svm/0.8.13/solc-0.8.13: __memset_chk: symbol not found
Error relocating /root/.svm/0.8.13/solc-0.8.13: __snprintf_chk: symbol not found
Error relocating /root/.svm/0.8.13/solc-0.8.13: dlmopen: symbol not found
Error relocating /root/.svm/0.8.13/solc-0.8.13: __sprintf_chk: symbol not found
Error relocating /root/.svm/0.8.13/solc-0.8.13: __strftime_l: symbol not found

Perhaps we need a solc that is built against musl instead of glibc? Or we need to install the correct packages

solc can run on alpine fwiw
https://github.com/ethereum/solidity/blob/44fdcdcb12d72383c3dce2ff4e623616c07143f7/scripts/Dockerfile

@dmfxyz
Copy link
Contributor

dmfxyz commented Mar 19, 2022

@tynes take a look at https://github.com/abdulrabbani00/foundry/pull/3/files. Been taking a look at this for a couple nights. I opened this PR in order to build off of #914. I think it gets the job done but def needs more eyes and testing.

@dmfxyz
Copy link
Contributor

dmfxyz commented Mar 19, 2022

If we are migrating to this PR happy to open a PR against your repo as well :)

@tynes
Copy link
Contributor Author

tynes commented Mar 19, 2022

@tynes take a look at https://github.com/abdulrabbani00/foundry/pull/3/files. Been taking a look at this for a couple nights. I opened this PR in order to build off of #914. I think it gets the job done but def needs more eyes and testing.

Awesome, thanks for looking into this problem! I'm gonna be mostly afk this weekend so feel free to coordinate with the other PR or open your own if you are around this weekend. Happy to help test/do whatever to get this over the line :)

Copy link
Member

@gakonst gakonst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Supportive, defer to @mattsse @onbjerg on any extra changes

@dmfxyz
Copy link
Contributor

dmfxyz commented Mar 20, 2022

Awesome, thanks for looking into this problem! I'm gonna be mostly afk this weekend so feel free to coordinate with the other PR or open your own if you are around this weekend. Happy to help test/do whatever to get this over the line :)

@tynes opened a PR into your branch. tynes#1. This dockerfile should resolve the issues we saw above with solc (tested on a number of platforms). If you rebase or merge that in than we can continue using this PR (or I can open a new one if you'd prefer that and mark you as co-author if that's less of a burden).

Dockerfile Outdated
ENV RUSTFLAGS="-C target-cpu=native"
RUN cargo build --release

RUN strip /usr/src/foundry/target/release/cast \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rust has built-in stripping since 1.59, could that be used instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL,
perhaps we add a stripped profile that inherits from release and sets strip

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defer to whatever @tynes wants here, I think they're equivalent?

Dockerfile Outdated
WORKDIR /usr/src/foundry
COPY . .

ENV RUSTFLAGS="-C target-cpu=native"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure about this one, this may be fine when built for your own PC but probably not when push to ghcr or dockerhub?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good Q. I am not sure. I think let's remove.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah mentioned concerns with this above. I think it should only be used for local installs.

Copy link
Member

@gakonst gakonst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM so far. I'd love if we could also have a CI integration here, but acknowledge that it might be too much work. Up to you @tynes @dmfxyz

Dockerfile Outdated
WORKDIR /usr/src/foundry
COPY . .

ENV RUSTFLAGS="-C target-cpu=native"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good Q. I am not sure. I think let's remove.

Dockerfile Outdated
WORKDIR /usr/src/foundry
COPY . .

ENV RUSTFLAGS="-C target-cpu=native"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ENV RUSTFLAGS="-C target-cpu=native"

Dockerfile Outdated
ENV RUSTFLAGS="-C target-cpu=native"
RUN cargo build --release

RUN strip /usr/src/foundry/target/release/cast \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defer to whatever @tynes wants here, I think they're equivalent?

@dmfxyz
Copy link
Contributor

dmfxyz commented Mar 20, 2022

LGTM so far. I'd love if we could also have a CI integration here, but acknowledge that it might be too much work. Up to you @tynes @dmfxyz

I think what might be best is to merge a Dockerfile first (but don't merge this PR yet, as there are outstanding changes needed here tynes#1), and then open a separate PR for CI integration. We have 2 PRs (three if I were to open one rather than PRing into @tynes branch) for Docker right now. I say we get them closed / merged and we can start from a clean slate for CI integration.

@onbjerg onbjerg mentioned this pull request Mar 21, 2022
@onbjerg
Copy link
Member

onbjerg commented Mar 21, 2022

Let's get tynes#1 merged into this PR and get this over the finish line. It would be nice if we could get a workflow of some sort (see #914 for inspiration) to build a Docker container nightly and push it to GHCR 😄

@dmfxyz
Copy link
Contributor

dmfxyz commented Mar 21, 2022

Let's get tynes#1 merged into this PR and get this over the finish line. It would be nice if we could get a workflow of some sort (see #914 for inspiration) to build a Docker container nightly and push it to GHCR 😄

Yeah, I have an example workflow here, and a successful run and publish [here](: https://github.com/dmfxyz/foundry/runs/5615229053?check_suite_focus=true). But it needs some more refinement and won't be ready for a bit.

re-do dockerfile to build from scratch alpine, fix solc
Copy link
Member

@gakonst gakonst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm! let's ship this and we can do the GHA release separately

@gakonst gakonst merged commit 77882ab into foundry-rs:master Mar 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
L-ignore Log: ignore PR in changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants