如何配置docker测试以在Travis CI上正确运行?

时间:2020-09-16 04:44:44

标签: python docker unix docker-compose travis-ci

我的前端(Vuejs)和后端(django)都有不同的Dockerfile,还有两个docker-compose文件来运行其开发服务器和测试阶段。它们在我的机器上运行得很好。但是,在Travis-CI上运行测试版本时,它们会出错。

docker-compose.yml

version: "3.4"

services:
  database:
    image: postgres:alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=uni69204passwd
      - POSTGRES_DB=uni-db
  backend:
    build: ./backend/
    volumes:
      - ./backend:/app
    ports:
      - "8000:8000"
    depends_on:
      - database
  frontend:
    build: ./frontend/
    command: npm run dev
    ports:
      - "3000:3000"
    volumes:
      - ./frontend:/app
    depends_on:
      - database
      - backend

volumes:
  backend:
  frontend:
  postgres_data:

docker-compose.test.yml

version: "3.4"

services:
  cypress:
    image: "cypress/included:3.4.0"
    environment:
      - CYPRESS_baseUrl=http://frontend:3000
    working_dir: /testing
    volumes:
      - ./frontend/:/testing
    entrypoint: "sh -c 'sleep 15; ./wait-for-it.sh frontend:3000 --strict -- cypress run'"

volumes:
  cypress:

.travis.yml

language: node_js
node_js:
  - 14.8.0
services:
  - docker
before_script:
  - docker-compose run frontend sh -c "npm install"
script:
  - docker-compose -f docker-compose.yml -f docker-compose.test.yml up --abort-on-container-exit
  - docker-compose run --entrypoint "" backend pytest --flake8

notifications:
  email: false

frontend/Dockerfile

FROM node:lts-alpine

EXPOSE 3000

# Use `node` user that `node:lts-alpine` provides
RUN mkdir -p /app && chown -R node /app

USER node

WORKDIR /app

backend/Dockerfile

FROM python:3.8.5-alpine

EXPOSE 8000

# install psycopg2, pillow and argon2_cffi dependencies
RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev libffi-dev openssl-dev jpeg-dev zlib-dev

RUN mkdir -p /app && adduser -D -h /app -u 1000 python && chown -R python /app

WORKDIR /app

ENV SECRET_KEY=e75)h*54&*^&(secret-keyd+=^%mb0n \
    SQL_USER=user \
    SQL_PASSWORD=uni69204passwd \
    SQL_HOST=database \
    SQL_PORT=5432 \
    SQL_ENGINE=django.db.backends.postgresql \
    SQL_DATABASE=uni-db \
    PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1

COPY requirements.txt /tmp/requirements.txt
RUN pip install -r /tmp/requirements.txt

USER python

ENTRYPOINT [ "python", "manage.py", "runserver", "0.0.0.0:8000" ]

这是我在测试构建期间遇到的错误:

npm WARN checkPermissions Missing write access to /app
npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /app
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/app'
npm ERR!  [Error: EACCES: permission denied, access '/app'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/app'
npm ERR! }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.
npm ERR! A complete log of this run can be found in:
npm ERR!     /home/node/.npm/_logs/2020-09-15T18_05_28_175Z-debug.log

1 个答案:

答案 0 :(得分:0)

在您的前端 Dockerfile 中,当您在创建 chown 目录后对其进行 /apping 时,请尝试在用户名后添加 :

RUN mkdir -p /app && chown -R node: /app

这将向用户和组 node 显示 /app。

也可能是您在 docker-compose 文件中使用 - ./frontend:/app 卷定义覆盖了它。