Skip to content

Commit 5ad5318

Browse files
committed
add dev docker and instruction to watch file changes and update on the fly
1 parent 0bff488 commit 5ad5318

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@
1515

1616
# test script
1717
test.sh
18+
19+
.env
20+

Dockerfile-dev

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM python:3.10.0-alpine
2+
3+
ENV PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple
4+
RUN pip install --upgrade pip
5+
RUN pip install mkdocs-material==9.5.5 mkdocs-glightbox
6+
7+
WORKDIR /hello-algo
8+
9+
EXPOSE 8000

Makefile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
VERBOSE=--verbose
2+
3+
all: # do nothing by default
4+
@echo "usage: make [build-docker|run-dev|dev|stop|sh]"
5+
6+
build-docker: # build dev container
7+
# docker build -t hello-algo-dev -f Dockerfile-dev .
8+
docker compose -f docker-compose-dev.yml build
9+
10+
buildx-docker:
11+
docker build -t hello-algo-dev -f Dockerfile-dev . --load
12+
13+
run-dev: # run in background
14+
docker compose -f docker-compose-dev.yml up -d
15+
16+
dev: # run dev container
17+
docker compose -f docker-compose-dev.yml $(VERBOSE) up
18+
19+
stop: # stop dev container
20+
docker compose -f docker-compose-dev.yml down
21+
22+
sh: # go to dev container shell
23+
docker exec -it hello-algo-dev sh

docker-compose-dev.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
services:
2+
hello-algo-dev:
3+
build:
4+
context: .
5+
dockerfile: Dockerfile-dev
6+
image: hello-algo-dev
7+
container_name: hello-algo-dev
8+
ports:
9+
- "${HTTP_PORT:-8000}:8000"
10+
# tty: true
11+
# stdin_open: true
12+
entrypoint:
13+
# - "/bin/sh"
14+
- mkdocs
15+
- serve
16+
- -a
17+
- 0.0.0.0:8000
18+
volumes:
19+
- ./:/hello-algo
20+
- ./docs:/hello-algo/build/docs
21+
- ./overrides:/hello-algo/build/overrides

docs/chapter_appendix/contribution.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,36 @@ docker-compose up -d
4545
```shell
4646
docker-compose down
4747
```
48+
49+
### 开发预览
50+
51+
上述 Docker 部署生成的是最终版本。在开发环境中,如果要实时预览修改效果,可以使用以下命令:
52+
53+
```shell
54+
mkdocs serve
55+
```
56+
57+
然后在浏览器中打开 `http://localhost:8000` 即可实时预览。
58+
59+
如果本地没有安装 mkdocs 环境,也可以使用 Docker 方式预览。
60+
61+
```shell
62+
make build # 构建 Docker 开发镜像
63+
make run-dev # 启动 Docker 开发镜像到后台
64+
```
65+
66+
镜像启动后,会监听`8000`端口,与上一节的 Docker 部署中一样,这是特意为之的,目标是为了减少端口占用,同时只启动一个 Docker 容器,对不熟悉 Docker 的同学少造成一些困扰。当然,如果你想启动到另外的端口,只需要在 `hello-algo` 目录下创建一个 `.env` 文件,内容如下。
67+
68+
```shell
69+
HTTP_PORT=9000
70+
```
71+
72+
此外,在日常使用中还可以使用如下命令。
73+
74+
```shell
75+
make run # 启动 Docker 开发镜像开前台,便于调试
76+
make stop # 停止 Docker 开发镜像
77+
make shell # 进入 Docker 开发镜像的 shell
78+
```
79+
80+
如果没有 `make` 工具,则可以参照 `Makefile` 文件中对应的命令手动执行。

0 commit comments

Comments
 (0)