Skip to content

Commit 4510a3b

Browse files
committed
first commit docker-lsp-python
0 parents  commit 4510a3b

File tree

7 files changed

+417
-0
lines changed

7 files changed

+417
-0
lines changed

.github/workflows/docker.yml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
name: Docker Build and Push
2+
3+
# 这个工作流使用未经 GitHub 认证的操作。
4+
# 它们由第三方提供,受单独的服务条款、隐私政策和支持文档约束。
5+
6+
on:
7+
push:
8+
branches:
9+
- main
10+
workflow_dispatch:
11+
12+
env:
13+
# 使用 docker.io 作为 Docker Hub(如果为空)
14+
REGISTRY: ghcr.io
15+
# github.repository 格式为 <account>/<repo>
16+
IMAGE_NAME: ${{ github.repository }}
17+
18+
concurrency:
19+
group: ${{ github.workflow }}-${{ github.ref }}
20+
cancel-in-progress: true
21+
22+
jobs:
23+
build:
24+
runs-on: ubuntu-latest
25+
26+
permissions:
27+
contents: read
28+
packages: write
29+
id-token: write
30+
attestations: write
31+
32+
33+
steps:
34+
- name: Tune GitHub Hosted Runner Network
35+
uses: smorimoto/tune-github-hosted-runner-network@v1
36+
37+
- uses: actions/checkout@v5
38+
39+
- name: Set up Docker Buildx
40+
uses: docker/setup-buildx-action@v3
41+
42+
- name: Log in to Container Registry
43+
uses: docker/login-action@v3
44+
with:
45+
registry: ${{ env.REGISTRY }}
46+
username: ${{ github.actor }}
47+
password: ${{ secrets.GITHUB_TOKEN }}
48+
49+
- name: Extract metadata
50+
id: meta
51+
uses: docker/metadata-action@v5
52+
with:
53+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
54+
tags: |
55+
type=raw,value=latest,priority=500
56+
type=raw,value=${{ github.run_number }},priority=400
57+
type=ref,event=branch
58+
type=ref,event=pr
59+
type=sha,prefix={{branch}}-
60+
61+
- name: Build and push Docker image
62+
id: build-and-push
63+
uses: docker/build-push-action@v6
64+
with:
65+
context: .
66+
pull: true
67+
push: true
68+
tags: ${{ steps.meta.outputs.tags }}
69+
labels: ${{ steps.meta.outputs.labels }}
70+
secrets: |
71+
GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}
72+
platforms: linux/amd64, linux/arm64
73+
cache-from: type=gha
74+
cache-to: type=gha,mode=max
75+
76+
- name: Attest Build Provenance
77+
uses: actions/attest-build-provenance@v3
78+
with:
79+
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
80+
subject-digest: ${{ steps.build-and-push.outputs.digest }}
81+
push-to-registry: true
82+
83+
cleanup:
84+
runs-on: ubuntu-latest
85+
needs: [build]
86+
if: always()
87+
88+
permissions:
89+
packages: write
90+
91+
steps:
92+
- name: Cleanup old packages
93+
uses: dataaxiom/ghcr-cleanup-action@v1
94+
with:
95+
token: ${{ secrets.GITHUB_TOKEN }}
96+
package-name: ${{ github.repository }}
97+
keep-last: 10

.gitignore

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Docker
2+
.dockerignore
3+
4+
# Node.js
5+
node_modules/
6+
npm-debug.log*
7+
yarn-debug.log*
8+
yarn-error.log*
9+
10+
# IDE
11+
.vscode/
12+
.idea/
13+
*.swp
14+
*.swo
15+
*~
16+
17+
# OS
18+
.DS_Store
19+
Thumbs.db
20+
21+
# Logs
22+
*.log
23+
24+
# Runtime data
25+
pids
26+
*.pid
27+
*.seed
28+
*.pid.lock
29+
30+
# Coverage directory used by tools like istanbul
31+
coverage/
32+
33+
# nyc test coverage
34+
.nyc_output
35+
36+
# Dependency directories
37+
node_modules/
38+
jspm_packages/
39+
40+
# Optional npm cache directory
41+
.npm
42+
43+
# Optional REPL history
44+
.node_repl_history
45+
46+
# Output of 'npm pack'
47+
*.tgz
48+
49+
# Yarn Integrity file
50+
.yarn-integrity
51+
52+
# dotenv environment variables file
53+
.env
54+
55+
# Temporary folders
56+
tmp/
57+
temp/

