DEV Community

Tom Watt
Tom Watt

Posted on • Updated 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.

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:

GitHub logo tomowatt / unity-docker-example

Example of running Unity WebGL within Docker


Game Start Screen

An example of how to run a Unity WebGL game using Docker. Although not the most exciting game, it could prove useful to be able to build and share games using Docker.

Run the game:

docker-compose up -d

Then visit localhost:8080 in a Browser.

Stop the game:

docker-compose down

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)

townofdon profile image
Don Juan Javier

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 the rm command from failing if default.conf didn't exist.

My docker-compose.yml:

version: '3.8'

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

My Dockerfile:

FROM nginx:stable

WORKDIR /webgl
COPY webgl/ .
VOLUME /webgl

WORKDIR /etc/nginx/conf.d
RUN rm -f default.conf
COPY webgl.conf webgl.conf
Enter fullscreen mode Exit fullscreen mode
tomowatt profile image
Tom Watt

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!