【问题标题】:Issue with Dockerising Django app using docker-compose使用 docker-compose Dockerising Django 应用程序的问题
【发布时间】:2020-01-17 21:37:21
【问题描述】:

我是 Docker 新手,我想将 Django 应用程序 dockerise 以作为容器运行。如下。

这里是Dockerfile

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

这里是docker-compose.ymlconf

version: '3'

networks:
    mynetwork:
        driver: bridge

services:

  db:
    image: postgres
    ports:
      - "5432:5432"
    networks:
      - mynetwork
    environment:
      POSTGRES_USER: xxxxx
      POSTGRES_PASSWORD: xxxxx

  web:
    build: .
    networks:
      - mynetwork
    links:
      - db
    environment:
      SEQ_DB: cath_local
      SEQ_USER: xxxxx
      SEQ_PW: xxxxx
      PORT: 5432
      DATABASE_URL: postgres://xxxxx:xxxxx@db:5432/cath_local

    command: python manage.py runserver 0.0.0.0:8000

    volumes:
      - .:/code

    ports:
      - "8000:8000"

    depends_on:
      - db

在我的 docker shell 上,我指向 Dockerfile 目录,如果我从 y 路径运行 ls 命令,我会看到 manage.py 文件,但如果我运行:

docker-compose up

我得到这个错误:

web_1 | python:无法打开文件'manage.py':[Errno 2]没有这样的文件或目录 core_web_1 以代码 2 退出

为什么我的应用找不到与“docker-compose up”命令位置相同的 manage.py 文件?

PS:当我运行 docker-compose 命令时,没有创建 /code 文件夹。对吗?

提前非常感谢

【问题讨论】:

    标签: python django docker docker-compose


    【解决方案1】:

    尝试像这样编辑您的 Dockerfile:

    FROM python:3
    ENV PYTHONUNBUFFERED 1
    RUN mkdir /code
    WORKDIR /code
    COPY requirements.txt /code/
    RUN pip install -r requirements.txt
    COPY . /code/
    CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
    

    并从compose 中删除command: python manage.py runserver 0.0.0.0:8000

    我假设manage.py/code/ 文件夹中,因为WORKDIR /codedockerfile 中,那么服务器将在构建阶段创建,文件将被复制到它

    【讨论】:

    • 似乎开始但现在我收到错误:db_1 | 2019-09-17 04:01:06.277 UTC [21] LOG:数据库系统于 2019-09-17 03:43:28 UTC core_web_1 以代码 0 退出
    • 检查日志docker logs db,我认为您需要使用ENV 为数据库添加根密码,就像您的compose 中的另一个一样
    • 2019-09-17 03:29:37.795 UTC [1] LOG:数据库系统已准备好接受连接 2019-09-17 03:30:11.297 UTC [1] LOG:收到智能关闭请求 2019-09-17 03:30:11.409 UTC [1] 日志:后台工作人员“逻辑复制启动器”(PID 27)退出,退出代码为 1 2019-09-17 03:30:11.411 UTC [22] 日志:关闭2019-09-17 03:30:11.463 UTC [1] 日志:数据库系统已关闭
    • 2019-09-17 03:32:21.968 UTC [1] 日志:侦听 IPv4 地址“0.0.0.0”,端口 5432 2019-09-17 03:32:21.971 UTC [1]日志:监听 IPv6 地址“::”,端口 5432 2019-09-17 03:32:21.975 UTC [1] 日志:监听 Unix 套接字“/var/run/postgresql/.s.PGSQL.5432”2019- 09-17 03:32:22.313 UTC [21] LOG:数据库系统于 2019-09-17 03:30:11 UTC 关闭
    • 是的,我尝试了..但没有任何改变...我找不到杀死我的 db proc 的原因
    【解决方案2】:

    作为错误状态 - manage.py 不在该目录中。 据我所知,您复制了两次requirements.txt

    FROM python:3
    ENV PYTHONUNBUFFERED 1
    RUN mkdir /code
    WORKDIR /code
    # move following line above 'pip install' and make sure that `manage.py` exists on the same directory as `requirements.txt`
    COPY . /code/
    # remove following line
    # COPY requirements.txt /code/
    RUN pip install -r requirements.txt
    # you can define CMD here, but for dev env it is much more convenient to define it on docker-compose.yml, so you do not need to rebuild the image in case of some changes of the COMMAND
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-19
      • 1970-01-01
      • 2021-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多