Skip to content

Using Custom Containers

Alex Hurt edited this page Nov 17, 2022 · 6 revisions

Step 0: Prerequisites

  • You have code that you want to run on Nautilus
  • You have created an account on the Nautilus GitLab
  • You have docker installed

Step 1: Create the Repository

Login to the Nautilus GitLab and create a public repository with an appropriate name for what you are doing.

You can then (if you want) choose to make everything except the container registry private, including the code, issues, etc. However, it is critical that the container registry must be public.

Step 2: Creating the Container

Using your local docker installation and code, create a standalone docker container, meaning that all libraries, packages, and code are present in the final container. Do not build in any requirement of code being mounted to the running container if you are wanting to use this container for jobs on Nautilus.

If you are unfamiliar with docker, there is a short tutorial on building a container with Dockerfiles [here](./Building-a-Dockerfile]. There are also many resources available online including the Dockerfile reference.

Once you have a standalone docker container, push your Dockerfile to the root of your repository from Step 1.

Step 3: CI/CD

We need the container to be publically available for Kubernetes to pull it and use it with our pods and jobs. To do this, we can utilise the CI/CD capabilities of GitLab.

Copy the gitlab-ci.yml file in this repo to the root of your repository from Step 1 and push it to GitLab. If you have cloned down this repo to ./nautilus and your custom code is in a repo located at ./code, you would run:

cp ./nautilus/gitlab-ci/gitlab-ci.yml ./code/.gitlab-ci.yml
cd ./code
git add .gitlab-ci.yml
git commit -m "CI/CD"
git push

GitLab will then build and push your container for every commit to the main branch of your repo.

Step 4: Specify Container in Kube Spec

Now that we have our container built and publically available, we need only to update the image parameter in the Kube Spec to the link to our custom image.

To get the link that you will need to use, click on Container Registry from your repository home page on GitLab. Here is an example of how to find that:

Screen_Shot_2022-09-11_at_6.18.27_PM

It will be in the format of: gitlab-registry.nrp-nautilus.io/USERNAME/REPOSITORY:TAG

You will then place that link in your Kube spec for your job or pod:

spec:
  containers:
    - name: container-name
      image: gitlab-registry.nrp-nautilus.io/USERNAME/REPOSITORY:TAG

Step 5: Create the Job/Pod

You can now use your custom container by simply creating the pod/job:

kubectl apply -f mycustompod.yml

Tips on Using Custom Containers

  • Always set the imagePullPolicy to IfNotPresent to ensure there is not any clashing of image names in the internal systems of Nautilus
  • Always use an explicit container tag, never use the latest tag -- there is a bug in the internal tool that Nautilus uses that will cause the Image Pull to fail.

Automating Custom Containers

If you want to use the same YAML file for multiple jobs, running directories, container tags, etc., you can automate this process using the envsubst command.

For exmaple, say on Nautilus you have data staged in multiple directories and you want to run multiple jobs with the same custom container on these three directories.

You can put in your kube spec:

metadata:
    name: $KUBE_JOB_NAME
spec:
    containers:
        - workingDir: $KUBE_WORKING_DIR

and then for the three directories, you can run:

KUBE_JOB_NAME=job1 KUBE_WORKING_DIR=/path/to/dir1 ensubst < MYFILE.yml | kubectl apply -f - 
KUBE_JOB_NAME=job2 KUBE_WORKING_DIR=/path/to/dir2 ensubst < MYFILE.yml | kubectl apply -f - 
KUBE_JOB_NAME=job3 KUBE_WORKING_DIR=/path/to/dir3 ensubst < MYFILE.yml | kubectl apply -f - 

and three jobs will be created with each one operating on a different directory, all from the same YAML file.

Clone this wiki locally