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

Dockerize node #257

Merged
merged 30 commits into from
Mar 14, 2024
Merged

Dockerize node #257

merged 30 commits into from
Mar 14, 2024

Conversation

phklive
Copy link
Contributor

@phklive phklive commented Mar 1, 2024

Docker will help us end-to-end test our code & enable easier sharing and installation of our code by our users.

Closes: #93

@phklive phklive requested review from bobbinth and hackaugusto March 4, 2024 19:16
@phklive phklive marked this pull request as ready for review March 4, 2024 19:16
Copy link
Contributor

@hackaugusto hackaugusto left a comment

Choose a reason for hiding this comment

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

Thanks! I left a few questions and suggestions.

Dockerfile Outdated Show resolved Hide resolved
Dockerfile Outdated
RUN cd miden-node && make

### Run Miden-Node
FROM ubuntu:22.04
Copy link
Contributor

@hackaugusto hackaugusto Mar 4, 2024

Choose a reason for hiding this comment

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

questions:

  • why ubuntu here instead of debian like the builder image? if we can, debian slim/alpine would be better
  • why 22.04 instead of 24.04?

Copy link
Contributor

Choose a reason for hiding this comment

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

This image should have LABELs. I suggest using open containers labels:

LABEL [email protected] \
      org.opencontainers.image.url=https://0xpolygonmiden.github.io/ \
      org.opencontainers.image.documentation=https://github.com/0xPolygonMiden/miden-node \
      org.opencontainers.image.source=https://github.com/0xPolygonMiden/miden-node \
      org.opencontainers.image.vendor=Polygon \
      org.opencontainers.image.licenses=MIT

ARG CREATED
ARG VERSION
ARG COMMIT
LABEL org.opencontainers.image.created=$CREATED \
      org.opencontainers.image.version=$VERSION \
      org.opencontainers.image.revision=$COMMIT

Copy link
Contributor

Choose a reason for hiding this comment

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

We need to add a VOLUME to have the store's db.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To add a volume we would need to modify where the node reads and writes it's database files in order to persist them to the volume.

Copy link
Contributor

Choose a reason for hiding this comment

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

To add a volume we would need to modify where the node reads and writes it's database files in order to persist them to the volume.

The WORKDIR setting should handle that.

Copy link
Contributor Author

@phklive phklive Mar 13, 2024

Choose a reason for hiding this comment

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

This is the last thing pending, have not yet been able to make it work correctly. Looking into it now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Managed to fix this, now the node creates a db folder to put it's sqlite files, this volume is mounted from the host as a volume.

Dockerfile Outdated Show resolved Hide resolved
Dockerfile Outdated Show resolved Hide resolved
scripts/start-miden-node.sh Outdated Show resolved Hide resolved
Makefile Outdated Show resolved Hide resolved
Dockerfile Outdated Show resolved Hide resolved
Dockerfile Outdated Show resolved Hide resolved
Dockerfile Outdated Show resolved Hide resolved
Dockerfile Outdated Show resolved Hide resolved
@phklive phklive changed the base branch from main to next March 11, 2024 17:51
@phklive phklive requested review from hackaugusto and polydez March 12, 2024 09:55
Copy link
Contributor

@polydez polydez left a comment

Choose a reason for hiding this comment

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

I've left one nit comment inline, but overall it looks good!


ARG CREATED
ARG VERSION
ARG COMMIT
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we move all args to the top (line 27 or even top of file) and group all labels into one single LABEL?

Copy link
Contributor

@hackaugusto hackaugusto Mar 14, 2024

Choose a reason for hiding this comment

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

I think this is not a good idea. When I'm debugging the DockerFile, I will rebuild the same image multiple times using the same COMMIT, the same VERSION, and different CREATED timestamps.

The CREATED timestamps change will invalidate the cache, and force a rebuild of everything that follows it. Moving this to the top of the file means we never reuse the caching system when rebuilding images, and we spend tons of time with rebuilds.

Please move it down, as far as you can. Here is the docs

Copy link
Contributor

@hackaugusto hackaugusto left a comment

Choose a reason for hiding this comment

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

Thanks! LGTM

Makefile.toml Show resolved Hide resolved
Dockerfile Outdated
RUN cd miden-node && make

### Run Miden-Node
FROM ubuntu:22.04
Copy link
Contributor

