Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker so slow while installing pip requirements

I am trying to implement a docker for a dummy local Django project. I am using docker-compose as a tool for defining and running multiple containers. Here I tried to containerize the Django-web-app and PostgreSQL two services.

Configuration used in Dockerfile and docker-compose.yml

Dockerfile

# Pull base image
FROM python:3.7-alpine

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /code

# Install dependencies
COPY requirements.txt /code/
RUN pip install -r requirements.txt

# Copy project
COPY . /code/

docker-compose.yml

version: '3.7'

services:
    web:
        build: .
        command: python manage.py runserver 0.0.0.0:8000
        volumes: 
            - .:/code
        ports:
            - "8000:8000"
        depends_on:
            - db
    db:
        image: postgres:11
        volumes:
            - postgres_data:/var/lib/postgresql/data/
volumes:
    postgres_data:

All seems okay. The path postgres integrations and all except one thing pip install -r requirements.txt. This is taking too much time to install from requirements. Last time I was giving up on this but at last the installation does completed but takes lots of time to complete.

In my scenario, the only issue is why the pip install so slow. If there is anything that I am missing? I am new to docker and any help on this topic will be highly appreciated. Thank you.

I was following this Link.

like image 599
dipesh Avatar asked Dec 02 '22 09:12

dipesh


1 Answers

Probably this is because PyPI wheels don’t work on Alpine. Instead of using precompile files Alpine downloads the source code and compile it. Try to use python:3.7-slim image instead:

# Pull base image
FROM python:3.7-slim

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /code

# Install dependencies
COPY requirements.txt /code/
RUN pip install -r requirements.txt

# Copy project
COPY . /code/

Check this article for more details: Alpine makes Python Docker builds 50× slower.

like image 140
neverwalkaloner Avatar answered Dec 04 '22 04:12

neverwalkaloner