Search code examples
dockerdockerfiledocker-machinedocker-registry

Docker how to create custom docker with multiple images in python


I have created a python file which uses multiple packages, for example, PyTorch, sklearn, etc. Now I want to create a custom docker file that uses multiple images for each package I have used in my python file and run it in docker. I didn't understand much until now. Can someone help me with started code with

1. import pytorch image
2.import/install sklearn package 
3. run a python file in the environment.

Solution

  • I am not sure if I get the question correctly or not, But the below docker file can be used to build docker python images. The file starts by - Importing one of the python officials gems - Copy the project files to the docker image - Install OS dependencies - Install python packages listed in requirements.txt file. - define the container command.

    # Use an official Python runtime as a parent image
    FROM python:2.7-slim-stretch
    
    # Set the working directory to /app
    WORKDIR /app
    
    # Copy the current directory contents into the container at /app
    ADD . /app
    
    
    # Install client libs
    RUN apt-get update && apt-get install -y --no-install-recommends \
            default-libmysqlclient-dev \
            gcc \
            vim \
            locales \
            dos2unix \
        && pip install pip==9.0.1 \
        && sed -i 's/^# en_US.UTF-8 UTF-8$/en_US.UTF-8 UTF-8/g' /etc/locale.gen \
        && locale-gen \
        && update-locale LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 \
        &&  pip install -r requirements.txt \
        &&  apt-get purge -y --auto-remove gcc \
        &&  apt-get clean \
        &&  rm -rf \
            /var/lib/apt/lists/* \
            /tmp/* \
            /var/tmp/* \
            /usr/share/man \
            /usr/share/doc \
            /usr/share/doc-base
    
    
    CMD ["python", "my.py"]script