So whilst learning on how to make Unity games, I got curious of what would be a good way to test, share and get feedback on.
There are various websites that will host the games for you but I like the idea of making a game that’s Open for people to contribute to, learn from and ultimately play.
So this is where I went down the path of looking into using Docker to host a WebGL game. After searching the web I came across a few others that looked to do something similar.
So to start, I've built and exported the Unity game and kept a simple file structure.
.
├── Dockerfile
├── docker-compose.yaml
├── webgl
└── webgl.conf
This allowed me to easily copy necessary files with a single COPY
within the Dockerfile.
To host game within Docker and keeping things simple, I've used Nginx as the base image as the HTML files only needed served.
But the default configuration needed to be updated to point to the copied files. This resulted in the following for the Nginx configuration, just using the index.html
created by Unity and update the location root to where the files were copied to.
Next part is the Dockerfile itself, putting all the pieces together to host the WebGL game.
Finally, using Docker Compose, I can finally launch the Docker image and play the game within a browser with a single docker-compose -d up
All the code can be found here:
Hope this helps anyone who is curious about doing a similar thing and I'll hope to improve this as I learn more about Unity, WebGL and Docker.
2022 Update: Add linked code examples via gists and embedded repository, updated Dockerfile code
Top comments (2)
Very helpful article, thanks! I wanted to test a Unity WebGL build and this pointed me in the right direction.
I ended up using a volume for the
webgl
directory so that changes to the build would be reflected immediately without needing to rebuild the docker image.Also had to disable compression as I couldn't get the nginx config to serve
*.gz
files correctly. Some people are saying that server-side compression doesn't play nice with pre-compressed build files - (see this link).Lastly, had to supply the
-f
flag to keep therm
command from failing ifdefault.conf
didn't exist.My docker-compose.yml:
My Dockerfile:
I’m glad to hear this helped someone!
The article and setup is probably a bit out of date now and could do with a bit of a refresh. I’ll try and look into it again as I’ve recently done so more work with Unity.
But thank you also for sharing your configuration and the issues you met!