Using Docker in Notebook¶
Using the Docker feature in a Notebook allows you to package the experimental environment, dependency configuration, and computing process in a unified way, so that the Notebook no longer depends on differences in the local environment.
Through Docker, a Notebook can maintain consistent runtime behavior across different nodes and platforms. This makes it easier to reproduce experimental results, share the analysis process, and better align with the production environment. It is especially suitable for scenarios such as data analysis, machine learning, and model validation.
This article briefly explains how to enable, verify, and use the Docker feature in a Notebook instance, and also lists some advanced Docker features and common troubleshooting cases.
Enable Docker¶
Prerequisites¶
Refer to Managing Helm Applications to install the required plugins.
Enable the Docker Feature¶
-
Log in to the AI Lab platform, enter the Notebook interface, and click the Create button.
Update an Existing Instance
To enable Docker in an existing instance, first click the ┇ button on the right of the instance, and then choose Update.
-
Fill in the basic information on the Notebook creation page, and then click Next.
-
Complete the resource configuration, and then click Next.
-
In the advanced configuration, select the Enable Docker option, and then click OK.
-
After the Notebook is created, return to the Notebook list page.
-
When the Notebook instance status changes from Pending to Running, it means the instance has started successfully. If it stays in Pending, refresh the page.
-
At this point, the icon in the Open column on the right of the instance is black and clickable. Click this icon to enter the corresponding Notebook instance.
Verify Whether the Docker Feature Is Available¶
In the terminal environment of the Notebook, you can verify whether the Docker environment has been enabled successfully by running the following commands.
# View the running containers
docker ps
# View all containers (including stopped ones)
docker ps -a
# View the details of a specified container (replace <container_name> with the actual container name or ID)
docker inspect <container_name>
If the commands run normally and return Docker container list information (even if the list is empty), it means the Docker service has started properly.
If you are prompted that the Docker command does not exist or that the Docker daemon cannot be connected, confirm that Docker has been correctly enabled.
Basic Container Management¶
The Docker feature provides complete container lifecycle management, supporting basic operations such as creating, starting, stopping, and deleting containers.
Create and Run a Container¶
Use the docker run command to create and start a container:
# Basic syntax
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
# Run a simple Ubuntu container
docker run -it ubuntu:20.04 /bin/bash
# Run a container in the background
docker run -d --name my-app nginx:latest
# Specify port mapping
docker run -d -p 8080:80 --name web-server nginx:latest
The commonly used parameters are described as follows:
| Parameter | Description |
|---|---|
-d | Run the container in the background |
-it | Run interactively, allocating a pseudo-terminal |
--name | Specify the container name |
-p | Port mapping, in the format of host port:container port |
-v | Mount a data volume |
Manage Containers¶
View the container status:
# View the running containers
docker ps
# View all containers (including stopped ones)
docker ps -a
# View the container details
docker inspect <container_name>
Start and stop a container:
# Start a stopped container
docker start <container_name>
# Stop a running container
docker stop <container_name>
# Restart a container
docker restart <container_name>
Access a Container¶
Enter a running container:
# Enter the interactive terminal of a container
docker exec -it <container_name> /bin/bash
# View all files in a container
docker exec <container_name> ls -la /app
Tip
It is recommended to use docker exec instead of docker attach to enter a container, because exec creates a new process and exiting it does not affect the running status of the container.
Mount Storage¶
A Notebook can mount a PVC or use a data space to achieve persistent data storage. Its mount path can be associated with Docker, thereby enabling data sharing between containers and persistent data storage.
# Mount to a specified directory
docker run -d -v /root/data:/workspace/data --name dev-env python:3.9
Create Images¶
The Docker feature supports creating and saving custom images in various ways to meet the needs of different scenarios.
docker build¶
Building an image with a Dockerfile is the most commonly used method:
# Basic build command
docker build -t my-app:latest .
# Specify the Dockerfile path
docker build -f /path/to/Dockerfile -t my-app:v1.0 .
docker save¶
Export an image as a tar file:
The exported image can be imported with the docker load command:
Use GPU¶
After the Docker feature is enabled in a Notebook, you can mount GPU resources into a container to provide hardware acceleration for AI training and inference.
Mount the GPU First¶
Use the --gpus parameter to mount a GPU into a container:
# Mount all GPUs
docker run --gpus all -it pytorch/pytorch:latest python
# Mount a specified number of GPUs
docker run --gpus 2 -it tensorflow/tensorflow:latest-gpu python
# Mount a specified GPU
docker run --gpus device=0 -it nvidia/cuda:11.8-devel-ubuntu20.04
Which GPUs Are Supported¶
Notebook supports a variety of GPUs. For details, see GPU Support Matrix.
Network Configuration¶
Docker containers can communicate with the host and external networks through various network modes.
Port Mapping¶
Map a container port to a host port to enable external access:
# Map a single port
docker run -d -p 8080:80 --name web-app nginx:latest
# Map multiple ports
docker run -d \ -p 8080:80 \ -p 8443:443 \ --name web-server nginx:latest
# Map to a specified IP
docker run -d -p 127.0.0.1:8080:80 --name local-app nginx:latest
# Map a random port
docker run -d -P --name random-port nginx:latest
Advanced Features¶
The Docker feature of Notebook supports advanced tools such as buildx and Compose to meet the containerized development needs in complex scenarios.
Docker buildx¶
Docker buildx is an extended build feature of Docker that supports multi-platform builds and advanced build features.
Basic Usage¶
# View the buildx version
docker buildx version
# View the available builders
docker buildx ls
# Create a new builder
docker buildx create --name mybuilder --use
# Start the builder
docker buildx inspect --bootstrap
Multi-platform Build¶
# Build a multi-platform image
docker buildx build --platform linux/amd64,linux/arm64 -t my-app:latest .
# Build and push to a registry
docker buildx build --platform linux/amd64,linux/arm64 -t my-app:latest --push .
# Build for a specific platform
docker buildx build --platform linux/amd64 -t my-app:amd64 .
Docker Compose¶
Docker Compose is used to define and run multi-container applications.
Installation and Basic Usage¶
# Check the Compose version
docker compose version
# Start the services
docker compose up -d
# View the service status
docker compose ps
# Stop the services
docker compose down
# View the logs
docker compose logs
Access the Image Registry¶
The Docker feature of Notebook supports accessing the image registry and other public and private image registries.
Image Registry¶
This platform provides a built-in image registry service, where users can store and manage custom images.
Access the Registry¶
# View the registry address (example)
# For the actual address, refer to the repository information in **_My Images_** provided by the platform
REGISTRY_URL="harbor.io"
# Pull an image
docker pull ${REGISTRY_URL}/my-namespace/my-app:latest
# Push an image
docker push ${REGISTRY_URL}/my-namespace/my-app:latest
Authentication Configuration¶
Note
The current version does not support automatically injecting the username and password. You need to configure the authentication information manually.
Configure authentication manually:
# Log in to the AI Lab image registry
docker login registry.io -u <your-username>
# Enter the password
Password: <your-password>
# Verify the login status
docker info | grep -A 5 "Registry Mirrors"
Troubleshooting¶
Common issues and solutions:
-
Container fails to start
-
Port access issues
-
GPU is unavailable
Important Notes
- When the Notebook is shut down, running Docker containers will be stopped.
- After the Notebook is restarted, you need to manually restart the Docker containers.
- Deleting the Notebook will also delete all Docker containers and any data that has not been persisted.
Best Practices
- Use Docker Compose to manage complex multi-container applications.
- Regularly clean up unused images and containers to save storage space
- Configure health checks and restart policies for containers in the production environment
- Use standardized image naming and version management specifications
By making reasonable use of the Docker feature of Notebook, developers can build a flexible and efficient containerized development and deployment environment, fully utilizing the computing resources and storage capabilities of the AI Lab platform.