ほぼ自分用で、研究室のサーバーで使う用。新しくプロジェクトを作るとかに使う雛形。
もしrequirements.txtがあれば先に入れておく。
まず init.sh を叩く。すると事前設定が完了するのでその後vscodeの左下のボタン押すなりしてコンテナを立ち上げる。
コンテナ内でpip install的なことがしたくなればrye addとrye syncする。
vscodeの設定
Dev › Containers: Docker Compose Path に podman-compose を設定する。
ディレクトリ構成
. |-- .devcontainer | `-- devcontainer.json |-- Dockerfile |-- compose.yml |-- init.sh `-- requirements.txt (optional)
これは最初なので、出来上がるとなんかいろいろフォルダとかができてる。
init.sh
#!/bin/bash
PROJECT_NAME=$1
PYTHON_VERSION=$2
if [ -z "$PROJECT_NAME" ]; then
read -p "Enter the name of your project (default is current directory name): " input
# もし入力が空なら、現在のディレクトリ名を格納
if [ -z "$input" ]; then
PROJECT_NAME=$(basename "$(pwd)")_project
else
PROJECT_NAME=$input
fi
fi
if [ -z "$PYTHON_VERSION" ]; then
read -p "Enter the Python version to use (e.g. 3.10): " input
# Pythonバージョンが指定されていない場合はデフォルト値を設定
if [ -z "$input" ]; then
PYTHON_VERSION="3.10"
else
PYTHON_VERSION=$input
fi
fi
# requirementsファイルの存在を検証し、変数に設定
if [ -f "requirements.txt" ]; then
REQUIREMENTS="requirements.txt"
else
REQUIREMENTS=""
fi
podman run \
--name creator \
--volume $(pwd):/$PROJECT_NAME \
--workdir /$PROJECT_NAME \
ubuntu:22.04 bash -c \
"apt-get update && apt-get install -y curl && \
curl -sSL https://rye-up.com/get | RYE_NO_AUTO_INSTALL=1 RYE_INSTALL_OPTION='--yes' bash && \
. \"\$HOME/.rye/env\" && \
if [ -z \"$REQUIREMENTS\" ]; then rye init; else rye init -r $REQUIREMENTS; fi && \
rye pin $PYTHON_VERSION && \
rye sync"
podman rm creator
echo DOCKER_COMPOSE_PROJECT_DIR=$(pwd) > .env
.devcontainer > devcontainer.json
{
"name": "project-name",
"dockerComposeFile": [
"../compose.yml"
],
"service": "app",
"workspaceFolder": "/workspace",
"customizations": {
"vscode": {
"extensions": [
"ms-python.python",
"charliermarsh.ruff",
"ms-azuretools.vscode-docker",
"ms-python.isort",
"ms-toolsai.jupyter",
"congyiwu.vscode-jupytext",
"njpwerner.autodocstring",
"usernamehw.errorlens",
"oderwat.indent-rainbow",
"usernamehw.errorlens",
"vivaxy.vscode-conventional-commits"
]
}
}
}
Dockerfile
参考:
FROM nvidia/cuda:12.0.0-cudnn8-devel-ubuntu22.04
RUN \
--mount=type=cache,target=/var/lib/apt/lists \
--mount=type=cache,target=/var/cache/apt/archives \
apt-get update && apt-get install -y \
libffi-dev \
libgl1-mesa-dev \
vim \
tmux \
git \
locales \
wget\
curl\
zip\
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
RUN wget https://fonts.google.com/download?family=Noto%20Sans%20JP -O /tmp/fonts_noto.zip && \
mkdir -p /usr/share/fonts &&\
unzip /tmp/fonts_noto.zip -d /usr/share/fonts
RUN locale-gen en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /workspace
ENV PATH $PATH:/root/.rye/shims/
RUN curl -sSf https://rye-up.com/get | RYE_NO_AUTO_INSTALL=1 RYE_INSTALL_OPTION="--yes" bash
RUN --mount=type=bind,source=pyproject.toml,target=pyproject.toml \
--mount=type=bind,source=requirements.lock,target=requirements.lock \
--mount=type=bind,source=requirements-dev.lock,target=requirements-dev.lock \
--mount=type=bind,source=.python-version,target=.python-version \
--mount=type=bind,source=README.md,target=README.md \
rye sync --no-dev --no-lock
RUN . .venv/bin/activate
ENTRYPOINT "bash"
compose.yml
podman-composeを利用する場合は、contextやvolumes等に相対パスが使えないらしい
services:
app:
build:
context: $DOCKER_COMPOSE_PROJECT_DIR # path to the directory containing the Dockerfile
dockerfile: Dockerfile
volumes:
- $DOCKER_COMPOSE_PROJECT_DIR:/workspace # mount the project directory to /workspace
user: root
environment:
NVIDIA_VISIBLE_DEVICES: 0,1,2,3
NVIDIA_DRIVER_CAPABILITIES: all
tty: true