Closed6

DockerでCUDA+Pythonの環境を構築する

3w36zj63w36zj6

Dockerfile

pyproject.tomlを使う場合

Python環境の管理にRyeを使用する場合の例。

Dockerfile
FROM nvidia/cuda:12.2.0-devel-ubuntu22.04

SHELL ["bash", "-c"]

RUN apt-get update \
    && apt-get -y upgrade

RUN apt-get -y install curl git

# Install Rye
RUN curl -sSf https://rye-up.com/get | RYE_INSTALL_OPTION="--yes" bash
ENV PATH $PATH:/root/.rye/shims/

WORKDIR /workdir

# Rye Sync
COPY ./pyproject.toml ./pyproject.toml
COPY ./.python-version ./.python-version
COPY ./requirements.lock ./requirements.lock
COPY ./requirements-dev.lock ./requirements-dev.lock
RUN rye sync

ENTRYPOINT "bash"

requirements.txtを使う場合

Dockerfile
FROM nvidia/cuda:12.2.0-devel-ubuntu22.04

ARG python_version="3.11.5"

SHELL ["bash", "-c"]

ENV HOME /root

RUN apt-get update \
    && apt-get -y upgrade

RUN apt-get -y install curl git

# Install pyenv
RUN apt-get -y install build-essential libssl-dev libffi-dev libncurses5-dev zlib1g zlib1g-dev libreadline-dev libbz2-dev libsqlite3-dev liblzma-dev
RUN curl https://pyenv.run | bash
ENV PYENV_ROOT $HOME/.pyenv
ENV PATH $PATH:$PYENV_ROOT/bin
ENV PATH $PATH:/root/.pyenv/shims
RUN echo 'eval "$(pyenv init -)"' >> $HOME/.bashrc
RUN . ~/.bashrc

WORKDIR /workdir

# Initialize Python Environment
COPY ./requirements.txt ./requirements.txt
RUN pyenv install ${python_version} \
    && pyenv global ${python_version} \
    && pip install -r requirements.txt

ENTRYPOINT "bash"
3w36zj63w36zj6

環境に入る

docker run -it --gpus all --shm-size 16G --rm -v $(pwd):/workdir IMAGE_NAME
3w36zj63w36zj6

Compose file

compose.yaml
services:
  learning: # 好きな名前をつける
    build:
      context: .
      dockerfile: Dockerfile
    deploy:
      resources:
        reservations:
          devices:
            - capabilities: [gpu]
    shm_size: "16gb"
    volumes:
      - .:/workdir

実行は以下のコマンド。

docker compose run learning
3w36zj63w36zj6

Dev Container

.devcontainer/devcontainer.json
{
  "name": "Existing Compose file",
  "dockerComposeFile": "../compose.yaml",
  "service": "learning",
  "workspaceFolder": "/workdir"
}
このスクラップは2023/10/14にクローズされました