做一个开箱即用的rust开发环境docker镜像

2023 May 26 See all posts


开箱即用的rust开发环境,开发工具vscode web版,配合插件rust-analyzer和CodeLLDB来做语法高亮分析和debug

开发环境组成

Dockerfile文件:

FROM ubuntu:22.04
SHELL ["/bin/bash", "-c"]

# 安装必要依赖与开发工具
RUN sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list && \
    apt-get update && apt-get install -y \
    openssl libssl-dev \
    gdb-multiarch cmake \
    git wget python3 vim file curl \
    autoconf automake autotools-dev  libmpc-dev libmpfr-dev libgmp-dev \
    gawk build-essential bison flex texinfo gperf libtool patchutils bc \
    zlib1g-dev libexpat-dev \
    ninja-build pkg-config libglib2.0-dev libpixman-1-dev libsdl2-dev \ 
    && rm -rf /var/lib/apt/lists/*


# 安装 code-server 和扩展
ARG CODE_VERSION=4.13.0
ARG RUST_ANALYZER_VERSION=2023-05-22
ARG CODELLDB_VERSION=1.9.1
RUN cd /usr/local/ && \
    wget https://github.com/coder/code-server/releases/download/v${CODE_VERSION}/code-server-${CODE_VERSION}-linux-amd64.tar.gz && \
    #wget http://10.200.5.65:8000/code-server-4.13.0-linux-amd64.tar.gz && \
    tar xf code-server-${CODE_VERSION}-linux-amd64.tar.gz && \
    ln -s  /usr/local/code-server-${CODE_VERSION}-linux-amd64/bin/code-server /usr/bin/code && \
    rm code-server-${CODE_VERSION}-linux-amd64.tar.gz && \

    wget https://github.com/rust-lang/rust-analyzer/releases/download/${RUST_ANALYZER_VERSION}/rust-analyzer-alpine-x64.vsix && \
    #wget http://10.200.5.65:8000/rust-analyzer-linux-x64.vsix && \
    code --install-extension rust-analyzer-linux-x64.vsix && \
    rm rust-analyzer-linux-x64.vsix && \

    wget https://github.com/vadimcn/codelldb/releases/download/v${CODELLDB_VERSION}/codelldb-x86_64-linux.vsix && \
    #wget http://10.200.5.65:8000/codelldb-x86_64-linux.vsix && \
    code --install-extension codelldb-x86_64-linux.vsix && \
    rm codelldb-x86_64-linux.vsix 

WORKDIR /root
# 安装 rust
ARG RUST_VERSION=nightly
ENV RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
ENV RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
RUN mkdir .cargo && \
    echo '[source.crates-io]' >> .cargo/config && \
    echo 'registry = "https://github.com/rust-lang/crates.io-index"' >> .cargo/config && \
    echo 'replace-with = "ustc"' >> .cargo/config && \
    echo '[source.ustc]' >> .cargo/config && \
    echo 'registry = "git://mirrors.ustc.edu.cn/crates.io-index"' >> .cargo/config && \
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs -o rustup-init && \
    chmod +x rustup-init && \
    ./rustup-init -y --default-toolchain ${RUST_VERSION} --target x86_64-unknown-linux-gnu && \
    rm rustup-init && \
    source $HOME/.cargo/env && \
    cargo install cargo-binutils && \
    rustup component add llvm-tools-preview && \
    rustup component add rust-src

EXPOSE 8080/tcp
CMD ["code", "--auth", "none", "--bind-addr", "0.0.0.0:8080"]

快速开始

运行docker客户端,输入

docker pull lyrisdocker/rust-dev:1.1

镜像成功拉取后,执行

docker run -d --privileged -p 8888:8080 lyrisdocker/rust-dev:1.1

这时使用浏览器打开 http://localhost:8888 即可启动开发环境。

后续可以在 Docker客户端中控制已创建容器的暂停与开启。

假设代码在/data/rst-proj 目录,则可以把工作目录附上去

docker run -d --privileged -v /data/rst-proj:/root/rst-proj -w /root/rst-proj -p 8888:8080 lyrisdocker/rust-dev:1.1

然后就可以在浏览器里面打开工作目录进行编码和设置断点调试了

Back to top