代码之家  ›  专栏  ›  技术社区  ›  mightycode Newton

Dockerise django应用程序用于生产,无法解决:process“/bin/sh-c pip install

  •  0
  • mightycode Newton  · 技术社区  · 2 年前

    我试着把一个django集装箱装进码头进行生产。

    所以我有我的档案:

    FROM python:3.8-alpine
    
    ENV PATH="/scripts:${PATH}"
    
    COPY ./requirements.txt /requirements.txt
    RUN apk add --update --no-cache --virtual .tmp gcc libc-dev linux-headers
    RUN pip install -r /requirements.txt
    RUN apk del .tmp
    
    RUN mkdir /app
    COPY ./Welzijn /app
    WORKDIR /app
    COPY ./scripts /scripts
    
    RUN chmod +x /scripts/*
    
    RUN mkdir -p /vol/web/media
    RUN mkdir -p /vol/web/static
    RUN adduser -D user
    RUN chown -R user:user /vol
    RUN chmod -R 755 /vol/web
    USER user
    
    CMD ["entrypoint.sh"]
    

    以及docker compose文件:

    version: "3.9"
    
    services:
      app:
        build:
          context: .
        ports:
          - "8000:8000"
        volumes:
          - ./Welzijn:/app
        command: >
          sh -c "python ./manage.py migrate &&      
                 python ./manage.py runserver 0:8000"
        environment:
          - DEBUG=1
        env_file:
          - ./.env
    
    

    和requirements.txt文件:

    Django>=4.0.4
    uWSGI>=2.0.18,<2.1
    djangorestframework>=3.13.1
    psycopg2>=2.9.3
    drf-spectacular>=0.22.1
    Pillow>=9.1.0
    drf-yasg==1.20.0 
    django-cors-headers==3.10.1
    django-dotenv
    

    但是当我执行docker compose-up-build时

    我收到以下错误:

     If you prefer to avoid building psycopg2 from source, please install the PyPI
    3.686       'psycopg2-binary' package instead.
    3.686
    3.686       For further information please check the 'doc/src/install.rst' file (also at
    3.686       <https://www.psycopg.org/docs/install.html>).
    3.686
    3.686       [end of output]
    3.686
    3.686   note: This error originates from a subprocess, and is likely not a problem with pip.
    3.690 error: metadata-generation-failed
    3.690
    3.690 × Encountered error while generating package metadata.
    3.690 ╰─> See above for output.
    3.690
    3.690 note: This is an issue with the package mentioned above, not pip.
    3.690 hint: See above for details.
    3.835
    3.835 [notice] A new release of pip is available: 23.0.1 -> 23.2.1
    3.835 [notice] To update, run: pip install --upgrade pip
    ------
    failed to solve: process "/bin/sh -c pip install -r /requirements.txt" did not complete successfully: exit code: 1
    

    问题:如何将django码头化生产?

    0 回复  |  直到 2 年前
        1
  •  1
  •   Ragas Imger    2 年前

    以下是使用Postgres数据库和PgAdmin将django码头化以用于生产的完整流程。

    Dockerfile

    FROM python:3.10-slim-buster
    
    # Creating workdir and copying the config and requirements dependencies
    WORKDIR /projectdirname
    COPY requirements.txt /projectdirname/
    
    RUN apt-get update && apt-get install -y git gcc\
        && pip install -r requirements.txt
    
    COPY . /projectdirname
    RUN chmod +x entrypoint.sh
    ENTRYPOINT [ "sh", "entrypoint.sh" ]
    

    docker-compose.yml

    version: '3'
    
    services:
      postgres:
        image: postgres:latest
        restart: always
        container_name: pg_container
        command: postgres -c 'max_connections=100'
        volumes:
          - postgres_db:/var/lib/postgresql/data
        env_file:
          - .env
    
      pgadmin:
        image: dpage/pgadmin4
        container_name: pgadmin4_container
        restart: always
        ports:
          - "5050:80"
        env_file:
          - .env
        environment:
          PGADMIN_DEFAULT_EMAIL: [email protected]
          PGADMIN_DEFAULT_PASSWORD: example
        volumes:
          - postgres_admin:/var/lib/postgresql/data/admin
    
      server:
        build: .
        restart: always
        container_name: django_container
        links:
          - postgres:postgres
          - pgadmin:pgadmin
        depends_on:
          - postgres
          - pgadmin
        env_file:
          - .env
        volumes:
          - ./:/projectdirname
        ports:
          - '8000:8000'
    
    volumes:
      postgres_db:
      postgres_admin:
    

    入口点.sh

    python manage.py migrate
    python manage.py runserver 0.0.0.0:8000
    

    .env

    SECRET_KEY=Place your secret key here
    POSTGRES_USER=test
    POSTGRES_PASSWORD=test
    POSTGRES_DB=dbName
    POSTGRES_PORT=5432
    POSTGRES_HOST=postgres