Build a Docker Image

为了避免遗忘,整理一下开发使用Docker配置PyTorch/TensorFlow环境的方法


DockerFile

# For more information, please refer to https://aka.ms/vscode-docker-python
FROM cddlyf/pytorch1.3.0-tensorflow-py36-cuda10.1:latest

# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1

# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1

# Copy pip requirements
COPY requirements.txt .

# Copy pre-downloaded wheels
COPY ./wheels ./wheels

# switch to ustc pip source
# RUN pip install --default-timeout=10 -U pip
# RUN pip config set global.index-url http://pypi.mirrors.ustc.edu.cn/simple
# RUN pip config set install.trusted-host pypi.mirrors.ustc.edu.cn
# RUN pip install -r requirements.txt

# Run pip install from file
RUN pip install --no-index --find-links=./wheels -r requirements.txt

RUN pip install torch-cluster==1.4.5
RUN pip install torch-geometric==1.3.2
RUN pip install torch-scatter==1.3.2
RUN pip install torch-sparse==0.4.3

# Install libraries
COPY ./mesh /mesh
# RUN apt-get install libboost-dev
WORKDIR /mesh
RUN make all

# Copy code from . to $CONTAINER/app
WORKDIR /app
COPY . /app

# Remove wheels
RUN rm -rf /app/wheels

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
# RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
# USER appuser

# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
# CMD ["python", "main.py"]

一些记录:

  1. 由于pip安装时间长,经常崩溃,可以先使用pip download -d wheels -r requirements.txt将依赖wheels下载下来,再进行本地安装
  2. 配置错误产生中间文件太多,可以用docker image prune , docker container prune 清理

构建与运行

docker build --pull -f "/your/path/to/Dockerfile" -t "img_name:tag" "/your/path/to/workdir"

# Replace 6667 with your port
docker run -p 6667:22  -it -d --name="container_name" img_name:tag

# Test docker ssh
docker ps
ssh -p 6667 root@0.0.0.0

发表回复

您的电子邮箱地址不会被公开。