3

I just start a sample django app. And use docker to run it. My docker image like:

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

My docker-compose.yml file:

version: '2'
services:
  django:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"

When I run docker-compose up command,it build successfully but failed in running command: python manage.py runserver 0.0.0.0:8000,it complained python: can't open file 'manage.py': [Errno 2] No such file or directory.

Is this a bug in docker for windows? Because I just follow the docs of docker Quickstart: Docker Compose and Django

Thank for you help!

You Gakukou
  • 643
  • 7
  • 16
  • Did you complete the `Create a Django project` and the `Connect the database` part of the tutorial? – vabada Sep 30 '16 at 12:39
  • Yes. I create the django project and run it in a virtual python enviroment. It works well. I just use sqlite3 database. – You Gakukou Sep 30 '16 at 12:50
  • So you did execute this line? `docker-compose run web django-admin.py startproject composeexample .` – vabada Sep 30 '16 at 12:52
  • I change the service name 'web' to 'django'. And my project created in host rather than use 'docker-compose run web django-admin.py startproject '. – You Gakukou Sep 30 '16 at 12:57
  • I follow the official docs again and use `docker-compose run web django-admin.py startproject composeexample .` to create a new django project. Then I use `docker-compose up` and the comtainer run successfully. But i can't find the django project file in my current directory. In addition, can't we run a existed django project in a docker container? – You Gakukou Sep 30 '16 at 13:21
  • How does your directory structure look like? – Railslide Sep 30 '16 at 13:22
  • Now my directory only has three file: D:\Users\zmrenwu\DockerDemo\docker-compose-example `docker-compose.yml` `Dockerfile` `requirements.txt` I don't know where is the new project structure. According to the official docs,it should show in current directory(.) – You Gakukou Sep 30 '16 at 13:37

1 Answers1

4

I think you either missed this step: docker-compose run web django-admin.py startproject composeexample . or you're using a directory that isn't available to the Virtual Machine that is running docker.

If it works when you remove volumes: .:/code from the Compose file, then you know the issue is the volumes.

I believe by default only the users home directory is shared with the VM, so if you create a project outside of that tree, you won't have access to the files from volumes.

dnephin
  • 25,944
  • 9
  • 55
  • 45
  • Thanks very much! This solve my problem. But if I can't mount the directory in host to VM, how could I share data between them? Is their any way to make it possible that VM can access the file on host? – You Gakukou Oct 01 '16 at 00:28
  • Thank again! You save my life. According to your analysis I search the answer in [Docker official forum](https://forums.docker.com/t/volume-mounts-in-windows-does-not-work/10693/5). The solution is we should enable host share the drive to docker container in docker settings. – You Gakukou Oct 01 '16 at 02:40