簡體   English   中英

dockerfile 在 github 中運行,有找不到模塊的問題

[英]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

我能知道我應該如何寫這個 dockerfile 才能工作嗎?

這是我目前的項目文件樹結構

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 個選擇:

  1. cd進入構建前的目錄(並將COPY backend/copium_api/requirements.txt.更改為COPY requirements.txt.
  2. 將您的第二個COPY語句更改為COPY backend/copium_api/*.
  3. 將您的入口點更改為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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM