Install Docker

sudo apt-get update -y
sudo apt-get upgrade -y

curl -fsSL https://get.docker.com -o get-docker.sh

sudo sh get-docker.sh -y

sudo usermod -aG docker ubuntu 

newgrp docker


Nvidia Container Toolkit

curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
  && curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
    sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
    sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list

sudo apt-get update
sudo apt-get install -y nvidia-container-toolkit

sudo nvidia-ctk runtime configure --runtime=docker

sudo systemctl restart docker


Dockerfile example

# Use a base image
#FROM python:3.11-slim
FROM nvidia/cuda:12.1.0-base-ubuntu22.04
# Set environment variables
ENV MY_VARIABLE=my_value
# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    software-properties-common \
    git \
    python3-pip \
    && rm -rf /var/lib/apt/lists/*
# Set the working directory
WORKDIR /app
# Copy files into the image
COPY . /app
# Expose a port
EXPOSE 8051
# Install Python dependencies based on the requirements.txt file
RUN pip3 install -r requirements.txt
# Configure a container that will run as an executable
ENTRYPOINT ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]
# Define a command to run on container startup
CMD ["./start.sh"]
# Keep container running
CMD ["tail -f /dev/null"]
Build image docker build -t image_name .
Run container docker run -b -p 8501:8501 -e API="XXX" --name image_container image_name
Remove all container. docker rm $(docker ps -a -q)
Remove all iamges. docker image rm $(docker images -q)


The process of building and running Docker containers can be automated using tools like Docker Compose.

services:
  web:  #service name
    build: .
    shm_size: 8gb # increase shared memory
    ports:
      - "8501:8501"
    volumes:
      - .:/app
    environment:
      - GOOGLE_API_KEY=${GOOGLE_API_KEY}
      - NVIDIA_VISIBLE_DEVICES=all
    command: tail -f /dev/null  #keep container running in the background
Build image and run container docker compose up --detach
remove container docker compose down