Dockerfile

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
FROM python:3.11-slim
2+
3+
# 创建用户和组
4+
RUN groupadd -g 1000 python && \
5+
useradd -m -u 1000 -g python python
6+
7+
# 安装基础依赖
8+
RUN apt-get update && \
9+
apt-get install -y --no-install-recommends \
10+
ca-certificates \
11+
curl \
12+
git \
13+
tini \
14+
gosu \
15+
build-essential && \
16+
rm -rf /var/lib/apt/lists/*
17+
18+
# 安装 Python LSP Server
19+
RUN pip install --no-cache-dir 'python-lsp-server[all]'
20+
21+
# 设置环境变量
22+
ENV UID=1000 USER=python \
23+
GID=1000 GROUP=python \
24+
PYTHON_VERSION=3.11 \
25+
LSP_VERSION=latest
26+
27+
# 复制入口脚本
28+
COPY ./entrypoint.sh /entrypoint.sh
29+
RUN chmod +x /entrypoint.sh
30+
31+
# 切换到非 root 用户
32+
USER python
33+
34+
# 设置工作目录
35+
WORKDIR /home/python
36+
37+
ENTRYPOINT ["/entrypoint.sh"]
38+
CMD ["pylsp"]

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 ReasLab
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# docker-lsp-python
2+
3+
基于 Docker 的 Python Language Server Protocol (LSP) 环境,支持多版本 Python 和 Python LSP Server。
4+
5+
## 特性
6+
7+
- 基于 Python 3.11 的 Python LSP Server
8+
- 包含完整的 Python LSP 插件生态
9+
- 用户和组管理,确保安全的文件权限
10+
- 自动化的 Docker 镜像构建和发布
11+
- 支持多架构构建(amd64, arm64)
12+
13+
## 使用方法
14+
15+
### 使用预构建镜像
16+
17+
预构建的镜像可在 [GHCR](https://github.com/reaslab/docker-lsp-python/pkgs/container/docker-lsp-python) 获取。您可以直接拉取并运行:
18+
19+
```sh
20+
docker pull ghcr.io/reaslab/docker-lsp-python:latest
21+
docker run --rm -it ghcr.io/reaslab/docker-lsp-python:latest
22+
```
23+
24+
### 可用标签
25+
26+
| 镜像标签 | 描述 |
27+
|----------|------|
28+
| `latest` | 最新版本(Python 3.11 + 最新 LSP) |
29+
| `{run_number}` | 构建编号标签(如 `123`|
30+
31+
### 环境变量
32+
33+
`entrypoint.sh` 脚本支持以下环境变量:
34+
35+
- `USER`, `GROUP`: 设置容器内的用户名/组名(默认:`python`
36+
- `UID`, `GID`: 设置用户/组 ID(默认:1000)
37+
- `XDG_CACHE_HOME`: 设置自定义缓存目录(默认:`/home/python/.cache`
38+
39+
示例:
40+
41+
```sh
42+
docker run -e USER=myuser -e UID=1234 ghcr.io/reaslab/docker-lsp-python:latest
43+
```
44+
45+
### 使用 Docker Compose
46+
47+
```yaml
48+
version: '3.8'
49+
services:
50+
python-lsp:
51+
image: ghcr.io/reaslab/docker-lsp-python:latest
52+
ports:
53+
- "2087:2087"
54+
volumes:
55+
- ~/.cache/python-lsp:/home/python/.cache
56+
- ~/.config/python-lsp:/home/python/.config
57+
- ./workspace:/workspace
58+
working_dir: /workspace
59+
environment:
60+
- USER=python
61+
- UID=1000
62+
- GROUP=python
63+
- GID=1000
64+
command:
65+
- pylsp
66+
- --tcp
67+
- --port
68+
- "2087"
69+
- --host
70+
- "0.0.0.0"
71+
```
72+
73+
### 本地开发
74+
75+
1. 克隆仓库:
76+
```sh
77+
git clone https://github.com/reaslab/docker-lsp-python.git
78+
cd docker-lsp-python
79+
```
80+
81+
2. 构建镜像:
82+
```sh
83+
docker build -t docker-lsp-python:latest .
84+
```
85+
86+
3. 运行容器:
87+
```sh
88+
docker run --rm -it docker-lsp-python:latest
89+
```
90+
91+
## 配置
92+
93+
### LSP 配置
94+
95+
容器包含默认的 LSP 配置,位于 `/home/python/.config/python-lsp/config.json`。您可以通过挂载卷来自定义配置:
96+
97+
```sh
98+
docker run -v /path/to/your/config.json:/home/python/.config/python-lsp/config.json ghcr.io/reaslab/docker-lsp-python:latest
99+
```
100+
101+
### 支持的插件
102+
103+
- **代码检查**: pycodestyle, pyflakes, mccabe
104+
- **代码格式化**: autopep8, yapf
105+
- **代码补全**: jedi
106+
- **类型检查**: pylint(默认禁用)
107+
- **其他**: preload, rope
108+
109+
## 自动化构建
110+
111+
此项目使用 GitHub Actions 进行自动化构建:
112+
113+
- **推送构建**: 推送到 main 分支时自动构建
114+
- **手动构建**: 通过 workflow_dispatch 手动触发
115+
- **多架构支持**: 同时构建 amd64 和 arm64 架构
116+
- **标签策略**: `latest` 为最新版本,`{run_number}` 为构建编号
117+
118+
## 开发
119+
120+
### 本地测试
121+
122+
```sh
123+
# 构建镜像
124+
docker build -t docker-lsp-python:latest .
125+
126+
# 运行容器
127+
docker run --rm -it docker-lsp-python:latest
128+
```
129+
130+
## 许可证
131+
132+
本项目采用 MIT 许可证。详见 [LICENSE](./LICENSE) 文件。
133+
134+
## 贡献
135+
136+
欢迎提交 Issue 和 Pull Request!
137+
138+
## 相关项目
139+
140+
- [python-lsp-server](https://github.com/python-lsp/python-lsp-server) - Python LSP Server 实现

docker-compose.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: lsp-python
2+
3+
services:
4+
python-lsp:
5+
image: docker-lsp-python:latest
6+
network_mode: host
7+
environment:
8+
- USER=python
9+
- UID=1000
10+
- GROUP=python
11+
- GID=1000
12+
volumes:
13+
- ~/.cache/python-lsp:/home/python/.cache
14+
- ~/.config/python-lsp:/home/python/.config
15+
- .:/workspace
16+
working_dir: /workspace
17+
command:
18+
- pylsp
19+
- --tcp
20+
- --port
21+
- "2087"
22+
- --host
23+
- "0.0.0.0"
24+
ports:
25+
- "2087:2087"

0 commit comments

Comments
 (0)