Wbat是运行我的Django服务器的docker命令吗?

问题描述 投票:0回答:1

我正在尝试对本地Django / MySql设置进行Docker化。我有这个目录和文件结构...

apache
docker-compose.yml
web
    - manage.py
    - venv
    - requirements.txt
    - ...

下面是我正在使用的docker-compose.yml文件...

version: '3'  

services:
  web:
    restart: always
    build: ./web
    expose:
      - "8000"
    links:
      - mysql:mysql
    volumes:
      - web-django:/usr/src/app
      - web-static:/usr/src/app/static
    #env_file: web/venv
    environment:
      DEBUG: 'true'
    command: [ "python", "./web/manage.py runserver 0.0.0.0:8000" ]

  mysql:
    restart: always
    image: mysql:5.7
    environment:
      MYSQL_DATABASE: 'maps_data'
      # So you don't have to use root, but you can if you like
      MYSQL_USER: 'chicommons'
      # You can use whatever password you like
      MYSQL_PASSWORD: 'password'
      # Password for root access
      MYSQL_ROOT_PASSWORD: 'password'
    ports:
      - "3406:3406"
    expose:
      # Opens port 3406 on the container
      - '3406'
    volumes:
      - my-db:/var/lib/mysql

volumes:
  web-django:
  web-static:
  my-db:

但是我什么时候跑

docker-compose up

我收到类似以下的错误

maps_web_1 exited with code 2
web_1    | python: can't open file './web/manage.py runserver 0.0.0.0:8000': [Errno 2] No such file or directory
maps_web_1 exited with code 2
maps_web_1 exited with code 2
web_1    | python: can't open file './web/manage.py runserver 0.0.0.0:8000': [Errno 2] No such file or directory
maps_web_1 exited with code 2

我应该使用另一种方式来引用manage.py文件吗?

django python-3.x docker server docker-compose
1个回答
0
投票

command:指令中,如果您使用数组语法,则负责将命令分解为单词。如您所显示的,您正在shell提示符下运行python "manage.py runserver 0.0.0.0:8000"的等效命令,并且有义务将整个命令和选项视为要运行的脚本的文件名,包括空格。如果您将其分解为一个单词,它将更好地工作

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

但是实际上根本没有理由在docker-compose.yml中指定此名称。无论您如何运行,这都是您要运行以启动容器的默认命令,因此它应该是映像的Dockerfile

中的默认命令。
...
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

[在现代Docker上您根本不需要links:(Docker Compose automatically sets up inter-container networking for you)。您绝对不想在应用程序代码上挂载命名卷:这隐藏了映像中的内容,并且(因为您已经告诉Docker这是关键的用户数据),如果您尝试尝试,它将强制Docker使用旧版本的应用程序更新您的图片。

为您提供了一个更简单的docker-compose.yml文件:

version: '3'      
services:
  web:
    restart: always
    build: ./web
    ports:           # to access the container from outside
      - "8000:8000"
    environment:
      DEBUG: 'true'

  mysql:
    restart: always
    image: mysql:5.7
    environment:
      MYSQL_DATABASE: 'maps_data'
      MYSQL_USER: 'chicommons'
      MYSQL_PASSWORD: 'password'
      MYSQL_ROOT_PASSWORD: 'password'
    ports:
      - "3406:3306"  # second port is always container-internal port
    volumes:
      - my-db:/var/lib/mysql

volumes:
  my-db:
© www.soinside.com 2019 - 2024. All rights reserved.