Separation of concerns for docker files and building (#41)

* Separation of concerns for docker files and building

* Separated out readme context
This commit is contained in:
Michael
2021-02-10 18:06:42 -07:00
committed by GitHub
parent 60d77d22c6
commit cb31cd0675
11 changed files with 240 additions and 212 deletions

View File

@@ -31,11 +31,71 @@ jobs:
run: |
docker-compose --file docker-compose.yml build
# Push image to GitHub Packages.
# See also https://docs.docker.com/docker-hub/builds/
push:
# Ensure test job passes before pushing image.
push_odin:
needs: test
runs-on: ubuntu-latest
if: github.event_name == 'push'
steps:
- uses: actions/checkout@v2
- name: Build image
run: |
docker build . --file Dockerfile --tag "${GITHUB_IMAGE_NAME}-odin" --tag "${DOCKER_IMAGE_NAME}-odin"
- name: Log into registry
run: |
echo "${{ github.token }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin
echo "${{ secrets.DOCKER_TOKEN }}" | docker login registry.hub.docker.com -u mbround18 --password-stdin
- name: Push Image to DockerHub
run: |
IMAGE_ID=registry.hub.docker.com/$DOCKER_IMAGE_NAME-odin
# Change all uppercase to lowercase
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
# Strip git ref prefix from version
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
# Strip "v" prefix from tag name
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
# Use Docker `latest` tag convention
[ "$VERSION" == "main" ] && VERSION=latest
echo IMAGE_ID=$IMAGE_ID
echo VERSION=$VERSION
docker tag "${DOCKER_IMAGE_NAME}-odin" $IMAGE_ID:$VERSION
docker push $IMAGE_ID:$VERSION
- name: Push Image to Github
run: |
IMAGE_ID=docker.pkg.github.com/${{ github.repository }}/$GITHUB_IMAGE_NAME-odin
# Change all uppercase to lowercase
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
# Strip git ref prefix from version
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
# Strip "v" prefix from tag name
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
# Use Docker `latest` tag convention
[ "$VERSION" == "main" ] && VERSION=latest
echo IMAGE_ID=$IMAGE_ID
echo VERSION=$VERSION
docker tag "${GITHUB_IMAGE_NAME}-odin" $IMAGE_ID:$VERSION
docker push $IMAGE_ID:$VERSION
push_valheim:
# Ensure test job passes before pushing image.
needs: push_odin
runs-on: ubuntu-latest
if: github.event_name == 'push'

View File

@@ -1,19 +1,13 @@
# ------------------ #
# -- Odin Builder -- #
# ------------------ #
FROM registry.hub.docker.com/library/rust:latest as RustBuilder
WORKDIR /data/odin
COPY . .
RUN cargo install --path . \
&& cargo build --release
FROM mbround18/valheim-odin:latest as RustBuilder
# ----------------------- #
# -- Script Formatting -- #
# ----------------------- #
FROM registry.hub.docker.com/library/alpine:latest as ScriptSanitize
FROM alpine:latest as ScriptSanitize
WORKDIR /data/scripts
COPY src/scripts/* ./
@@ -21,11 +15,10 @@ COPY src/scripts/* ./
RUN apk add dos2unix --update-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ --allow-untrusted \
&& dos2unix /data/scripts/**
# --------------- #
# -- Steam CMD -- #
# --------------- #
FROM registry.hub.docker.com/cm2network/steamcmd:root
FROM cm2network/steamcmd:root
RUN apt-get update \
&& apt-get install -y \
@@ -55,7 +48,7 @@ ENV PASSWORD "12345"
ENV AUTO_UPDATE "0"
COPY --from=ScriptSanitize --chmod=755 /data/scripts/*.sh /home/steam/scripts/
COPY --from=ScriptSanitize --chmod=755 /data/scripts/init.sh /init.sh
COPY --from=ScriptSanitize --chmod=755 /data/scripts/entrypoint.sh /entrypoint.sh
COPY --from=RustBuilder --chmod=755 /data/odin/target/release /home/steam/.odin
#WORKDIR /home/steam/valheim
@@ -66,5 +59,5 @@ RUN usermod -u ${PUID} steam \
&& groupmod -g ${PGID} steam \
&& chsh -s /bin/bash steam
ENTRYPOINT ["/bin/bash","/init.sh"]
CMD ["/bin/bash", "/home/steam/scripts/entrypoint.sh"]
ENTRYPOINT ["/bin/bash","/entrypoint.sh"]
CMD ["/bin/bash", "/home/steam/scripts/start_valheim.sh"]

11
Dockerfile.odin Normal file
View File

@@ -0,0 +1,11 @@
# ------------------ #
# -- Odin Builder -- #
# ------------------ #
FROM rust:latest as RustBuilder
WORKDIR /data/odin
COPY . .
RUN cargo install --path . \
&& cargo build --release

View File

@@ -47,52 +47,11 @@ services:
- ./valheim/server:/home/steam/valheim
```
## Odin
Odin is a CLI tool utilized for installing, starting, and stopping [Valheim] servers
### Gotchas
### [Odin]
- Odin relies on Rust. [Please install Rust](https://www.rust-lang.org/tools/install)
- Odin also assumes that you have SteamCMD already installed. [Install instructions for SteamCMD.](https://developer.valvesoftware.com/wiki/SteamCMD)
- If you have the proper build tools installed you should be able to run Odin on any system.
- Current Supported Architecture: Unix & Linux based systems.
### Installation
> Make sure you have build essentials installed before you install this crate
```sh
cargo install --git https://github.com/mbround18/valheim-docker.git --branch main
```
### Usage
![Main Menu](./docs/assets/main-menu.png)
#### Install Valheim
```sh
odin install
```
![Install Menu](./docs/assets/install-menu.png)
#### Start Valheim
```sh
odin start
```
![Start Menu](./docs/assets/start-menu.png)
#### Stop Valheim
```sh
odin stop
```
![Install Menu](./docs/assets/stop-menu.png)
This repo has a CLI tool called [Odin] in it! It is used for managing the server inside the container. If you are looking for instructions for it click here: [Odin]
## Versions:
@@ -128,6 +87,7 @@ odin stop
[//]: <> (Links below...................)
[Odin]: ./docs/odin.md
[Valheim]: https://www.valheimgame.com/
[//]: <> (Image Base Url: https://github.com/mbround18/valheim-docker/blob/main/docs/assets/name.png?raw=true)

View File

@@ -1,6 +1,13 @@
version: "3"
services:
odin:
image: mbround18/valheim-odin:latest
build:
context: .
dockerfile: ./Dockerfile.odin
valheim:
depends_on:
- odin
image: mbround18/valheim:latest
environment:
- PORT=30017

46
docs/odin.md Normal file
View File

@@ -0,0 +1,46 @@
# Odin
Odin is a CLI tool utilized for installing, starting, and stopping [Valheim] servers
## Gotchas
- Odin relies on Rust. [Please install Rust](https://www.rust-lang.org/tools/install)
- Odin also assumes that you have SteamCMD already installed. [Install instructions for SteamCMD.](https://developer.valvesoftware.com/wiki/SteamCMD)
- If you have the proper build tools installed you should be able to run Odin on any system.
- Current Supported Architecture: Unix & Linux based systems.
## Installation
> Make sure you have build essentials installed before you install this crate
```sh
cargo install --git https://github.com/mbround18/valheim-docker.git --branch main
```
## Usage
![Main Menu](./assets/main-menu.png)
#### Install Valheim
```sh
odin install
```
![Install Menu](./assets/install-menu.png)
### Start Valheim
```sh
odin start
```
![Start Menu](./assets/start-menu.png)
### Stop Valheim
```sh
odin stop
```
![Install Menu](./assets/stop-menu.png)

View File

@@ -1,57 +1,53 @@
#!/usr/bin/env bash
cd /home/steam/valheim || exit 1
STEAM_UID=${PUID:=1000}
STEAM_GID=${PGID:=1000}
ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ >/etc/timezone
# Configure ENV
. /home/steam/scripts/load_env.sh
initialize () {
echo "
echo "
###########################################################################
Valheim Server - $(date)
STEAM_UID ${STEAM_UID} - STEAM_GUID ${STEAM_GID}
$1
Initializing your container...
###########################################################################
"
"
log() {
echo "[Valheim][root]: $1"
}
log () {
echo "[Valheim][steam]: $1"
}
# shellcheck disable=SC2039
if [ "${EUID}" -ne 0 ]; then
log "Please run as root"
exit
fi
initialize "Installing Valheim via Odin..."
log "Switching UID and GID"
# shellcheck disable=SC2086
usermod -u ${PUID} steam || echo "Looks like no changes were needed to the user!"
# shellcheck disable=SC2086
groupmod -g ${PGID} steam || echo "Looks like no changes were needed to the user!"
export SteamAppId=892970
export PATH="/home/steam/.odin:$PATH"
log "Setting up file systems"
STEAM_UID=${PUID:=1000}
STEAM_GID=${PGID:=1000}
mkdir -p /home/steam/valheim
# Setting up server
log "Running Install..."
odin install || exit 1
echo "
# Load Valheim base directory,
cd /home/steam/valheim
" > /home/steam/.bashrc
log "Herding Cats..."
log "Starting server..."
chown -R ${STEAM_UID}:${STEAM_GID} /home/steam/valheim
mkdir -p /home/steam/scripts
chown -R ${STEAM_UID}:${STEAM_GID} /home/steam/scripts
mkdir -p /home/steam/valheim
echo "export PATH=\"/home/steam/.odin:$PATH\"" >>/home/steam/.bashrc
cp /home/steam/steamcmd/linux64/steamclient.so /home/steam/valheim
chown -R ${STEAM_UID}:${STEAM_GID} /home/steam/
chown -R ${STEAM_UID}:${STEAM_GID} /home/steam/valheim
odin start || exit 1
# Launch run.sh with user steam (-p allow to keep env variables)
log "Launching as steam..."
cd /home/steam/valheim || exit 1
trap 'cleanup' INT TERM EXIT
trap 'exec goso steam cd /home/steam/valheim && odin stop' INT TERM EXIT
cleanup() {
log "Halting server! Received interrupt!"
odin stop
exit
}
initialize "
Valheim Server Started...
Keep an eye out for 'Game server connected' in the log!
(this indicates its online without any errors.)
" >> /home/steam/valheim/output.log
tail -f /home/steam/valheim/output.log
while :; do
sleep 1s
done
exec gosu steam "$@"

View File

@@ -1,84 +0,0 @@
#!/usr/bin/env bash
ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ >/etc/timezone
echo "
###########################################################################
Valheim Server - $(date)
Initializing your container...
###########################################################################
"
log() {
echo "[Valheim][root]: $1"
}
# shellcheck disable=SC2039
if [ "${EUID}" -ne 0 ]; then
log "Please run as root"
exit
fi
log "Switching UID and GID"
# shellcheck disable=SC2086
usermod -u ${PUID} steam
# shellcheck disable=SC2086
groupmod -g ${PGID} steam
log "Setting up file systems"
STEAM_UID=${PUID:=1000}
STEAM_GID=${PGID:=1000}
mkdir -p /home/steam/valheim
echo "
# Load preset env from Dockerfile.
. /home/steam/scripts/load_env.sh > /dev/null 2>&1 &
# Load Valheim base directory,
cd /home/steam/valheim
" > /home/steam/.bashrc
chown -R ${STEAM_UID}:${STEAM_GID} /home/steam/valheim
mkdir -p /home/steam/scripts
chown -R ${STEAM_UID}:${STEAM_GID} /home/steam/scripts
mkdir -p /home/steam/valheim
echo "export PATH=\"/home/steam/.odin:$PATH\"" >>/home/steam/.bashrc
cp /home/steam/steamcmd/linux64/steamclient.so /home/steam/valheim
chown -R ${STEAM_UID}:${STEAM_GID} /home/steam/
chown -R ${STEAM_UID}:${STEAM_GID} /home/steam/valheim
# Launch run.sh with user steam (-p allow to keep env variables)
log "Launching as steam..."
cd /home/steam/valheim || exit 1
write_env_var() {
env_name="$1"
# shellcheck disable=SC2039
VARIABLE_VALUE=$(printf '%s\n' "${!env_name}" | tr -d '"')
echo "Writing $1 to env file..."
if [[ $2 = true ]]; then
echo "${env_name}=\"${VARIABLE_VALUE}\"" >> /home/steam/.env
else
echo "${env_name}=${VARIABLE_VALUE}" >> /home/steam/.env
fi
}
echo "" >/home/steam/.env
write_env_var "PORT"
write_env_var "NAME" true
write_env_var "WORLD" true
write_env_var "PUBLIC"
write_env_var "PASSWORD" true
write_env_var "AUTO_UPDATE" true
trap 'cleanup' INT TERM EXIT
cleanup() {
echo "Running Cleanup!....."
cd /home/steam/valheim || exit 1
gosu ${STEAM_UID} "odin stop"
exit 0
}
gosu ${STEAM_UID} "$@"

View File

@@ -1,25 +0,0 @@
#!/usr/bin/env sh
echo "
Loading ENV.....
"
export "$(grep ^PORT= /home/steam/.env)"
export "$(grep ^NAME= /home/steam/.env)"
export "$(grep ^WORLD= /home/steam/.env)"
export "$(grep ^PUBLIC= /home/steam/.env)"
export "$(grep ^PASSWORD= /home/steam/.env)"
export "$(grep ^AUTO_UPDATE= /home/steam/.env)"
echo "
Variables loaded.....
Port: ${PORT}
Name: ${NAME}
World: ${WORLD}
Public: ${PUBLIC}
Password: (REDACTED)
Auto Update: ${AUTO_UPDATE}
"

View File

@@ -1,5 +1,4 @@
#!/usr/bin/env bash
. /home/steam/scripts/load_env.sh
echo "
Shutting down the server.........
"

View File

@@ -0,0 +1,65 @@
#!/usr/bin/env bash
cd /home/steam/valheim || exit 1
STEAM_UID=${PUID:=1000}
STEAM_GID=${PGID:=1000}
initialize () {
echo "
###########################################################################
Valheim Server - $(date)
STEAM_UID ${STEAM_UID} - STEAM_GUID ${STEAM_GID}
$1
###########################################################################
"
}
log () {
echo "[Valheim][steam]: $1"
}
initialize "Installing Valheim via Odin..."
echo "
Variables loaded.....
Port: ${PORT}
Name: ${NAME}
World: ${WORLD}
Public: ${PUBLIC}
Password: (REDACTED)
Auto Update: ${AUTO_UPDATE}
"
export SteamAppId=892970
export PATH="/home/steam/.odin:$PATH"
# Setting up server
log "Running Install..."
odin install || exit 1
log "Herding Cats..."
log "Starting server..."
odin start || exit 1
trap 'cleanup' INT TERM EXIT
cleanup() {
log "Halting server! Received interrupt!"
odin stop
exit
}
initialize "
Valheim Server Started...
Keep an eye out for 'Game server connected' in the log!
(this indicates its online without any errors.)
" >> /home/steam/valheim/output.log
tail -f /home/steam/valheim/output.log
while :; do
sleep 1s
done