Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Tom Watt
Tom Watt

Posted on • Edited 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 modeExit fullscreen mode

This allowed me to easily copy necessary files with a singleCOPY 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 theindex.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 singledocker-compose -d up

All the code can be found here:

GitHub logo tomowatt / unity-docker-example

Example of running Unity WebGL within Docker

unity-docker-example

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 visitlocalhost: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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
townofdon profile image
Don Juan Javier
Coder, Digital Creator, Musician, Aspiring Game Dev
  • Work
    Senior Software Engineer
  • Joined

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 thewebgl 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:

version: '3.8'services:  webgl:    build: .    ports:      - "8080:80"    volumes:      - ./webgl:/webgl
Enter fullscreen modeExit fullscreen mode

My Dockerfile:

FROM nginx:stableWORKDIR /webglCOPY webgl/ .VOLUME /webglWORKDIR /etc/nginx/conf.dRUN rm -f default.confCOPY webgl.conf webgl.conf
Enter fullscreen modeExit fullscreen mode
CollapseExpand
 
tomowatt profile image
Tom Watt
Tech Tinkerer.Automagically building and trying out too much.
  • Location
    Edinburgh
  • Education
    The Open University
  • Pronouns
    he/him
  • Work
    Senior Support Engineer @ Buildkite
  • Joined

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!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Tech Tinkerer.Automagically building and trying out too much.
  • Location
    Edinburgh
  • Education
    The Open University
  • Pronouns
    he/him
  • Work
    Senior Support Engineer @ Buildkite
  • Joined

More fromTom Watt

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp