Issue with Dockerising Django app using docker-compose

☆樱花仙子☆ 提交于 2020-08-08 05:40:42

问题


I am new to Docker and I want to dockerise the Django app to run as a container. Followed as below.

Here is the Dockerfile

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

Here is docker-compose.yml conf

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

well on my docker shell i point to Dockerfile directory, if i run an ls command from y path i see the manage.py file, but if i run:

docker-compose up

i get this error:

web_1 | python: can't open file 'manage.py': [Errno 2] No such file or directory core_web_1 exited with code 2

Why my app don't find manage.py file that is in the same position as the "docker-compose up" command is?

PS: No /code folder is created when i run docker-compose command. Is it correct?

So many thanks in advance


回答1:


try to edit your Dockerfile like this:

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"]

and remove command: python manage.py runserver 0.0.0.0:8000 from compose

I assumed that the manage.py is in /code/ folder, since you have WORKDIR /code in the dockerfile then the server will be created in the build stage and the files will be copied to it




回答2:


as error states - manage.py is not in that directory. And as far as I can see you are copying requirements.txt twice.

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


来源:https://stackoverflow.com/questions/57967183/issue-with-dockerising-django-app-using-docker-compose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!