'How is python importing a package in Docker without installing it in the container first?

I have a new developer on our team and asked her to build a simple proof of concept of a Python client for an Apache Pulsar cluster in Docker.

What's got me flummoxed is that no where did she include a some step to install python package requirements. Normally, I have always had a Dockerfile with a line like:

# Install pip requirements
COPY requirements.txt .
RUN python -m pip install -r requirements.txt

She however, she just has a docker-compose.yml file that looks like:

version: "3"
services:
  pulsar:
    image: apachepulsar/pulsar:latest
    command: ["bin/pulsar", "standalone"]
    volumes:
      - ./modules:/app

  producer:
    image: apachepulsar/pulsar:latest
    volumes:
      - ./modules:/app
    command: ["python", "/app/producer.py"]
    depends_on:
      - pulsar

  consumer:
    image: apachepulsar/pulsar:latest
    command: ["python", "/app/consumer.py"]
    volumes:
      - ./modules:/app
    depends_on:
      - pulsar

There's no command anywhere to install packages. And yet she has a simple script where she imports pulsar a package to facilitate interacting with Apache Pulsar and it runs and behaves fine on my machine. See below for example:

#!python
import pulsar

print('------ consumer output')
client = pulsar.Client('pulsar://pulsar:6650')
consumer = client.subscribe('my-topic', 'my-subscription')

while True:
    msg = consumer.receive()
    try:
        print("Received message '%s' id='%s'", msg.data().decode('utf-8'), msg.message_id(), flush=True)
        consumer.acknowledge(msg)
    except:
        consumer.negative_acknowledge(msg)

client.close()

How is this possible? I run a docker system prune thinking that maybe I had an image with the packages installed somehow? (Not sure that's even possible). But even after that the program still works.



Solution 1:[1]

All steps are here

https://pulsar.apache.org/docs/en/standalone-docker/

pip3 install pulsar-client

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Spann