Oualid.games logo Oualid.games

Dockerize Your Perforce

Oualid | Jul 28, 2024
/images/docker-perforce.png

Introduction

As an Unreal Engine game developer, I know the challenges of managing complex projects and maintaining efficient version control. Perforce (Helix Core) has been invaluable in tracking changes and collaborating with my team. However, setting up a Perforce server can be a hassle, especially alongside game development tasks. That’s where Docker comes in, offering a streamlined solution by containerizing the Perforce server, making setup and maintenance easier. In this blog, I’ll share how to set up Perforce in Docker, providing a more manageable and consistent workflow for developers.

DockerFile

Explanation

To set up a Docker image for our P4D server, we first need to create a Dockerfile. Let’s create a file named Dockerfile.

We’ll start with a base image using the latest Ubuntu LTS version available:

FROM ubuntu:noble

Next, we update the package lists and install curl and gpg to add the Perforce repository to our package sources:

RUN apt-get update
RUN apt-get install curl gpg -y

We then add the Perforce repository to the package sources:

RUN curl -sS https://package.perforce.com/perforce.pubkey | gpg --dearmor -o /usr/share/keyrings/perforce.gpg

RUN echo "deb [signed-by=/usr/share/keyrings/perforce.gpg] http://package.perforce.com/apt/ubuntu noble release" > /etc/apt/sources.list.d/perforce.list

RUN apt-get update

Now, we install the P4D server and clean the package cache:

RUN apt-get install helix-p4d -y
RUN apt-get clean

We create a system user and group named perforce to run the server:

ARG DOCKER_USER=perforce
RUN addgroup --system $DOCKER_USER && adduser --system $DOCKER_USER

Next, we set the working directory to root, copy the docker-entrypoint.sh script, set the appropriate permissions, and assign ownership to the perforce user:

WORKDIR /
COPY docker-entrypoint.sh /
RUN chmod 500 /docker-entrypoint.sh
RUN chown $DOCKER_USER:$DOCKER_USER /docker-entrypoint.sh

We create directories for Perforce data and set permissions:

RUN mkdir /metadata && mkdir /library && mkdir /journals && mkdir /backup && mkdir /ssl
RUN chown $DOCKER_USER:$DOCKER_USER /metadata /library /journals /backup /ssl
RUN chmod 700

Switching to the perforce user, we set the necessary environment variables:

USER $DOCKER_USER

ENV P4PORT=ssl:1666
ENV P4ROOT=/metadata
ENV P4NAME=myserver
ENV P4JOURNAL=/journals/journal
ENV P4SSLDIR=/ssl

Finally, we set the entry point to the docker-entrypoint.sh script:

ENTRYPOINT ["/docker-entrypoint.sh"]

Content of Dockerfile

FROM ubuntu:noble

RUN apt-get update
RUN apt-get install curl gpg -y

RUN curl -sS https://package.perforce.com/perforce.pubkey | gpg --dearmor -o /usr/share/keyrings/perforce.gpg

RUN echo "deb [signed-by=/usr/share/keyrings/perforce.gpg] http://package.perforce.com/apt/ubuntu noble release" > /etc/apt/sources.list.d/perforce.list

RUN apt-get update

RUN apt-get install helix-p4d -y
RUN apt-get clean

ARG DOCKER_USER=perforce
RUN addgroup --system $DOCKER_USER && adduser --system $DOCKER_USER

WORKDIR /
COPY docker-entrypoint.sh /
RUN chmod 500 /docker-entrypoint.sh
RUN chown $DOCKER_USER:$DOCKER_USER /docker-entrypoint.sh

RUN mkdir /metadata && mkdir /library && mkdir /journals && mkdir /backup && mkdir /ssl
RUN chown $DOCKER_USER:$DOCKER_USER /metadata /library /journals /backup /ssl
RUN chmod 700

USER $DOCKER_USER

ENV P4PORT=ssl:1666
ENV P4ROOT=/metadata
ENV P4NAME=myserver
ENV P4JOURNAL=/journals/journal
ENV P4SSLDIR=/ssl

ENTRYPOINT ["/docker-entrypoint.sh"]

Entry Point Script

Explanation

Create a file named docker-entrypoint.sh, which will be the script that runs every time the Docker image starts. In this script, we check if P4D has a serverID. If not, it indicates the first run, and we proceed to set up the server. We set the serverID to the value of the $P4NAME environment variable using:

p4d -xD $P4NAME

Next, we generate the SSL credentials files for the server with:

p4d -Gc

Finally, we start the server using the p4d command. It’s recommended by Epic Games to use case-insensitive mode, which can be enabled with the -C1 option.

For more details, you can refer to Epic Games’ documentation on using Perforce as source control for Unreal Engine.

Content of docker-entrypoint.sh

#!/bin/sh
if ! p4d  -C1 -xD; then
    p4d  -C1 -xD $P4NAME
    p4d  -C1 -Gc
fi
p4d -C1

Docker Compose

Explanation

The docker-compose.yml is used to define the container.

  • container_name specifies the custom name for the container
  • build: dockerfile: Dockerfile specifies the Dockerfile used to build the image in our case the one above
  • restart: unless-stopped we set the restart policy to auto start when docker is started unless we stop it.
  •  environment: here we set the environment variables
    • P4PORT=ssl:1666 this specifies the port and the protocol p4d will use
    • P4NAME=perforceserver this sets the server name
  • volumes: mounts the named volumes to the container
  • ports: - 1666:1666 forwards the port 1666
  • volumes declares the named containers

Content of docker-compose.yml

services:
  p4d:
    container_name: "p4d"
    build:
        dockerfile: Dockerfile
    restart: unless-stopped
    hostname: perforce
    environment:
      - P4PORT=ssl:1666
      - P4NAME=perforceserver
    volumes:
      - "serverdata:/metadata"
      - "serverlibrary:/library"
      - "serverjournals:/journals"
      - "servercheckpoints:/checkpoints"
      - "ssl:/ssl"
    ports:
      - 1666:1666

volumes:
  serverdata:
  serverlibrary:
  servercheckpoints:
  serverjournals:
  ssl:

Starting the container

To start the container, simply use the docker compose up command. The -d flag runs the container in detached mode, which allows it to run in the background.

docker compose up -d

Setting Up The Server

  1. Download and Install Helix Visual Client (P4V):
  2. Launch P4Admin:
    • After installation, launch P4Admin to start configuring your server.
  3. Configure Server Connection:
    • Set the server address to ssl:<ip_address>:1666, replacing <ip_address> with your server’s IP address.
  4. Create a New User:
    • Click on “New” to create a new user account. Upon logging in for the first time, you will be prompted to become the sole user with superuser access. Click “Yes” to confirm and proceed.
  5. Set Password Security:
    • Navigate to the “Administration” tab. Click on “Password Security Level…” and set the security level to “strong passwords required.” This setting ensures that only users with appropriate permissions can create new user accounts.

Conclusion

Dockerizing your Perforce server simplifies the setup and reduces the typical headaches associated with server management. It creates a consistent environment, making it easier to focus on the creative aspects of game development rather than infrastructure hassles. With Docker, you get a streamlined, easy-to-maintain solution that keeps your team in sync and your projects on track. It’s a practical approach for developers looking to keep things smooth and efficient.