Gitlab CI / CD部署Django Docker容器

时间:2020-11-05 07:38:22

标签: python django docker docker-compose gitlab-ci

我一直在为django项目设置gitlab ci / cd配置,该项目将作为容器部署。

这是我尝试过的: CI / CD-

image: creatiwww/docker-compose:latest

services:
  - docker:dind



stages:
    - lint
    - build
    - deploy

variables:
  TAG_LATEST: $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_NAME:latest
  TAG_COMMIT: $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_NAME:$CI_COMMIT_SHORT_SHA

lint:
  stage: lint
  image: python:3.8
  before_script:
    - pip install pipenv
    - pipenv install --dev
  script:
    - pipenv run python -m flake8 --exclude=migrations,settings.py backend
  allow_failure: false

build:
  stage: build
  script:
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY_IMAGE
    - echo "IMAGE_APP_TAG=$TAG_LATEST" >> .env
    - docker-compose build
    - docker-compose push
  only:
    - master

deploy-to-prod:
  stage: deploy
  script:
    - eval $(ssh-agent -s)
    - echo "${ID_RSA}" | tr -d '\r' | ssh-add - > /dev/null
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY_IMAGE
    - echo "IMAGE_APP_TAG=$TAG_LATEST" >> .env
    - echo "SECRET_KEY=$SECRET_KEY" >> .env
    - docker-compose -H "ssh://$SERVER_USER@$SERVER_IP" down --remove-orphans
    - docker-compose -H "ssh://$SERVER_USER@$SERVER_IP" pull
    - docker-compose -H "ssh://$SERVER_USER@$SERVER_IP" up -d
  only:
    - master
  when: manual

管道成功,但是在检查容器的日志时,我得到以下输出-

python: can't open file 'manage.py': [Errno 2] No such file or directory

我在docker ps中的图像字段也为空。

请帮助

1 个答案:

答案 0 :(得分:0)

将代码放入Docker_Compose.yml

版本:“ 3.7”

服务: 后端: 版本号:./project_name 命令:sh -c“ cd项目&& python3 manage.py迁移&& python3 manage.py运行服务器0.0.0.0:8000” 端口: -8000:8000 取决于: - D b network_mode:主机

db: 图片:postgres:12.0-高山 network_mode:主机 端口: -5432:5432 数量: -postgres_data:/ var / lib / postgresql / data / 环境: -POSTGRES_USER ='db_user' -POSTGRES_PASSWORD ='db_password' -POSTGRES_DB ='db_name'

卷: postgres_data: