0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

StarDistをDockerで動かす

Posted at

Stardist GPU環境をDockerで構築する(Ubuntu + TensorFlow + Jupyter + StarDist)

環境:

  • Ubuntu 20.04 LTS
  • NVIDIA RTX 4000 SFF Ada
  • NVIDIA Container Toolkitインストール済
  • Docker, Docker Composeインストール済

Dockerfile

# Use TensorFlow 2.10.1 GPU Jupyter image as base
FROM tensorflow/tensorflow:2.10.1-gpu-jupyter

# Ensure root privileges
USER root

# System updates and tools
RUN apt-get update && \
    apt-get install -y sudo gosu && \
    rm -rf /var/lib/apt/lists/*

# Upgrade pip
RUN pip3 install --upgrade pip

# Install Python packages
RUN pip3 install \
    "numpy<2" \
    "stardist[tf2]" \
    gputools==0.2.15 \
    EDT \
    reikna==0.8.0 \
    numba \
    jupyterlab

# Setup working directory and symlinks
RUN mkdir -p /notebooks && \
    ln -s /data /notebooks/data && \
    ln -s /tf/tensorflow-tutorials /notebooks/tensorflow-tutorials

# Copy helper script
COPY createuser.sh /usr/bin/createuser.sh
RUN chmod +x /usr/bin/createuser.sh

# Expose port for Jupyter Lab
EXPOSE 8888

# Use custom user creation script as entrypoint
ENTRYPOINT ["/usr/bin/createuser.sh"]

# Default command
CMD ["jupyter", "lab", "--ip=0.0.0.0", "--notebook-dir=/notebooks", "--no-browser", "--allow-root"]

docker-compose.yml

version: '3.8'
services:
  stardist:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: stardist_container
    runtime: nvidia
    environment:
      - USER_NAME=dockeruser
      - GROUP_NAME=dockeruser
      - USER_ID=1000
      - GROUP_ID=1000
      - USER_PASSWORD=yourpassword
      - ROOT_PASSWORD=yourrootpassword
      - NVIDIA_VISIBLE_DEVICES=all
      - NVIDIA_DRIVER_CAPABILITIES=compute,utility
    volumes:
      - /home/youruser/Data:/data
    ports:
      - "8777:8888" #自分の環境では8888がもう使われているので。
    tty: true

createuser.sh

#!/bin/bash
USER_NAME=${USER_NAME:-dockeruser}
GROUP_NAME=${GROUP_NAME:-dockeruser}
USER_ID=${USER_ID:-1000}
GROUP_ID=${GROUP_ID:-1000}
USER_PASSWORD=${USER_PASSWORD:-password}
ROOT_PASSWORD=${ROOT_PASSWORD:-rootpassword}

# Group setup
if ! getent group "$GROUP_NAME" >/dev/null 2>&1; then
  groupadd -g "$GROUP_ID" "$GROUP_NAME"
fi

# User setup
if ! id -u "$USER_NAME" >/dev/null 2>&1; then
  useradd -m -s /bin/bash -u "$USER_ID" -g "$GROUP_NAME" "$USER_NAME"
  echo "$USER_NAME:$USER_PASSWORD" | chpasswd
  usermod -aG sudo "$USER_NAME"
fi

echo "root:$ROOT_PASSWORD" | chpasswd

# Drop privileges to created user
exec gosu "$USER_NAME" "$@"

entrypoint.sh

#!/bin/bash
# Print the URL to access Jupyter Lab
echo "Access Jupyter notebook at http://localhost:8888 (or http://<your_docker_host>:8888 if accessing from another machine)"

# Start Jupyter Lab
exec jupyter lab --ip=0.0.0.0 --no-browser --allow-root

これを Stardist ディレクトリにおいてdocker compose up --build -d

💡 createuser.shentrypoint.shは、「rootでログインするのがなんとなく嫌」「ボリュームマウントをミスなく行いたい」という目的でおまじない的に入れています。
💡notebooksディレクトリを作っておかないとJupyter Labを立ち上げたときにマウントしたボリュームが見えないです。

参考:
https://forum.image.sc/t/difficulty-installing-stardist-tensorflow-gputools-with-anaconda/104305/15

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?