skip to Main Content

Let’s say I have a Python container using Docker, called libraries_container in which some libraries are installed, for example Numpy.
On the other hand, I have another container, also Python called app_container that has an application in Flask, but in which Numpy is NOT installed.
Is there a way to import Numpy from libraries_container in the Flask application?

For example, let’s say that from the Flask application I have a route where I print 10 random numbers generated with numpy.random.randint, something like this

from flask import Flask
import numpy as np

app = Flask(__name__)

@app.route('/test', methods=['POST'])
def generate_random_numbers():
    random_numbers = np.random.randint(1, 101, 10)
    return ', '.join(map(str, random_numbers))

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

It is expected to appear the error of module not found (numpy), and what I want is to be able to import it from the other container (this is just an example for reference).

On the other hand, the idea is to use docker-compose for reasons of the project I am working on.

Thank you very much for your time

2

Answers


  1. Yes, using the COPY command and the --from agruement. This is a common pattern with multi stage builds.

    For example, if you wanted to copy all of the files from the multi-py uvicorn container into your own:

    FROM python:3.12

    COPY --from=ghcr.io/multi-py/python-uvicorn:py3.12-slim-LATEST /usr/local/lib/python3.12/site-packages/* /usr/local/lib/python3.12/site-packages/
    
    Login or Signup to reply.
  2. No, you can’t do this. Each container has its own isolated filesystem, and the images’ filesystems are separate from each other as well. If import numpy starts looking in /usr/lib/python3.12/site-packages, each container will have its own copy of that directory with its own contents, backed by their respective separate images.

    There also isn’t a way to mount a directory from one container to another, since volume mounts tend to be raised as a workaround for incomplete images. For things like Python libraries you have a mix of Python source files, C extension libraries, and in the case of NumPy probably even compiled FORTRAN, and the exact set of files to mount or copy would be hard to tell in any case.

    That is: "an image of libraries" doesn’t really make sense as a standalone thing.

    One thing you can do is use your image of libraries as a base image for your application image. If you had a requirements file listing NumPy and other dependencies, you could have a matching Dockerfile

    ARG python_version=3.12-slim
    FROM python:${python_version}
    COPY requirements.txt /etc
    RUN pip install -r /etc/requirements.txt
    

    and build it into its own image

    docker build -t my-python-base .
    

    Then when you have your own application image, it could start from that

    FROM my-python-base
    WORKDIR /app
    COPY ./ ./
    CMD ["./main.py"]
    

    You have to docker build the library image by hand. You wouldn’t normally run a container off of it (what is its main process?) it wouldn’t be listed in a Compose file at all. So long as you had locally built the base image, you could build: the application image via Compose normally.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search