我正试图在我的python alpine docker容器中安装预提交钩子,以便我可以通过
运行它们docker-compose run ... "/py/bin/pre-commit run --all-files"
我先安装git,然后运行git init,然后安装hooks。一切正常,我输入了一个"ls -a"在.git文件夹显示的构建过程中进行调试的命令。
.
..
.flake8
.git
.pre-commit-config.yaml
app
core
manage.py
然而,在构建完成后,我运行
docker-compose run ... "ls -a"
.git文件夹消失了。
因此,运行预提交会产生以下错误:An error has occurred: FatalError: git failed. Is it installed, and are you in a Git repository directory?
Check the log at /home/django-user/.cache/pre-commit/pre-commit.log
ERROR: 1
下面是我的Dockerfile的完整代码:
FROM python:3.9-alpine3.13
LABEL maintainer="dummy-maintainer"
ENV PYTHONUNBUFFERED 1
# Add the requirements files temporarily to the container
# to intall the python dependencies.
COPY ./requirements.txt /tmp/requirements.txt
COPY ./requirements-dev.txt /tmp/requirements-dev.txt
# Add the app to the container.
COPY ./app /app
# Add the pre-commit configuration to the container.
COPY ./.pre-commit-config.yaml /app/.pre-commit-config.yaml
# Change the working directory to the app directory
WORKDIR /app
# Use port 8000 for communication
EXPOSE 8000
ARG DEV=false
# 1. Create a virtual environment for the python dependencies.
# 2. Install the postgres client and the build dependencies for the postgres python adapter.
# 3. Create a virtual dependencies folder for the build dependencies. (Makes cleaning up easier)
# 4. Install the python dependencies via pip.
# 5. If this is the development mode, install further python dependencies needed for development.
# 6. Furthermore, create an empty git repository to run pre-commit checks on the source code.
RUN python -m venv /py &&
/py/bin/pip install --upgrade pip &&
apk add --update --no-cache postgresql-client &&
apk add --update --no-cache --virtual .tmp-build-deps
build-base postgresql-dev musl-dev gcc libc-dev linux-headers postgresql-dev libffi-dev &&
/py/bin/pip install -r /tmp/requirements.txt &&
if [ $DEV = "true" ];
then /py/bin/pip install -r /tmp/requirements-dev.txt &&
apk add --update --no-cache git &&
git init . &&
/py/bin/pre-commit install-hooks &&
ls -a;
fi &&
rm -rf /tmp &&
apk del .tmp-build-deps &&
adduser
--disabled-password
django-user
# Add the py folder to the path to simplify python commands
ENV PATH="/py/bin:$PATH"
USER django-user
这是我的docker-compose.yml:
version: "3.9"
services:
app:
build:
context: .
args:
- DEV=true
ports:
- "8000:8000"
volumes:
- ./app:/app
command: >
sh -c "python manage.py wait_for_db &&
python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"
environment:
- DB_HOST=db
- DB_NAME=devdb
- DB_USER=devuser
- DB_PASS=changeme
depends_on:
- db
db:
image: postgres:13-alpine
volumes:
- dev-db-data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=devdb
- POSTGRES_USER=devuser
- POSTGRES_PASSWORD=changeme
volumes:
dev-db-data:
有谁知道问题是什么吗?
volumes:
挂载隐藏图像的/app
目录中的所有内容,并用主机内容替换它。如果你有一个设置,你的Dockerfile用COPY
命令或RUN
宁步骤重新组织文件结构来生成文件,你也不能在同一个目录上使用volumes:
挂载。
所以在您的情况下,您有一个./.pre-commit-config.yaml
文件和一个./app/manage.py
脚本。在Dockerfile中,你正在合并这两个目录,所以你有/app/.pre-commit-config.yaml
和/app/manage.py
;但是随后volumes:
将容器的/app
目录替换为主机的./app
目录,没有预提交文件。
我通常在这里给出的建议是不要试图将所有内容都强行放入Docker空间。在你的主机系统上,你需要普通的开发工具,比如文本编辑器和Web浏览器,你甚至可能已经预装了Python;也安装Git,你就可以直接运行git commit
而不需要docker-compose run app
包装器。
如果在Docker中只使用工具对你来说很重要,那么你需要使容器文件系统的布局与主机的完全匹配。
FROM python:3.9-alpine
RUN adduser --disabled-password django-user
# Create and switch to a directory to hold the application
WORKDIR /app
# Copy and install the Python-level requirements
COPY requirements*.txt ./
RUN apk add --virtual .build-deps ...
&& pip install -r requirements.txt
&& apk del .build-deps
# Copy in the entire source tree
COPY ./ ./
# Explain how to run the container
WORKDIR /app/app
EXPOSE 8000
USER django-user
ENTRYPOINT ["../entrypoint.sh"] # runs wait_for_db, migrate
CMD ["./manage.py", "runserver", "0.0.0.0:8000"]
现在,由于映像具有与主机系统完全相同的布局,您可以绑定挂载整个主机源代码树,也可以仅挂载app
子目录。
volumes: ['.:/app']
volumes: ['./app:/app/app']