Tutorial: Create a custom workspace image that supports arbitrary user IDs
This tutorial guides you through creating a custom workspace image that supports arbitrary user IDs. Once complete, you can use this custom image with any workspace you create in GitLab.
To create a custom workspace image that supports arbitrary user IDs:
- Create a base Dockerfile.
- Add support for arbitrary user IDs.
- Build the custom workspace image.
- Push the custom workspace image to the GitLab container registry.
- Use the custom workspace image in GitLab.
Before you begin
You need the following:
- A GitLab account with permission to create and push container images to the GitLab container registry.
- Docker installed on your local machine.
Create a base Dockerfile
Let’s start by creating a base Dockerfile for our container image. Use the Python
3.11-slim-bullseye
image from Docker Hub as our starting point:
FROM python:3.11-slim-bullseye
Great! You’ve created the foundation for your custom workspace image. Next, add the code that enables arbitrary user ID support.
Add support for arbitrary user IDs
Next, you will add support for arbitrary user IDs to the base image. This ensures your workspace runs in GitLab.
To add a new gitlab-workspaces
user with an ID of 5001
, and assign them the necessary
directory permissions, add the following code to your Dockerfile:
RUN useradd -l -u 5001 -G sudo -md /home/gitlab-workspaces -s /bin/bash -p gitlab-workspaces gitlab-workspaces
ENV HOME=/home/gitlab-workspaces
WORKDIR $HOME
RUN mkdir -p /home/gitlab-workspaces && chgrp -R 0 /home && chmod -R g=u /etc/passwd /etc/group /home
USER 5001
Build the custom workspace image
With your Dockerfile complete, you’re ready to build your custom workspace image:
Run the following command in the directory where you created the Dockerfile:
docker build -t my-gitlab-workspace .
This might take a few minutes depending on your internet connection and system speed.
After the build process completes, test the image locally:
docker run -ti my-gitlab-workspace sh
You should now have permission to run commands as the gitlab-workspaces
user. Perfect! Your image
is working locally. Next, you will make it available in GitLab.
Push the custom workspace image to the GitLab container registry
Push your custom workspace image to the GitLab container registry for use in your projects:
Sign in to your GitLab account:
docker login registry.gitlab.com
Tag the image with the GitLab container registry URL:
docker tag my-gitlab-workspace registry.gitlab.com/your-namespace/my-gitlab-workspace:latest
Remember to replace
your-namespace
with your actual GitLab namespace.Push the image to the GitLab container registry:
docker push registry.gitlab.com/your-namespace/my-gitlab-workspace:latest
This upload might take a while depending on your internet connection speed.
Well done! Your custom workspace image is now safely stored in the GitLab container registry and ready to use.
Use the custom workspace image in GitLab
For the final step, you will configure your project to use your custom workspace image:
Update the container image in your project’s
.devfile.yaml
:schemaVersion: 2.2.0 components: - name: tooling-container attributes: gl/inject-editor: true container: image: registry.gitlab.com/your-namespace/my-gitlab-workspace:latest
Remember to replace
your-namespace
with your actual GitLab namespace.
Congratulations! You’ve successfully created and configured a custom workspace image that supports arbitrary user IDs. You can now use this custom image with any workspace you create in GitLab.
Related topics
Docs
Edit this page to fix an error or add an improvement in a merge request.
Create an issue to suggest an improvement to this page.
Product
Create an issue if there's something you don't like about this feature.
Propose functionality by submitting a feature request.
Feature availability and product trials
View pricing to see all GitLab tiers and features, or to upgrade.
Try GitLab for free with access to all features for 30 days.
Get help
If you didn't find what you were looking for, search the docs.
If you want help with something specific and could use community support, post on the GitLab forum.
For problems setting up or using this feature (depending on your GitLab subscription).
Request support