我正在嘗試在我的 python alpine docker 容器中安裝預提交鉤子,以便我可以通過
docker-compose run ... "/py/bin/pre-commit run --all-files"
我首先安裝 git,然后運行 ??git init,然后安裝鉤子。一切正常,我已經輸入了一個“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:
有誰知道問題可能是什么?
uj5u.com熱心網友回復:
volumes:
掛載隱藏了映像目錄中的所有內容,/app
并將其替換為主機內容。如果您的 Dockerfile 使用COPY
命令或RUN
ning 步驟重新組織檔案結構以生成檔案,那么您也不能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']
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/498416.html
下一篇:在合并沖突中如何防止修改提交?