DEV Community

Tom Watt
Tom Watt

Posted on

Running an Unity WebGL game within Docker

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
Enter fullscreen mode Exit fullscreen mode

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.

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /webgl;
        index  index.html;

    error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
Enter fullscreen mode Exit fullscreen mode

Next part is the Dockerfile itself, putting all the pieces together to host the WebGL game.

FROM nginx:stable

WORKDIR /webgl
COPY webgl/ .

WORKDIR /etc/nginx/conf.d
RUN rm default.conf
COPY webgl.conf webgl.conf
Enter fullscreen mode Exit fullscreen mode

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

version: '3.8'

    build: .
      - "8080:80"
Enter fullscreen mode Exit fullscreen mode

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.

Discussion (0)