Home > Software design >  dockerfile run in github, having module not found issue
dockerfile run in github, having module not found issue

Time:04-08

i am pretty sure if i am doing the build in my local with this current docker file, i can run the image

# 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"]

but as i am doing this on Github action i am seeing this issue

 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.

not able to start anything with this error

/usr/local/bin/python: No module named server

can i know how should i write this dockerfile so it can work?

here is my project file tree structure currently

github action

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

under my init.py file in server directory

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)

just a normal fastapi calling

CodePudding user response:

You're using --file backend/copium_api/Dockerfile but all build context is relative to . meaning your files are not copied as you expect.
You have 3 options:

  1. cd into the directory before building (and change COPY backend/copium_api/requirements.txt . to COPY requirements.txt .)
  2. Change your second COPY statement to COPY backend/copium_api/* .
  3. Change your entrypoint to python -m backend/copium_api/server

Suggested changes:
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 action:

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
  • Related