Docker error while installing python dependencies – no module named ‘databases’

I am trying to run docker container with docker-compose.
For install python dependencies I am using pipenv.
In this container I am trying to run FastApi application with async databases using ‘databases’ library.

This is my Pipfile:

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
fastapi = {extras = ["all"], version = "*"}
databases = {extras = ["postgresql"], version = "*"}

[dev-packages]

[requires]

Dockerfile:

# Pull base image
FROM python:3.8
# Set environment varibles
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV PYTHONPATH=./code
WORKDIR /code/
# Install dependencies
RUN pip install pipenv
COPY Pipfile Pipfile.lock /code/
#RUN pipenv install --system --dev
RUN pipenv install --deploy --system
RUN pipenv install databases
COPY . /code/
EXPOSE 8000
#CMD ["python", "main.py"]
CMD pipenv run python main.py

Docker-compose.yml

version: "3"
services:
  postgres:
    image: postgres:11
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=postgres
    volumes:
      - data:/var/lib/postgresql/data

  app:
    build: .
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - postgres
      - redis

  pgadmin:
    container_name: pgadmin
    image: dpage/pgadmin4
    environment:
      - [email protected]
      - PGADMIN_DEFAULT_PASSWORD=admin
    ports:
      - "5050:80"
    depends_on:
      - postgres

volumes:
  data: {}

And main.py file:

import uvicorn
from fastapi import FastAPI, Depends
from pydantic import BaseModel
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, Session
import databases

SQLALCHEMY_DATABASE_URL = "postgresql://postgres:[email protected]/postgres"

database = databases.Database(SQLALCHEMY_DATABASE_URL)

engine = create_engine(SQLALCHEMY_DATABASE_URL)

Base = declarative_base()

class UserModel(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    first_name = Column(String, unique=True, index=True)
    last_name = Column(String)
    age = Column(Integer,)


Base.metadata.create_all(bind=engine)

app = FastAPI()

class UserSchema(BaseModel):
    first_name: str
    last_name: str = None
    age: int

    class Config:
        orm_mode = True

user_table = UserModel.__table__


@app.post("/user/", response_model=UserSchema)
async def create_user(user: UserSchema):
    query = user_table.insert().values(
        first_name=user.first_name, last_name=user.last_name, age=user.age
    )
    last_rec = await database.execute(query)
    return {**user.dict(), "id":last_red}


@app.get("/user/", response_model=UserSchema)
async def get_user(first_name: str):
    _user = user_table.select().where(first_name=first_name)
    return _user


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)

I am not sure why I am still getting this error:

Traceback (most recent call last):
  File "main.py", line 7, in <module>
    import databases
ModuleNotFoundError: No module named 'databases'

Any ideas?

Kind regards

Source: Docker Questions