From 4fc6c3f2aecae2551b1dac13cd4392bf6dadad04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=92=B2=E6=BA=90?= <48008469+puyuan1996@users.noreply.github.com> Date: Fri, 15 Sep 2023 14:45:05 +0800 Subject: [PATCH] feature(pu): add Dockerfile and its usage instructions (#95) * feature(pu): add Dockerfile and its usage instructions * polish(pu): polish dockerfile and its usage instructions --- Dockerfile | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 24 ++++++++++++++++++++++++ README.zh.md | 26 ++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..d36b5398c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,52 @@ +# This Dockerfile describes the process of creating a Docker image that includes +# the necessary environment to run the LightZero library. + +# The Docker image is based on Ubuntu 20.04, and it installs Python 3.8 and other +# necessary dependencies. It then clones the LightZero library from its GitHub +# repository and installs it in an editable mode. + +# Before building the Docker image, create a new empty directory, move this Dockerfile into it, +# and navigate into this directory. This is to avoid sending unnecessary files to the Docker daemon +# during the build. Then you can then build the Docker image using the following command in your terminal: +# docker build -t ubuntu-py38-lz:latest -f ./Dockerfile . + +# To run a container from the image in interactive mode with a Bash shell, you can use: +# docker run -dit --rm ubuntu-py38-lz:latest /bin/bash + +# Once you're inside the container, you can run the example Python script with: +# python ./LightZero/zoo/classic_control/cartpole/config/cartpole_muzero_config.py + +# Note: The working directory inside the Docker image is /opendilab, so you don't need +# to change your current directory before running the Python script. + + +# Start from Ubuntu 20.04 +FROM ubuntu:20.04 + +# Set the working directory in the Docker image +WORKDIR /opendilab + +# Install Python 3.8 and other dependencies +# We update the apt package list, install Python 3.8, pip, compilers and other necessary tools. +# After installing, we clean up the apt cache and remove unnecessary lists to save space. +RUN apt-get update && \ + apt-get install -y python3.8 python3-pip gcc g++ swig git && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* + +# Create a symbolic link for Python and pip +# This makes it easy to call python and pip from any location in the container. +RUN ln -s /usr/bin/python3.8 /usr/local/bin/python && \ + ln -s /usr/bin/pip3 /usr/local/bin/pip + +# Update pip and setuptools to the latest version +# This step ensures that we have the latest tools for installing Python packages. +RUN python -m pip install --upgrade pip setuptools + +# Clone the LightZero repository from GitHub +# This step downloads the latest version of LightZero to our Docker image. +RUN git clone https://github.com/opendilab/LightZero.git + +# Install the LightZero package in editable mode +# The -e option allows us to edit the source code without needing to reinstall the package. +RUN pip install -e ./LightZero \ No newline at end of file diff --git a/README.md b/README.md index 8dad689fb..cfbe259e0 100644 --- a/README.md +++ b/README.md @@ -152,6 +152,30 @@ Kindly note that LightZero currently supports compilation only on `Linux` and `m We are actively working towards extending this support to the `Windows` platform. Your patience during this transition is greatly appreciated. +## Installation with Docker + +We also provide a Dockerfile that sets up an environment with all dependencies needed to run the LightZero library. This Docker image is based on Ubuntu 20.04 and installs Python 3.8, along with other necessary tools and libraries. +Here's how to use our Dockerfile to build a Docker image, run a container from this image, and execute LightZero code inside the container. +1. **Download the Dockerfile**: The Dockerfile is located in the root directory of the LightZero repository. Download this [file](https://github.com/opendilab/LightZero/blob/main/Dockerfile) to your local machine. +2. **Prepare the build context**: Create a new empty directory on your local machine, move the Dockerfile into this directory, and navigate into this directory. This step helps to avoid sending unnecessary files to the Docker daemon during the build process. + ```bash + mkdir lightzero-docker + mv Dockerfile lightzero-docker/ + cd lightzero-docker/ + ``` +3. **Build the Docker image**: Use the following command to build the Docker image. This command should be run from inside the directory that contains the Dockerfile. + ```bash + docker build -t ubuntu-py38-lz:latest -f ./Dockerfile . + ``` +4. **Run a container from the image**: Use the following command to start a container from the image in interactive mode with a Bash shell. + ```bash + docker run -dit --rm ubuntu-py38-lz:latest /bin/bash + ``` +5. **Execute LightZero code inside the container**: Once you're inside the container, you can run the example Python script with the following command: + ```bash + python ./LightZero/zoo/classic_control/cartpole/config/cartpole_muzero_config.py + ``` + [comment]: <> (- [AlphaGo Zero](https://www.nature.com/articles/nature24270) ) ## Quick Start diff --git a/README.zh.md b/README.zh.md index fb65b9196..c57783bbd 100644 --- a/README.zh.md +++ b/README.zh.md @@ -140,6 +140,32 @@ pip3 install -e . 请注意,LightZero 目前仅支持在 `Linux` 和 `macOS` 平台上进行编译。 我们正在积极将该支持扩展到 `Windows` 平台。 +### 使用 Docker 进行安装 + +我们也提供了一个Dockerfile,用于设置包含运行 LightZero 库所需所有依赖项的环境。此 Docker 镜像基于 Ubuntu 20.04,并安装了Python 3.8以及其他必要的工具和库。 +以下是如何使用我们的 Dockerfile 来构建 Docker 镜像,从该镜像运行一个容器,并在容器内执行 LightZero 代码的步骤。 + +1. **下载 Dockerfile**:Dockerfile 位于 LightZero 仓库的根目录中。将此[文件](https://github.com/opendilab/LightZero/blob/main/Dockerfile)下载到您的本地机器。 + +2. **准备构建上下文**:在您的本地机器上创建一个新的空目录,将 Dockerfile 移动到此目录,并导航到此目录。这一步有助于在构建过程中避免向 Docker 守护进程发送不必要的文件。 + ```bash + mkdir lightzero-docker + mv Dockerfile lightzero-docker/ + cd lightzero-docker/ + ``` +3. **构建 Docker 镜像**:使用以下命令构建 Docker 镜像。此命令应在包含 Dockerfile 的目录内运行。 + ```bash + docker build -t ubuntu-py38-lz:latest -f ./Dockerfile . + ``` +4. **从镜像运行容器**:使用以下命令以交互模式启动一个 Bash shell 的容器。 + ```bash + docker run -dit --rm ubuntu-py38-lz:latest /bin/bash + ``` +5. **在容器内执行 LightZero 代码**:一旦你在容器内部,你可以使用以下命令运行示例 Python 脚本: + ```bash + python ./LightZero/zoo/classic_control/cartpole/config/cartpole_muzero_config.py + ``` + ## 快速开始 使用如下代码在 [CartPole](https://gymnasium.farama.org/environments/classic_control/cart_pole/) 环境上快速训练一个 MuZero 智能体: