github 中的 dockerfile 运行,有找不到模块的问题
dockerfile run in github, having module not found issue
我很确定如果我在本地使用当前 docker 文件进行构建,我可以 运行 图像
# pull the official docker image
FROM python:3.9.4-slim
# install requirements
COPY backend/copium_api/requirements.txt .
RUN pip install -r requirements.txt
# copy project
COPY . .
EXPOSE 8715
CMD ["python", "-m", "server"]
但是当我在 Github 操作中执行此操作时,我看到了这个问题
Successfully installed anyio-3.5.0 asgiref-3.5.0 beautifulsoup4-4.10.0 bs4-0.0.1 certifi-2021.10.8 charset-normalizer-2.0.12 click-8.1.2 fastapi-0.75.1 h11-0.13.0 idna-3.3 pydantic-1.9.0 requests-2.27.1 sniffio-1.2.0 soupsieve-2.3.2 starlette-0.17.1 typing-extensions-4.1.1 urllib3-1.26.9 uvicorn-0.17.6
WARNING: Running pip as root will break packages and permissions. You should install packages reliably by using venv: https://pip.pypa.io/warnings/venv
WARNING: You are using pip version 21.1.1; however, version 22.0.4 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
Removing intermediate container 64e026fcc3df
---> 23a3ab0ffc48
Step 4/7 : COPY . .
---> ad6a4b0c1ab5
Step 5/7 : EXPOSE 8715
---> Running in 0ec8c1464e7f
Removing intermediate container 0ec8c1464e7f
---> 893d7c4e024c
Step 6/7 : CMD ["python", "-m", "server"]
---> Running in 553478071e4d
Removing intermediate container 553478071e4d
---> 0282cdbca6f9
Step 7/7 : LABEL org.opencontainers.image.source="https://github.com/intothefantasy/copium-mtg"
---> Running in b399cef673af
Removing intermediate container b399cef673af
---> 88faeb304247
Successfully built 88faeb304247
Successfully tagged ghcr.io/intothefantasy/copium:latest
/usr/local/bin/python: No module named server
Error: Process completed with exit code 1.
出现此错误无法启动任何内容
/usr/local/bin/python: No module named server
我能知道我应该如何编写这个 docker 文件才能正常工作吗?
github动作
name: Docker Image CI
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.DOCKER_TOKEN }}
- uses: actions/checkout@v3
- name: Build the Docker image
run: |
docker build . --file backend/copium_api/Dockerfile --tag app:latest
docker run app:latest
docker push app:latest
我的 init.py 文件在服务器目录下
from server.api.router import api_router
from common.constant import API_PREFIX, API_TITLE, API_DOCS, OPENAPI_URL
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI(
title = API_TITLE,
docs_url = API_DOCS,
openapi_url = OPENAPI_URL,
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(api_router, prefix=API_PREFIX)
只是一个普通的fastapi调用
您正在使用 --file backend/copium_api/Dockerfile
,但所有构建上下文都是相对于 .
的,这意味着您的文件未按预期复制。
您有 3 个选项:
cd
进入构建前的目录(并将COPY backend/copium_api/requirements.txt .
更改为COPY requirements.txt .
)
- 将您的第二个
COPY
语句更改为 COPY backend/copium_api/* .
- 将您的入口点更改为
python -m backend/copium_api/server
建议的更改:
Dockerfile
:
# pull the official docker image
FROM python:3.9.4-slim
# install requirements
COPY requirements.txt .
RUN pip install -r requirements.txt
# copy project
COPY . .
EXPOSE 8715
CMD ["python", "-m", "server"]
Github 动作:
name: Docker Image CI
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.DOCKER_TOKEN }}
- uses: actions/checkout@v3
- name: Build the Docker image
run: |
cd backend/copium_api/
docker build --tag app:latest .
docker run app:latest
docker push app:latest
我很确定如果我在本地使用当前 docker 文件进行构建,我可以 运行 图像
# pull the official docker image
FROM python:3.9.4-slim
# install requirements
COPY backend/copium_api/requirements.txt .
RUN pip install -r requirements.txt
# copy project
COPY . .
EXPOSE 8715
CMD ["python", "-m", "server"]
但是当我在 Github 操作中执行此操作时,我看到了这个问题
Successfully installed anyio-3.5.0 asgiref-3.5.0 beautifulsoup4-4.10.0 bs4-0.0.1 certifi-2021.10.8 charset-normalizer-2.0.12 click-8.1.2 fastapi-0.75.1 h11-0.13.0 idna-3.3 pydantic-1.9.0 requests-2.27.1 sniffio-1.2.0 soupsieve-2.3.2 starlette-0.17.1 typing-extensions-4.1.1 urllib3-1.26.9 uvicorn-0.17.6
WARNING: Running pip as root will break packages and permissions. You should install packages reliably by using venv: https://pip.pypa.io/warnings/venv
WARNING: You are using pip version 21.1.1; however, version 22.0.4 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
Removing intermediate container 64e026fcc3df
---> 23a3ab0ffc48
Step 4/7 : COPY . .
---> ad6a4b0c1ab5
Step 5/7 : EXPOSE 8715
---> Running in 0ec8c1464e7f
Removing intermediate container 0ec8c1464e7f
---> 893d7c4e024c
Step 6/7 : CMD ["python", "-m", "server"]
---> Running in 553478071e4d
Removing intermediate container 553478071e4d
---> 0282cdbca6f9
Step 7/7 : LABEL org.opencontainers.image.source="https://github.com/intothefantasy/copium-mtg"
---> Running in b399cef673af
Removing intermediate container b399cef673af
---> 88faeb304247
Successfully built 88faeb304247
Successfully tagged ghcr.io/intothefantasy/copium:latest
/usr/local/bin/python: No module named server
Error: Process completed with exit code 1.
出现此错误无法启动任何内容
/usr/local/bin/python: No module named server
我能知道我应该如何编写这个 docker 文件才能正常工作吗?
github动作
name: Docker Image CI
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.DOCKER_TOKEN }}
- uses: actions/checkout@v3
- name: Build the Docker image
run: |
docker build . --file backend/copium_api/Dockerfile --tag app:latest
docker run app:latest
docker push app:latest
我的 init.py 文件在服务器目录下
from server.api.router import api_router
from common.constant import API_PREFIX, API_TITLE, API_DOCS, OPENAPI_URL
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI(
title = API_TITLE,
docs_url = API_DOCS,
openapi_url = OPENAPI_URL,
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(api_router, prefix=API_PREFIX)
只是一个普通的fastapi调用
您正在使用 --file backend/copium_api/Dockerfile
,但所有构建上下文都是相对于 .
的,这意味着您的文件未按预期复制。
您有 3 个选项:
cd
进入构建前的目录(并将COPY backend/copium_api/requirements.txt .
更改为COPY requirements.txt .
)- 将您的第二个
COPY
语句更改为COPY backend/copium_api/* .
- 将您的入口点更改为
python -m backend/copium_api/server
建议的更改:
Dockerfile
:
# pull the official docker image
FROM python:3.9.4-slim
# install requirements
COPY requirements.txt .
RUN pip install -r requirements.txt
# copy project
COPY . .
EXPOSE 8715
CMD ["python", "-m", "server"]
Github 动作:
name: Docker Image CI
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.DOCKER_TOKEN }}
- uses: actions/checkout@v3
- name: Build the Docker image
run: |
cd backend/copium_api/
docker build --tag app:latest .
docker run app:latest
docker push app:latest