Choose a reason for hiding this comment

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

To add a volume we would need to modify where the node reads and writes it's database files in order to persist them to the volume.

The WORKDIR setting should handle that.

node/Dockerfile Outdated
Comment on lines 30 to 31
libssl-dev \
libsqlite3-dev \
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need these dependencies? If so, can you please add some docs about it?

Copy link
Contributor

Choose a reason for hiding this comment

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

@hackaugusto IIRC, we need them for building under linux.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is the runner image, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes it is indeed. Running the program failed when these were not installed on the machine.

Copy link
Contributor

Choose a reason for hiding this comment

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

That is interesting. Can you please add a comment above the deps. It seems a bit off that the headers are needed to run the compiled binary. Also, what was the error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

2024-03-12 16:55:51 miden-node: error while loading shared libraries: libsqlite3.so.0: cannot open shared object file: No such file or directory

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, for that you shouldn't need the headers, you only need the library. libsqlite3 should be enough, it is only marginally better though (to avoid confusion like I had here)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hence what do you recommend to do?

Copy link
Contributor

@hackaugusto hackaugusto Mar 12, 2024

Choose a reason for hiding this comment

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

Here is what I have been proposing:

  • keep dependencies to a minimum
  • document why they are needed

why am I proposing the above?

  • it is hard to maintain script files without this information, for example, the discussion of gcc and clang being needed is something that happened because of lack of documentation. I'm trying to prevent that
  • the above is specially a problem when upgrade the file down the line, and usually causes issues were one doesn't know why something is there, so we default to "better not touch it since it is working", which can compound over time

so here, since libsqlite3-dev is not needed, these are the headers, we would remove that dependency and have libsqlite3 instead. since that is the .so file that is needed to run the software, and add a comment # sqlite3: used by the store.

That way we have a minimum number of dependencies and some traceability on them, making it easier to maintain it.

edit: same thing for libssl-dev

@phklive phklive requested a review from hackaugusto March 14, 2024 11:06
Copy link
Contributor

@hackaugusto hackaugusto left a comment

Choose a reason for hiding this comment

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

Thank you! I got the node started and played around with it a bit :) Super neat. We have some few things to fix though.

node/Dockerfile Outdated
Comment on lines 32 to 33
RUN --mount=type=cache,target=/usr/local/cargo/registry \
cargo fetch --manifest-path node/Cargo.toml
Copy link
Contributor

Choose a reason for hiding this comment

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

This path doesn't seem right.

  • The build cache is ./target ref
  • The registry cache is $CARGO_HOME/registry, which is usually $HOME/.cargo/registry ref

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I just tested, the cache is not working, the VM is downloading everything fresh:

[1/2] STEP 10/12: RUN --mount=type=cache,target=/usr/local/cargo/registry     cargo fetch --manifest-path node/Cargo.toml
info: syncing channel updates for '1.75-aarch64-unknown-linux-gnu'
info: latest update on 2023-12-28, rust version 1.75.0 (82e1608df 2023-12-21)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
info: installing component 'rust-std'
info: installing component 'rustc'
info: installing component 'rustfmt'

(also compiling)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed for now the caching does not work. I am looking into doing it manually as we did or using cargo-chef: https://github.com/LukeMathWalker/cargo-chef

What do you think?

Makefile.toml Show resolved Hide resolved
**/secrets.dev.yaml
**/values.dev.yaml
/bin
/target
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not working for me. Maybe it is a different among podman and docker. But this is causing me to upload 11G of data to the docker VM, and at first sign it looked like podman wasn't working.

Can you change the paths from /bin and /target to ./bin and ./target? And also test on your machine?

Copy link
Contributor

Choose a reason for hiding this comment

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

I now wonder how the --mount is implemented on MacOs, because I may have given bad advice with the cargo cache below, if the data has to be copied to the VM.

RUN miden-node make-genesis --inputs-path node/genesis.toml

# Run Miden node
FROM debian:bookworm-slim
Copy link
Contributor

Choose a reason for hiding this comment

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

Note: The final image has an okay size:

REPOSITORY                  TAG                 IMAGE ID      CREATED             SIZE
localhost/miden-node-image  latest              63b9223ac3cb  About a minute ago  124 MB

Copy link
Contributor

Choose a reason for hiding this comment

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

Note: start/stop is working, the state is not lost

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: The final image has an okay size:

REPOSITORY                  TAG                 IMAGE ID      CREATED             SIZE
localhost/miden-node-image  latest              63b9223ac3cb  About a minute ago  124 MB

Do you have ideas in mind to improve on it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: start/stop is working, the state is not lost

This is positive right?

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you have ideas in mind to improve on it?

I think this size is good enough. I was just adding context to the PR.

Note: start/stop is working, the state is not lost

Yes. This was not a request. I was just adding context to the PR.


ARG CREATED
ARG VERSION
ARG COMMIT
Copy link
Contributor

@hackaugusto hackaugusto Mar 14, 2024

Choose a reason for hiding this comment

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

I think this is not a good idea. When I'm debugging the DockerFile, I will rebuild the same image multiple times using the same COMMIT, the same VERSION, and different CREATED timestamps.

The CREATED timestamps change will invalidate the cache, and force a rebuild of everything that follows it. Moving this to the top of the file means we never reuse the caching system when rebuilding images, and we spend tons of time with rebuilds.

Please move it down, as far as you can. Here is the docs

Makefile.toml Outdated
# docker
[tasks.docker-build-node]
command = "docker"
args = ["build", "-f", "node/Dockerfile", "-t", "miden-node-image", "."]
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not setting the ENV vars:

time="2024-03-14T13:29:04+01:00" level=warning msg="missing \"CREATED\" build argument. Try adding \"--build-arg CREATED=<VALUE>\" to the command line"
time="2024-03-14T13:29:04+01:00" level=warning msg="missing \"VERSION\" build argument. Try adding \"--build-arg VERSION=<VALUE>\" to the command line"
time="2024-03-14T13:29:04+01:00" level=warning msg="missing \"COMMIT\" build argument. Try adding \"--build-arg COMMIT=<VALUE>\" to the command line"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, I am curious from what data you would want to populate these fields ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added the version and created from the crate version and date shell command.

I am not sure how you would like to get the commit info.

Copy link
Contributor

Choose a reason for hiding this comment

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

The COMMIT should have the git commit.
The VERSION should have the node's software version.
The CREATED should have the created timestamp.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Where can i get the created timestamp and the git commit ?

node/Dockerfile Outdated
VOLUME db

# Copy artifacts from the builder stage
COPY --from=builder /app/node/miden-node.toml miden-node.toml
Copy link
Contributor

Choose a reason for hiding this comment

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

This means we can't change the configuration without having to rebuild all images. This is not good. This should be mounted from the host.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I enabled the manual mounting of the file using the -v flag.

Copy link
Contributor

Choose a reason for hiding this comment

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

ah, makes sense, so this file is the default, and the user can overwrite it with the flag 👍

node/Dockerfile Outdated Show resolved Hide resolved
Copy link
Contributor

@hackaugusto hackaugusto left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

Makefile.toml Outdated Show resolved Hide resolved
@phklive
Copy link
Contributor Author

phklive commented Mar 14, 2024

Tested. Last thing we could do would be to improve caching to reduce build time of the image, created a issue so we can merge this PR and go forward: #277

@phklive phklive merged commit 2e66d45 into next Mar 14, 2024
5 checks passed
@phklive phklive deleted the phklive-dockerize-node branch March 14, 2024 15:23
Copy link
Contributor

Choose a reason for hiding this comment

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

Quick question: why do we need to duplicate this file here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We do not need to, fixed it in the next PR.

bobbinth pushed a commit that referenced this pull request Apr 12, 2024
* Working initial docker

* fmt

* added script

* Removed docker script + updated integration

* script works

* Need to install grpcurl

* Docker works

* Working Dockerfile

* ci: turn doc warnings into errors (#259)

* Removed makefile, removing start.sh file moving in Dockerfile

* Added bookworm + alpine + removed gcc + added caching

* Moved Dockerfile to node and added Makefile.toml

* cargo make works + builds dockerfile for node

* remove start script

* added comment regarding PID1

* Set labels at top of Dockerfile + added comments

* Added correct dependencies and explanation

* Volume works persisting db files between runs

* Added documentation

* Moved labels + enable mounting of miden-node.toml

* Added docker-run-node command + added build arguments

* Add git commit as arg to docker container

* Added spacing

---------

Co-authored-by: Augusto Hack <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add full docker-compose
4 participants