Mr.WRPST
Mr.WRPST

Reputation: 21

How to fix python: can't open file '//manage.py': [Errno 2] No such file or directory in Django

How to fix Remainder of file ignored python: can't open file '//manage.py': \[Errno 2\] No such file or directory when use command docker-compose up

This is all my code, and I've also attached an image file.

dockerfile files

FROM python:3.10

ENV PYTHONUNBUFFERED 1 

COPY requirements.txt .
RUN pip install -r requirements.txt
 
EXPOSE 8000
CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]

docker-compose.yml:

version: '3.7'
services:
  db:
    image: mariadb:10
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD = mariadb
      - MYSQL_DATABASE = mariadb
      - MYSQL_USER = mariadb
      - MYSQL_PASSWORD = mariadb
      - MARIADB_ROOT_PASSWORD=mysecretpassword
    ports:
      - 3306:3306
    volumes:
      - "mysqldata:/var/lib/mysql"  

  web:
    build:
      context: .
      dockerfile: Dockerfile
    restart: always
    command: python manage.py runserver 0.0.0.0:8000
    environment:
      - DATABASE_URL=mariadb+mariadbconnector://user:mariadb@db:3306/mariadb
    ports:
      - "8000:8000"
    depends_on:
      - db

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080
    depends_on:
      - db
      
volumes:
  mysqldata:

setting.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mariadb',
        'USER': 'mariadb',
        'PASSWORD': 'mariadb',
        'HOST': '127.0.0.1',
        'PORT': 3306,
    }
}

file image Django

Upvotes: 1

Views: 849

Answers (1)

David Maze
David Maze

Reputation: 159040

In your Dockerfile, you don't COPY any of your application code in, just the requirements.txt file. A typical setup copies the entire source tree in after installing package dependencies

# existing
COPY requirements.txt .
RUN pip install -r requirements.txt

# add missing
COPY ./ ./

You should also be able to see this if you docker-compose run web ls, starting a temporary container with a different command; your source code just won't be there.

There are a couple of other small errors or cleanups that can improve this setup as well:

  • In the mysql container, there are spaces around the equals signs in the environment: block. Compose doesn't recognize this, so you set e.g. MYSQL_USER (ending with a space) to mariadb (starting with a space). Remove these spaces, so MYSQL_USER=mariadb.
  • The database configuration is wrong, and you'll get a "connection refused" error at startup. The application container will think 127.0.0.1 is the application container and not the database container or the host. Use the Compose service name of the database container; 'host': 'db'.
  • In the Dockerfile, you're installing the application into the top-level directory of the image filesystem. This will probably work but isn't necessarily a best practice. Early on switch to some other directory; WORKDIR /app.
  • You don't need to repeat the Dockerfile CMD as the Compose command:. I'd delete the command: line.
  • You can use the short form of Compose build: since you have the default Dockerfile name and no other options; build: . with only the directory name and not a mapping.

Upvotes: 2

Related Questions