From 5ad4ad7cc2d334da54251fcefa7d3f7f42e82296 Mon Sep 17 00:00:00 2001 From: Garen Tyler Date: Thu, 1 May 2025 20:48:25 -0600 Subject: [PATCH] Simplify build process and improve README --- Dockerfile | 16 ++---- README.md | 120 ++++++++++++++++++++++++++++++++++++++------- docker-bake.hcl | 1 - docker-compose.yml | 39 +-------------- 4 files changed, 108 insertions(+), 68 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4eea673..2feaab9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,20 +2,12 @@ FROM rust:1.86.0-alpine3.21 AS base RUN apk add --no-cache musl-dev git RUN cargo install cargo-chef --locked --version 0.1.71 WORKDIR /app -RUN git config --global --add safe.directory /app ARG FEATURES RUN addgroup --gid 25565 --system composition && adduser --uid 25565 --system --ingroup composition --home /app composition RUN chown -R composition:composition /app RUN chown -R composition:composition /usr/local/cargo USER composition - -FROM base AS dev -RUN cargo install cargo-watch --locked --version 8.5.3 -VOLUME /app -VOLUME /app/.git -EXPOSE 25565 -ENTRYPOINT ["cargo", "watch", "-x"] -CMD ["run -- server"] +RUN git config --global --add safe.directory /app FROM base AS planner COPY Cargo.toml . @@ -33,12 +25,12 @@ COPY .git .git RUN cargo build --release --no-default-features --features $FEATURES RUN strip target/release/composition -FROM alpine:3.21 AS prod +FROM alpine:3.21 RUN apk add --no-cache tini RUN addgroup --gid 25565 --system composition && adduser --uid 25565 --system --ingroup composition --home /app composition VOLUME /app/data WORKDIR /app/data -COPY --from=builder /app/target/release/composition /app +COPY --from=builder /app/target/release/composition /usr/bin EXPOSE 25565 USER composition -ENTRYPOINT ["tini", "--", "/app/composition"] +ENTRYPOINT ["tini", "--", "/usr/bin/composition"] diff --git a/README.md b/README.md index 76fc29d..27eabe4 100644 --- a/README.md +++ b/README.md @@ -1,33 +1,117 @@ -# Composition +# 📓 Composition Composition is a new Minecraft server written from the ground-up in Rust. Composition is targeting Minecraft version 1.21.1, protocol version 767. -The main goal is to get a working server, then optimize for speed (multi-threading/kubernetes/etc). +The main goal is to get a working server, then optimize for speed +(multi-threading/kubernetes/etc). Composition is my personal project to learn more about networking, -game development, and Rust and is not currently intended for production use. +game development, Rust, Docker, and CI/CD pipelines. +Composition is not currently intended for production use. I want to use it as a platform to experiment with ideas about how a Minecraft server could work, such as a tickless design, one that could run across a distributed kubernetes cluster, or a world as a database that's accessed by multiple servers. -## Getting Started +## 🚀 Getting Started -- The only prerequisite for running Composition is to have Docker and Docker Compose installed. -- Clone the project: `git clone https://github.com/garentyler/composition.git` -- Run it in proxy mode: `docker compose --profile proxy up` +- The only prerequisite for running Composition is to have Docker installed. +- Pull the latest version of the Docker image: + `docker pull git.garen.dev/garentyler/composition:latest` +- Make sure a folder exists for the it to store its data. The uid/gid of the + process inside the container is `25565:25565`, so make sure the folder + is writable by that user. - Proxy mode will start an instance of `itzg/minecraft-server` on port 25565 and - the Composition proxy in 25566. This is useful for testing packet parsing and - handling. -- Run it in server mode: `docker compose --profile server up` + To get the right file permissions, you can run the following: - Server mode starts Composition on port 25566. - This is useful for testing the server logic and world generation. -- To build release images, use `docker buildx bake -f docker-bake.hcl`. - This will create a multi-arch image that can be run on amd64 and arm64. + ```sh + mkdir -p composition-server + sudo chown -R 25565:25565 composition-server + sudo chmod -R 775 composition-server + ``` -## Project Structure +### đŸ•šī¸ Running Composition as a Server + +Standalone command: + +```sh +docker run -it -v .:/app/data --network host git.garen.dev/garentyler/composition server +``` + +Example docker-compose.yml file: + +```yml +services: + # Composition running as a server. + composition-server: + image: git.garen.dev/garentyler/composition:latest + container_name: composition-server + restart: unless-stopped + ports: + - "25565:25565" + volumes: + - ./composition-server:/app/data + command: server +``` + +### 🌐 Running Composition as a Proxy + +Standalone command: + +```sh +docker run -it -v .:/app/data --network host git.garen.dev/garentyler/composition proxy +``` + +Example docker-compose.yml file: + +```yml +services: + # Example Minecraft server to run as a reference. + reference: + image: itzg/minecraft-server + container_name: reference + restart: unless-stopped + ports: + - "25565:25565" + environment: + EULA: "TRUE" + VERSION: "1.21.1" + # Composition running as a proxy. + composition-proxy: + image: git.garen.dev/garentyler/composition:latest + container_name: composition-proxy + restart: unless-stopped + depends_on: + reference: + condition: service_healthy + restart: true + ports: + - "25566:25566" + volumes: + - ./composition-proxy:/app/data + command: proxy -U reference +``` + +## đŸ› ī¸ Building From Source + +To build Composition from source, you will need to have Rust and Cargo installed. +The stable toolchain is recommended, but the nightly toolchain may be required for some features. + +```sh +cargo build --release +``` + +To build the Docker image, you will need to have Docker configured with Bake. + +```sh +# To build both linux/amd64 and linux/arm64 images, use the following command: +docker buildx bake -f docker-bake.hcl + +# To build only the linux/amd64 image, use the following command: +docker buildx bake -f docker-bake.hcl --set default.platform=linux/amd64 +``` + +## đŸ—ī¸ Project Structure Composition is built using the async runtime and networking from Tokio. On startup, Composition sets up logging, reads the configuration, and @@ -36,7 +120,7 @@ start in (server, proxy, world, etc). The subcommand performs any startup tasks, such as loading the world, and then begins the main event loop. -### Cargo Features +### đŸĻ€ Cargo Features Composition has a non-optional core and multiple cargo features that can be enabled or disabled to configure functionality. @@ -46,7 +130,7 @@ The `proxy` feature enables the `composition proxy` command, which runs a proxy The `world` feature is not yet implemented. When finished, it will host a world server that can be accessed by multiple server cores. -## Useful Resources +## 📋 Useful Resources - [wiki.vg archive](https://minecraft.wiki/w/Minecraft_Wiki:Projects/wiki.vg_merge) (now merged with the Minecraft wiki) - [Protocol Specification](https://minecraft.wiki/w/Java_Edition_protocol) diff --git a/docker-bake.hcl b/docker-bake.hcl index 0ce4631..7dc1724 100644 --- a/docker-bake.hcl +++ b/docker-bake.hcl @@ -18,7 +18,6 @@ target "default" { "${REGISTRY}/composition:${RELEASE_VERSION}", "${REGISTRY}/composition:${GITHUB_SHA}" ] - target = "prod" args = { FEATURES = "server,proxy" } diff --git a/docker-compose.yml b/docker-compose.yml index e05cf71..6d1f8f1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,47 +1,12 @@ -# This file is only meant to provide a simple dev environment. -# For prod images, look at docker-bake.hcl. +# This file is only meant to provide a convenient dev environment. +# For prod images, look at docker-bake.hcl and README.md. services: - server: - container_name: composition - restart: unless-stopped - profiles: [server] - build: - context: . - dockerfile: Dockerfile - target: dev - command: [ "run -- server" ] - ports: - - "25566:25565" - volumes: - - .:/app - - .git:/app/.git reference: container_name: reference restart: unless-stopped - profiles: [server, proxy] image: itzg/minecraft-server ports: - "25565:25565" environment: EULA: "TRUE" VERSION: "1.21.1" - proxy: - container_name: composition-proxy - restart: unless-stopped - profiles: [proxy] - build: - context: . - dockerfile: Dockerfile - target: dev - depends_on: - reference: - condition: service_healthy - restart: true - command: [ "run -- proxy -U reference -l trace" ] - ports: - - "25566:25566" - volumes: - - .:/app - - .git:/app/.git - environment: - CARGO_HOME: "/app/.cargo"