2017-08-21 47 views
0

我嘗試在gitlab ci中運行我的django測試python manage.py test。因此我正在使用一個泊塢窗圖像。泊塢窗圖像建立很好,但是當它運行在gitlab的測試中,我得到ImportError: No module named 'django'無法在Docker鏡像上導入Django GitLab CI

Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment? 

見我.gitlab-ci.yml

image: registry.gitlab.com/app/core:latest 
services: 
    - postgres:latest 
stages: 
    - test 

variables: 
    SECRET_KEY: test-secret 
    POSTGRES_DB: ... 
    POSTGRES_USER: ... 
    POSTGRES_PASSWORD: ... 

python_tests: 
    stage: test 
    before_script: 
     - export DATABASE_NAME=... 
     - export DATABASE_USER=... 
     - export DATABASE_PASSWORD=... 
     - export DATABASE_HOST=postgres 
     - source /app/venv/bin/activate 
    script: 
     - python manage.py test 

Dockerfile

FROM ubuntu:16.04 

RUN apt-get update -y -qq 
RUN apt-get install -y -qq build-essential libffi-dev libpq-dev libfontconfig1 
RUN apt-get install -y -qq python3 python3-dev python3-pip 
RUN apt-get install -y -qq libpq-dev 
RUN apt-get install -y -qq nodejs npm 

WORKDIR /app 

# pip 
COPY requirements.txt /app 
RUN pip3 install --upgrade pip 
RUN pip3 install virtualenv 
RUN virtualenv --no-site-packages venv 
RUN . venv/bin/activate 
RUN pip3 install -r /app/requirements.txt 

# npm 
RUN ln -s `which nodejs` /usr/bin/node 
COPY web/vueapp/package.json /app 
RUN npm install 

回答

0

那麼你有一個問題你的方法。考慮你的運行,下面的語句在Dockerfile

RUN . venv/bin/activate 
RUN pip3 install -r /app/requirements.txt 

上述兩個聲明都像開兩個終端,在一個執行. venv/bin/activate和一個pip3 install -r /app/requirements.txt

所以你的環境被激活,你讓它關閉,然後下一個PIP3語句安裝在全局程序包上。所以,你的代碼更改下面

RUN . venv/bin/activate && pip3 install -r /app/requirements.txt 

當你正在運行腳本您正在激活的環境,然後運行的是空白,沒有包裝的虛擬環境中蟒manager.py。所以上面的改變應該爲你解決這個問題