PDA

View Full Version : [CoD2] Setup CoD2 with Docker



Lonsofore
24th February 2019, 20:34
https://1000logos.net/wp-content/uploads/2017/07/Docker-Logo-500x148.png

About

Hello all. I want to show you how we can use CoD2 server in Docker container.

I tried IzNoGoD's method, I wrote my own and after some time I started to use Docker at home and at work then, in production. At this moment, I run 2 my CoD2 servers in Docker. Now I want to share my experience to Killtube.

Advantages of this method:

In my opinion, this method is much easier than IzNoGoD's (https://killtube.org/showthread.php?2454-CoD2-Setup-CoD2-on-your-ubuntu-14-04-server);
And much more stable than my old one (https://killtube.org/showthread.php?2873-CoD2-Install-CoD2-on-your-VDS-much-faster!);
All CoD2 versions;
In this method you don't have to use specific OS (e.g. Ubuntu 14.04). You can use even Windows and still have libcod;
You don't have to install any of CoD2 requirements to your system;
Libcod already installed. If you have to use specific version or repo - you can change it in arguments;
Last cod2_lnxded (https://killtube.org/showthread.php?1719-CoD2-Latest-cod2-linux-binaries-(1-0-1-2-1-3)) versions installed;
Auto-restarting server on crash;
Server health monitoring (with getinfo);
Logging.


And before we start: all steps here I write for Linux. If you really want to have CoD2 server on Windows - there's no really much job, but all these steps you should convert by yourself.

So, let's start!


Requirements

Docker and Docker-Compose


Docker is a computer program that performs operating-system-level virtualization, also known as "containerization". Docker is used to run software packages called "containers". Containers are isolated from each other and bundle their own application, tools, libraries and configuration files; they can communicate with each other through well-defined channels. All containers are run by a single operating-system kernel and are thus more lightweight than virtual machines. Containers are created from "images" that specify their precise contents. Images are often created by combining and modifying standard images downloaded from public repositories. (c) Wikipedia



Compose is a tool for defining and running multi-container Docker applications. (c) Docker documentation


First of all, let's install Docker:


sudo curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh


And Docker-Compose:


sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose


If you don't want to always write sudo to use Docker - give a permissions to your user:


sudo usermod $(whoami) -aG docker



Files

I left my docker image almost empty (for legal reasons also), so you have to upload these files to your server:

main folder of your cod2
fs_game of your server
your maps (optional)


You can put it anywhere you want on your server. For example: I put it like this (~ is a home dir):

~/cod2/main
~/cod2/myserver
~/cod2/Library



Get CoD2 image

Docker image - is like a template for containers. You can pull it once and make a lot of containers on it. So you can save a lot of disk space on it.

You have 2 ways how to get the image:

I created a repo in Docker Hub (https://hub.docker.com/r/lonsofore/cod2), so you can pull the image from the web.
I also created a GitHub repo (https://github.com/Lonsofore/cod2docker), where you can get a Dockerfile to create a Docker image by yourself.


If you more like the first way - no need to do now, pass this stage and move to the next one.

If you want to build your own image (with arguments, maybe), let's zoom in it:

Clone the repo
Edit build.sh file with your parameters (you can look available parameters on the top of Dockerfile)
Execute it!



Setup container

Now we will setup our container (server) with a compose file. Create a file called docker-compose.yml (or just copy it from my repo). It's a basic text config file. Put there this text and then we will change it on your needs:



# example for 1.0 cod2 server
version: '3.7'
services:
myserver:
image: lonsofore/cod2:1.0
container_name: myserver
restart: always
stdin_open: true
tty: true
network_mode: host
volumes:
- ~/cod2/main:/cod2/main
- ~/cod2/myserver:/cod2/myserver
- ~/cod2/Library:/cod2/.callofduty2/server/Library
environment:
PARAMS: "+set fs_homepath /cod2/.callofduty2/ +set fs_game myserver +set dedicated 2 +set net_port 28960 +exec myserver.cfg"
CHECK_PORT: 28960


In this file you should change only 2 things:

Volumes. It's a folders or files which would be available from the container. Format: local:container. You can see the file structure that I used in the example above. Change it on your needs. If you don't have custom maps - you can delete a line with Library.
Environment. It's your server settings. PARAMS is what would be executed by your server. Change fs_game, port and server cfg name to yours there. CHECK_PORT - also your server port, it used to monitore container health.



Run

Ok, after all these changes we're ready to start our container.

Write in console: docker-compose up -d

If everything is ok, you can see your container in containers list. Write: docker ps

In STATUS field you can see container's health. After one or two minutes it should be 'healthy'.

To see server logs write: docker logs ID

To attach your container and use server console copy your server's ID from list (first column) and write: docker attach ID

To deattach press Ctrl+P and then Ctrl+Q.


Advanced part (optional)

Your own Docker image

If your server has some system requirements (like a apt libs) you can create your own image based on mine. It's very easy! For example, I want to show you a Dockerfile of my own server:



FROM lonsofore/cod2:1.0

# vodka requirements
RUN apt-get update \
&& apt-get install -y \
python3 \
python3-pip \
geoip-bin \
&& rm -rf /var/lib/apt/lists \
&& pip3 install geoip2


All you need is just install your requirements. In my case, I leave ENTRYPOINT command and use it from the base image. If you want to change it for some reason - copy it from base image to preload libcod.


Server restarts

There are two types of restarts:


If CoD2 would stop for some reason (e.g. ShutdownGame) - Docker will restart it automatically ('restart' part in docker-compose.yml)
If for some reason CoD2 would still run, but health check (you can look that part in Dockerfile. it's just a getinfo from the server, which takes each 5 seconds 3 times) would fail (e.g. server just freezed) - docker container would be marked as 'unhealthy'. But! In this case, the container wouldn't restart automatically - there is no such functionality in Docker. In this case you should use some tool, which would process it. For my server I use a docker image called autoheal (https://hub.docker.com/r/willfarrell/autoheal/). An example of my docker-compose.yml for autoheal:


version: '3.7'
services:
autoheal:
image: willfarrell/autoheal
container_name: autoheal
restart: always
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
AUTOHEAL_CONTAINER_LABEL: "all"

AUTOHEAL_CONTAINER_LABEL with value "all" means that all unhealthy containers would be restarted.

So, your server should be up even after freeze or something like.


Logging

As I said before, you always can look a full log via 'docker logs <container id>'. But if you also want to see a log file, you can find it here:

/var/lib/docker/containers/<container id>/<container id>-json.log


Credits

In this part I just want to say thank you to kung foo man and IzNoGoD. They helped me a lot in Killtube Discord chat.


Links

DockerHub (https://hub.docker.com/r/lonsofore/cod2)
GitHub (https://github.com/Lonsofore/cod2docker)

Ni3ls
25th February 2019, 21:41
Where is the logging and auto restart part? How do u manage that?

maxdamage99
26th February 2019, 16:55
Auto-restart after CoD2 engine errors (==== ShutdownGame ====) too or only after CoD2 process crash?

Lonsofore
27th February 2019, 21:08
Agree, I missed these parts. Few words about restarts:


If CoD2 would stop for some reason (e.g. ShutdownGame, as Damage said) - Docker will restart it automatically ('restart' part in docker-compose.yml)
If for some reason CoD2 would still run, but health check (you can look that part in Dockerfile. it's just a getinfo from the server, which takes each 5 seconds 3 times) would fail (e.g. server just freezed) - docker container would be marked as 'unhealthy'. But! In this case, the container wouldn't restart automatically - there is no such functionality in Docker. In this case you should use some tool, which would process it. For my server I use a docker image called autoheal (https://hub.docker.com/r/willfarrell/autoheal/). An example of my docker-compose.yml for autoheal:


version: '3.7'
services:
autoheal:
image: willfarrell/autoheal
container_name: autoheal
restart: always
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
AUTOHEAL_CONTAINER_LABEL: "all"

AUTOHEAL_CONTAINER_LABEL with value "all" means that all unhealthy containers would be restarted.

So, your server should be up even after freeze or something like.


About logging:
As I said before, you always can look a full log via 'docker logs <container id>'. But if you also want to see a log file, you can find it here:
/var/lib/docker/containers/<container id>/<container id>-json.log


I pinned this answer to the head, in Advanced part.

IzNoGoD
27th February 2019, 21:40
Merge support for https://pterodactyl.io/ maybe?

(I'm not using it, but seems like the best free game panel on the market today, using docker)

Lonsofore
27th February 2019, 22:00
Merge support for https://pterodactyl.io/ maybe?

(I'm not using it, but seems like the best free game panel on the market today, using docker)

Well, looks interesting, but:
1. What do you mean with "merge support"?
2. It's a panel to manage games in Docker containers, with a lot of dependencies, but... it isn't in Docker container. That's strange for me.

P.S. GUI is really nice

Lonsofore
26th April 2019, 22:45
Update.

* Now you don't have to download cod2_lnxded - it's included to image.
* Moved from i386 ubuntu architecture to amd64. Did it only because of child images with different programs (like a image of my server).

Lonsofore
9th May 2020, 00:41
Up!

Optimized Docker layers;
Extended healthcheck retries (CaptainSlow (https://killtube.org/member.php?986-CaptainSlow) had some errors with false-positive healthchecks on map-restart);
Updated libcod version to latest;
Uploaded all version to Docker Hub (https://hub.docker.com/r/lonsofore/cod2)!

CaptainSlow
1st September 2020, 13:57
Heads up: Docker is making changes to their image retention policy on Docker Hub. Inactive images, defined as images that have not been pulled or pushed in 6 months, will be deleted from Docker Hub starting November 1, 2020.
So please make sure to (automatically) rebuild your image at least every 6 months or nobody will be able to pull the image anymore..... which would be a shame, because it's a great image :)

Lonsofore
19th September 2020, 03:12
Heads up: Docker is making changes to their image retention policy on Docker Hub. Inactive images, defined as images that have not been pulled or pushed in 6 months, will be deleted from Docker Hub starting November 1, 2020.
So please make sure to (automatically) rebuild your image at least every 6 months or nobody will be able to pull the image anymore..... which would be a shame, because it's a great image :)

Thank you for the information! I think, it's easier to make a cron-task to pull the image every month. Actually, even without the image on Docker Hub it's always possible to build it on your own machine with the Github repo.

Lonsofore
22nd December 2020, 10:51
Good news, everyone! Few updates:

Started to use semver (https://semver.org/) in package versions
Added environment variables parsing. Now you could use COD2_SET_ prefix to set cvar! E.g. COD2_SET_net_port=28960. The same with COD2_SETA_ and COD2_SETS_, feel free to use. But don't use "_" and "=" in names and values. If you really need - use PARAMS env.
Now you don't have to specify CHECK_PORT - it would be parsed from envs or params string. But it you want, you still can - backwards compatibility!
Github Actions! Now all builds automatically upload to DockerHub (https://hub.docker.com/repository/docker/lonsofore/cod2) and Github Packages (https://github.com/Lonsofore/cod2docker/packages/548649), so don't worry about DockerHub limits.