AI Training - Tutorial - Build & use custom Docker image

Explanations on how to build and use your own custom image

Last updated 31st January, 2023.

Objective

This tutorial covers the process of building your own job image for specific needs

Requirements

Write your own Dockerfile

Create a new file and name it Dockerfile

Choosing a base image

First you need to choose a base image to start from.

Header of your Dockerfile should look like this :

FROM <base-image>

If you want to be able to use GPU hardware on your AI TRAINING jobs, the base image should have cuda drivers installed on.

Here is a list of base images (including cuda drivers) that OVHcloud uses within notebooks :

For example if you want to start from the base image tensorflow/tensorflow:2.3.0-gpu :

FROM tensorflow/tensorflow:2.3.0-gpu

Install what you need

Bash command instructions on your Dockerfile should begin with RUN prefix.

Example if your want to install vim :

RUN apt-get update && apt-get install -y vim

You can copy files from your local directory inside docker image with the COPY prefix.

Example if you want to add the file example.txt at the root of the image :

COPY example.txt /example.txt

Images in AI Training are not run as root user, but by an "ovh" user with UID 42420. It means that if you want to be able to write in a specific directory at runtime you will have to give it specific rights. You can do it with the following instruction :

RUN chown -R 42420:42420 <your-target-directory>

The home directory for the "ovh" user (with UID 42420) will be /workspace. If your base image (the one used by the FROM instruction) does not create the /workspace directory (and it probably doesn't if you didn't use an image provided by OVHcloud), then you should create it in your Dockerfile.

RUN mkdir -p /workspace && chown -R 42420:42420 /workspace
ENV HOME /workspace
WORKDIR /workspace

You can set environment variables with the ENV prefix.

Example if you want to add an environment variable KEY with value VALUE

ENV KEY VALUE

For more information about dockerfile we recommand you to refer to the official documentation

Build image

Once your Dockerfile is complete and match your needs you have to choose a name and build the image using the following command in the same directory :

docker build . -t <image-identifier>

The dot . argument indicates that your build context (place of the Dockerfile and other needed files) is the current directory.

The -t argument allow you to choose the identifier to give to your image. Usually image identifiers are composed of a name and a version tag <name>:<version>.

Please make sure that the docker image you will push in order to run containers using AI products respects the linux/AMD64 target architecture. You could, for instance, build your image using buildx as follows:

docker buildx build --platform linux/amd64 ...

Test it locally (Optional)

If you want to verify that your built image is working properly, run the following command :

docker run --rm -it --user=42420:42420 <image-identifier>

Don't forget the --user=42420:42420 argument if you want to simulate the exact same behavior that will occur on AI TRAINING jobs. It executes the docker container as the specific OVHcloud user (user 42420:42420).

Push image in the registry of your choice

Pushing your image to a registry is needed in order for AI Training to pull it.

AI Training provides a default registry called Shared registry where users are able to push their custom images. It is linked with every project by default.

If you prefer using your own private docker registry instead of the shared one, feel free to use it. Just don't forget to add your registry in your AI Training project before using it.

The basic commands to push a docker image to a registry is :

docker login -u <registry-user> -p <registry-password> <registry>
docker tag <image-identifier> <registry>/<image-identifier>
docker push <registry>/<image-identifier>

Example if you want to push an image named custom-image inside a registry registry.gra.training.ai.cloud.ovh.net :

docker login -u <registry-user> -p <registry-password> my-registry.ai.cloud.ovh.net
docker tag custom-image:latest my-registry.ai.cloud.ovh.net/custom-image:latest
docker push my-registry.ai.cloud.ovh.net/custom-image:latest

If you want to know the exact commands to push on the shared registry, please consult the Details button of the Shared Docker Registry section in the Home panel of AI Training.

image

A full Dockerfile example about building a Jupyter notebook image with Tensorflow is available on our example repository here.

Go further

  • You can also build a custom Docker image to deploy a Streamlit app. Here it is.
  • You can imagine deploying a Docker image for data processing and training tasks. Refer to this tutorial.

Feedback

Please send us your questions, feedback and suggestions to improve the service:


Czy ten przewodnik był pomocny?

Zachęcamy do przesyłania sugestii, które pomogą nam ulepszyć naszą dokumentację.

Obrazy, zawartość, struktura - podziel się swoim pomysłem, my dołożymy wszelkich starań, aby wprowadzić ulepszenia.

Zgłoszenie przesłane za pomocą tego formularza nie zostanie obsłużone. Skorzystaj z formularza "Utwórz zgłoszenie" .

Dziękujemy. Twoja opinia jest dla nas bardzo cenna.


Inne przewodniki, które mogą Cię zainteresować...

OVHcloud Community

Dostęp do OVHcloud Community Przesyłaj pytania, zdobywaj informacje, publikuj treści i kontaktuj się z innymi użytkownikami OVHcloud Community.

Porozmawiaj ze społecznością OVHcloud

Zgodnie z Dyrektywą 2006/112/WE po zmianach, od dnia 1 stycznia 2015 r., ceny brutto mogą różnić się w zależności od kraju zameldowania klienta
(ceny brutto wyświetlane domyślnie zawierają stawkę podatku VAT na terenie Polski).