Robin Fischer

Game programmer that wants to share and document.

Run Unity Personal inside a Docker container

TL;DR: For in-depth info skip to next paragraph. Use the ready baked images available on DockerHub or follow the steps beneath to create your own activated Unity Linux on a Docker image.

Intro

I have quite a few Unity based code repositories and naturally I wanted to automate most tasks. Mainly running tests and building .unitypackage files for distribution and easy integration. Normally I use Submodules to integrate my code in other projects. But sometimes on Gamejams or Prototypes it is nice to just drag in a .unitypackage file. Of course you can run Unity on a Windows Gitlab Runner if you host your own. But I wanted to use shared runners and asked myself if it is possible with Unity Linux and Docker. So I started out to run Unity on Linux inside a Docker container. The main problem was the activation of the Unity personal edition. As of this time you can only activate Unity via command-line when using a serial number (You can check the options HERE). You only have a serial number when you have a version above Unity Personal. I’m using Unity Personal so I could not use the easy command-line activation. To log in to your Unity Personal edition you have to have a GUI and there is where the problem starts. Here is my guide on how to activate Unity Personal inside a Docker container:

Steps

  1. You need a Dockerfile to create an image with Unity and all needed dependencies. You can use the one I am using if you want a quick start, but you can also look through the needed dependencies and create your own Dockerfile. The Dockerfile uses a bash script to download Unity from a certain link. You can aquire the links from HERE. The GitHub repository that is linked above uses branches for the different Unity versions. If you switch to a branch you can see the link used in the get-unity.sh file. Please note that the other steps in the Dockerfile (e.g. copying the .ulf and .pem file) are the result of the following steps. If you want to follow this post with a fresh slate then comment out the ADD steps that copy the .pem and .ulf file to the image. If you want to you can use the ready built images but keep in mind that the image is activated on the account I used when creating the license files.

  2. Install Docker by following the instructions HERE

  3. Create your Docker image by running cd <DIRECTORY_WITH_DOCKERFILE> and docker image build --tag my-unity-image:tagname .. If you are on Windows make sure your Dockerfile and any other file used by Docker uses Unix-Style line endings. This step can take some time. Especially the “Starting Unity3D installer download…” step. It took 30 min on my machine.

  4. Get inside that container. The following commands we only want to run once and then extract the results. That’s why we will continue to run commands inside the Docker container instead of adding them to the Dockerfile. Use docker run -it --name MyUnityDockerContainer -p 8080:5900 my-unity-image:tagname to create the Docker container and enter it with your shell. The -p switch exposes a port from the Docker container to the host machine so we can communicate over the network interface. We need this later.

  5. To use Unity’s launcher GUI to activate this version of Unity we need a VNC server and client. We can use XVFB to create a ‘fake’ window server. Run the following bash commands:

     # Enhance our sources list so we have access to required packages
     echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
     apt-get update
    
     # Install vnc, xvfb in order to create a 'fake' display
     apt-get install -y x11vnc xvfb
     mkdir ~/.vnc
     # Setup a password
     x11vnc -storepasswd 1234 ~/.vnc/passwd
    
  6. Commit changes from installing VNC server docker commit MyUnityDockerContainer my-unity-image:tagname. This steps creates a revision of our changes to the image. It will be helpful in the next steps.

  7. Remove the docker container docker container rm MyUnityDockerContainer. This seems weird but we want to create a new container based on our newly created image revision. The image a container is using cannot be updated. We can only remove and recreate the container.

  8. Start the container in attach mode docker run -it --name MyUnityDockerContainer -p 8080:5900 my-unity-image:tagname. Your shell should now run inside the Docker container and not on your host machine. You can now start the VNC server inside the Docker container using x11vnc -forever -usepw -create.

  9. Install and connect VNC client on your host machine. I use RealVNC as my VNC client but any modern VNC client should work. Connect to your running docker container using localhost:8080 and the password (in this example: ‘1234’) used when starting x11vnc.

  10. Start Unity inside the docker container VNC session by executing /opt/Unity/Editor/Unity. There will be some prompts from Unity, just hit OK on those.

  11. Activate Unity using the normal launcher GUI. I would recommend to not use your private/main Unity account and instead create a new Account that is then used in your build environment. Continue the on screen steps until the launcher lets you create a new project. Yes, you have to do the survey! :)

    alt text Unity inside VNC client Looks interesting but it works.

  12. Now Unity is activated in our Docker container but what now? We want to be able to create a Docker image with an already registered Unity. For this we have to find out what Unity did to our OS when we activated it. Luckily Docker does feature a diff command. Yes, you can diff a complete OS! Open a new shell window/tab and lets run the diff by using the command docker diff MyUnityDockerContainer on our host machine. Now we can look through the changes and after some searching we can see two files which are suspiciously placed: /root/.local/share/unity3d/Unity/Unity_v5.x.ulf and /root/.local/share/unity3d/Certificates/CACerts.pem.

  13. Copy the files out of the container using docker cp MyUnityDockerContainer:/root/.local/share/unity3d/Unity/Unity_v5.x.ulf Unity_v5.x.ulf and docker cp MyUnityDockerContainer:/root/.local/share/unity3d/Certificates/CACerts.pem CACerts.pem. The files are now placed in the current directory where your shell is placed.

  14. With the license files we can now enhance the Dockerfile to add the files to the image. Look at the finished Dockerfile linked above to see how to add the files in the image creation process. The Dockerfile can be used on DockerHub to create images on your account. You can also use it locally with docker image build <PATH_TO_DOCKERFILE> to create an image.

  15. With these added license files Unity should start correctly and see valid license files which are required for any kind of Unity operation. Hooray!

Conclusion

This concludes the steps I used to activate Unity Personal inside a Docker container. You can of course build your own Unity images or you can use my DockerHub Repository where I maintain most Unity Linux versions as Docker images. You can also create a Docker image that is based on one of my images in case you have to add something to the image.

I hope you enjoyed this little guide and all steps work on your machine.