Skip to content

Commit

Permalink
Merge pull request #732 from vsbogd/release-docker
Browse files Browse the repository at this point in the history
Minimize docker image size
  • Loading branch information
vsbogd authored Jul 9, 2024
2 parents 2f7a59b + 6b62400 commit e027fe2
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 50 deletions.
1 change: 1 addition & 0 deletions .dockerignore
13 changes: 12 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/build
# `.gitignore` file is also used to exclued files going into Docker image
# building context. All ignored files should be put into the same `.gitignore`
# at the root of the repository by this reason.
*.swp
compile_commands.json
.cache
Expand All @@ -7,3 +9,12 @@ Cargo.lock
.DS_Store
.vscode
.aider*
__pycache__
/build
/python/hyperon.egg-info
/python/dist
/python/build
/python/*.so
/python/*.dylib
/python/wheelhouse
/python/hyperon/_version.py
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ include(ExternalProject)

option(GIT "Adds git features to hyperon library; requires OpenSSL and Zlib" ON)

set(HYPERONC_INSTALL_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/hyperonc-prefix")
set(HYPERONC_INSTALL_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/hyperonc-install")
message(STATUS "HYPERONC_INSTALL_PREFIX = ${HYPERONC_INSTALL_PREFIX}")

set(IS_RELEASE_BUILD $<IF:$<OR:$<CONFIG:Release>,$<CONFIG:RelWithDebInfo>,$<CONFIG:MinSizeRel>>,1,0>)
Expand All @@ -16,7 +16,6 @@ set(BUILD_CONFIGURATION $<IF:${IS_RELEASE_BUILD},Release,Debug>)
ExternalProject_Add(
hyperonc
BUILD_ALWAYS 1
PREFIX "${HYPERONC_INSTALL_PREFIX}"
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/c"
CMAKE_ARGS
-DGIT=${GIT}
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ hyperon = { path = "./lib", version = "0.1.12" }
regex = "1.5.4"
log = "0.4.0"
env_logger = "0.8.4"

[profile.release]
strip = "symbols"
54 changes: 41 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
FROM ubuntu:22.04
FROM python:3.10-slim-bookworm AS os

RUN apt-get update && \
FROM os AS build

RUN apt update && \
DEBIAN_FRONTEND=noninteractive \
TZ=UTC \
apt-get install -y sudo git python3 python3-pip curl gcc cmake \
apt install -y sudo git curl cmake build-essential \
pkg-config libssl-dev zlib1g-dev && \
rm -rf /var/lib/apt/lists/*

Expand All @@ -16,25 +18,51 @@ WORKDIR ${HOME}

RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > /tmp/rustup.sh
RUN sh /tmp/rustup.sh -y && rm /tmp/rustup.sh
ENV PATH="${PATH}:/home/user/.cargo/bin"
ENV PATH="${PATH}:${HOME}/.cargo/bin"
RUN cargo install cbindgen

RUN python3 -m pip install conan==1.64 pip==23.1.2
ENV PATH="${PATH}:/home/user/.local/bin"
ENV PATH="${PATH}:${HOME}/.local/bin"
RUN conan profile new --detect default

ADD --chown=user:users . ${HOME}/hyperon-experimental
WORKDIR ${HOME}/hyperon-experimental
RUN mkdir build

WORKDIR ${HOME}/hyperon-experimental/lib
RUN cargo build
RUN cargo test
WORKDIR ${HOME}/hyperon-experimental
RUN cargo test --release
RUN cargo build --release

WORKDIR ${HOME}/hyperon-experimental/build
RUN cmake ..
ENV BUILD=${HOME}/hyperon-experimental/build
RUN mkdir ${BUILD}
WORKDIR ${BUILD}
RUN cmake -DCMAKE_BUILD_TYPE=Release ..
RUN make
RUN make check

ENV HYPERONPY=${BUILD}/hyperonpy-install
RUN mkdir ${HYPERONPY}
RUN python3 -m pip install --prefix ${HYPERONPY} ../python

WORKDIR ${HOME}/hyperon-experimental
RUN python3 -m pip install -e ./python[dev]

CMD bash

FROM os

ENV BUILD=/home/user/hyperon-experimental/build
COPY --from=build /home/user/hyperon-experimental/target/release/metta-repl /usr/bin
COPY --from=build ${BUILD}/hyperonc-install /usr
COPY --from=build ${BUILD}/hyperonpy-install /usr/local
COPY --from=build ${BUILD}/../python/VERSION /root/version

RUN cat >>/root/welcome <<EOF
echo ""
echo " Welcome to the MeTTa $(cat /root/version) running environment."
echo " Use the following commands to run MeTTa interpreter:"
echo ""
echo " metta-repl - start Rust based REPL"
echo " metta-py - start Python based script executor"
echo ""
EOF
RUN echo "sh /root/welcome" >/root/.bashrc

CMD bash
49 changes: 27 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,47 @@ The following command installs the latest release version from PyPi package repo
python3 -m pip install hyperon
```

# Prepare development environment

## Docker

A docker image can be used as a ready to run stable and predictable development
environment. Please keep in mind that resulting image contains temporary build
files and takes a lot of a disk space. It is not recommended to distribute it
as an image for running MeTTa because of its size.
Another way is using released Docker image:
```
docker run -ti trueagi/hyperon:latest
```

### Ready to use image
After installing package or starting Docker container run MeTTa Python based
interpreter:
```
metta-py
```

Run latest docker image from the Dockerhub:
Using Docker you can also run Rust REPL:
```
docker run -ti trueagi/hyperon:latest
metta-repl
```

### Build image
# Using latest development version

Docker 26.0.0 or greater version is required.
## Docker

A docker image can be used as a ready to run stable and predictable development
environment. Docker 26.0.0 or greater version is required to build image
manually.

Build Docker image from a local copy of the repo running:
```
docker build -t trueagi/hyperon .
```

Or build it without cloning the repo running:
Or build it without local copy of the repo running:
```
docker build \
--build-arg BUILDKIT_CONTEXT_KEEP_GIT_DIR=1 \
-t trueagi/hyperon \
http://github.com/trueagi-io/hyperon-experimental.git#main
```

Run the image:
Use `--target build` option to create an image which keeps the full build
environment and can be used for developing interpreter:
```
docker run --rm -ti trueagi/hyperon
docker build --target build -t trueagi/hyperon .
```

If the docker image doesn't work, please raise an
Expand Down Expand Up @@ -126,9 +131,9 @@ cargo run --example sorted_list

Run Rust REPL:
```
cargo run --features no_python --bin metta
cargo run --features no_python --bin metta-repl
```
You can also find executable at `./target/debug/metta`.
You can also find executable at `./target/debug/metta-repl`.

To enable logging during running tests or examples export `RUST_LOG`
environment variable:
Expand Down Expand Up @@ -179,18 +184,18 @@ pytest ./tests

One can run MeTTa script from command line:
```
metta ./tests/scripts/<name>.metta
metta-py ./tests/scripts/<name>.metta
```

Run REPL:
```
cargo run --bin metta
cargo run --bin metta-repl
```
You can also find executable at `./target/debug/metta`.
You can also find executable at `./target/debug/metta-repl`.

### Logger

You can enable logging by prefixing the `metta` command line by
You can enable logging by prefixing the MeTTa command line by

```
RUST_LOG=hyperon[::COMPONENT]*=LEVEL
Expand Down
2 changes: 0 additions & 2 deletions c/.gitignore

This file was deleted.

5 changes: 5 additions & 0 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,8 @@ This makes it easier to find blocks that might be source of issues. Usually it i
not required to mark C API functions `unsafe` because they are not intended to
be used from the Rust safe code.

### Git

`.gitignore` file is also used to exclued files going into Docker image
building context. All ignored files should be put into the same `.gitignore` at
the root of the repository by this reason.
8 changes: 0 additions & 8 deletions python/.gitignore

This file was deleted.

1 change: 1 addition & 0 deletions python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dynamic = [

[project.scripts]
metta = "hyperon.metta:main"
metta-py = "hyperon.metta:main"

[project.optional-dependencies]
dev = [
Expand Down
4 changes: 2 additions & 2 deletions repl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ pep440_rs = { version = "0.3.11", optional = true }
hyperon = { workspace = true, optional = true } #TODO: We can only link Hyperon directly or through Python, but not both at the same time. The right fix is to allow HyperonPy to be built within Hyperon, See https://github.com/trueagi-io/hyperon-experimental/issues/283

[[bin]]
name = "metta"
name = "metta-repl"
path = "src/main.rs"

[features]
default = ["python"]
no_python = ["hyperon"]
python = ["pyo3", "pep440_rs"]
minimal = ["hyperon/minimal", "no_python"] #WARNING: The interpreter belonging to the hyperon python module will be used if hyperon is run through python
git = ["hyperon/git"]
git = ["hyperon/git"]

0 comments on commit e027fe2

Please sign in to comment.