From 9d3e9c1a3b0dd761084fc99f7ce22be2a3af87cb Mon Sep 17 00:00:00 2001 From: sergystepanov Date: Tue, 9 Jun 2020 15:38:43 +0300 Subject: [PATCH] Initial Docker rewrite (#190) * Initial Docker rewrite * Add rebuild * Up * Up * Up * Pre-release * Pack assets with ext games * Remove Makefile Docker because it's broken on Windows and a dup * Add default container name * Update docker-compose.yml * Add app binaries into the system bin folder * Update * Update --- .dockerignore | 16 +++++++++++++--- .env | 1 + Dockerfile | 48 ++++++++++++++++++++++++++++++++++++---------- Makefile | 18 +++++------------ README.md | 13 +++++++++---- docker-compose.yml | 17 ++++++++++++++++ 6 files changed, 83 insertions(+), 30 deletions(-) create mode 100644 .env create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore index 947490d5..ce9da1b1 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,6 +1,16 @@ -*/assets/games/* -.git +.git/ .gitignore +.github/ +.idea/ + +.env +Dockerfile +docker-compose.yml + LICENSE VERSION -docs +README.md +bin/ +docs/ + +assets/games/ diff --git a/.env b/.env new file mode 100644 index 00000000..45ec980e --- /dev/null +++ b/.env @@ -0,0 +1 @@ +CLOUD_GAME_GAMES_PATH=./assets/games diff --git a/Dockerfile b/Dockerfile index 8c7503fb..16996c59 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,15 +1,43 @@ -From golang:1.12 +# The base cloud-game image +ARG BUILD_PATH=/go/src/github.com/giongto35/cloud-game -RUN apt-get update +# build image +FROM golang:1.14 AS build +ARG BUILD_PATH +WORKDIR ${BUILD_PATH} -RUN apt-get install pkg-config libvpx-dev libopus-dev libopusfile-dev -y +# system libs layer +RUN apt-get update && apt-get install -y \ + make \ + pkg-config \ + libvpx-dev \ + libopus-dev \ + libopusfile-dev \ + && rm -rf /var/lib/apt/lists/* -RUN mkdir -p /cloud-game -COPY . /cloud-game/ -WORKDIR /cloud-game +# go deps layer +COPY go.mod go.sum ./ +RUN go mod download -# Install server dependencies -RUN go install ./cmd/coordinator -RUN go install ./cmd/worker +# app build layer +COPY ./ ./ +RUN make build -EXPOSE 8000 +# base image +FROM debian:10-slim +ARG BUILD_PATH +WORKDIR /usr/local/share/cloud-game + +RUN apt-get update && apt-get install -y \ + libvpx5 \ + libopus0 \ + libopusfile0 \ + && rm -rf /var/lib/apt/lists/* + +COPY --from=build ${BUILD_PATH}/bin/ ./ +# it assumed there are only binary files yet +RUN cp -s $(pwd)/* /usr/local/bin +COPY web ./web +COPY assets/emulator/libretro/cores/*.so ./assets/emulator/libretro/cores/ + +EXPOSE 8000 9000 3478/tcp 3478/udp diff --git a/Makefile b/Makefile index 0679ce4f..88655bf6 100644 --- a/Makefile +++ b/Makefile @@ -76,27 +76,19 @@ dev.run: dev.build-local ./bin/coordinator --v=5 & ./bin/worker --coordinatorhost localhost:8000 -dev.run-docker: - docker build . -t cloud-game-local - docker stop cloud-game-local || true - docker rm cloud-game-local || true - # Coordinator and worker should be run separately. - docker run --privileged -v $(pwd)/games:/cloud-game/games -d --name cloud-game-local -p 8000:8000 -p 9000:9000 cloud-game-local bash -c "coordinator --v=5 & worker --coordinatorhost localhost:8000" - # RELEASE # Builds the app for new release. # # Folder structure: -# release # - assets/ -# - games/ (shared between both executables) -# - emulator/libretro/cores/ (filtered by extension) +# - games/ (shared between both executables) +# - emulator/libretro/cores/ (filtered by extension) # - web/ # - coordinator # - worker # -# params: -# - RELEASE_DIR: the name of the output folder (default: _release). +# Config params: +# - RELEASE_DIR: the name of the output folder (default: release). # - DLIB_TOOL: the name of a dynamic lib copy tool (with params) (e.g., ldd -x -y; defalut: ldd). # - DLIB_SEARCH_PATTERN: a grep filter of the output of the DLIB_TOOL (e.g., mylib.so; default: .*so). # Be aware that this search pattern will return only matched regular expression part and not the whole line. @@ -105,7 +97,7 @@ dev.run-docker: # - DLIB_ALTER: a special flag to use altered dynamic copy lib tool for macOS only. # - CORE_EXT: a file extension of the cores to copy into the release. # -# example: +# Example: # make release DLIB_TOOL="ldd -x" DLIB_SEARCH_PATTERN=/usr/lib.*\\\\s LIB_EXT=so # RELEASE_DIR ?= release diff --git a/README.md b/README.md index 623c5a61..433a672e 100644 --- a/README.md +++ b/README.md @@ -32,10 +32,6 @@ Direct play an existing game: **[Pokemon Emerald](http://cloudretro.io/?id=652e4 5. **Horizontally scaled**: The infrastructure is designed to be able to scale under high traffic by adding more instances. 6. **Cloud storage**: Game state is storing on online storage, so you can come back and continue playing your incomplete game later. -## Run on local by Docker - -You try running the server directly by `make dev.run-docker`. It will spawn a docker environment and you can access the service on `localhost:8000`. - ## Development environment Install Golang https://golang.org/doc/install . Because the project uses GoModule, so it requires Go1.11 version. @@ -76,6 +72,15 @@ Because the coordinator and workers need to run simultaneously. Workers connect * `go run cmd/coordinator/main.go` - spawn coordinator * `go run cmd/worker/main.go --coordinatorhost localhost:8000` - spawn workers connecting to coordinator +## Run with Docker + +In case if you want to run the app as a Docker container, +use `docker-compose up --build` (`CLOUD_GAME_GAMES_PATH` is env variable for games on your host). +It will spawn a docker environment and you can access the service on `localhost:8000`. + +*Note.* +Docker for Windows is not supposed to work with provided configuration, use WSL2 instead. + ## Technical Document - [webrtchacks Blog: Open Source Cloud Gaming with WebRTC](https://webrtchacks.com/open-source-cloud-gaming-with-webrtc/) - [Wiki (outdated)](https://github.com/giongto35/cloud-game/wiki) diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..6916bdda --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,17 @@ +version: '3' +services: + + cloud-game: + build: . + image: cloud-game-local + container_name: cloud-game-local + privileged: true + command: > + bash -c "coordinator --v=5 & worker --coordinatorhost localhost:8000" + ports: + - "8000:8000" + - "9000:9000" + - "3478:3478/tcp" + - "3478:3478/udp" + volumes: + - ${CLOUD_GAME_GAMES_PATH}:/usr/local/share/cloud-game/assets/games