skip to Main Content

I have the docker file:

FROM python:3.9-slim

RUN apt-get update && apt-get install -y openssh-server
RUN pip install 
    cryptography 
    bcrypt 
    pynacl 
    paramiko 
    scp 
    loguru 
    pydantic 
    psutil

CMD ["/usr/sbin/sshd", "-D", "-h", "/etc/ssh/key"]
WORKDIR /code

And this main.py file import references:

import psutil
from loguru import logger
from paramiko import SSHClient
from pydantic import BaseModel
from scp import SCPClient

When running docker-compose with:

COMMAND='python main.py "hostname" 10.0.0.1,10.0.0.2,192.168.0.5' docker-compose up

I get this output:

infra-host_a-1  | Traceback (most recent call last):
infra-host_a-1  |   File "main.py", line 10, in <module>
infra-host_a-1  |     from loguru import logger
infra-host_a-1  | ModuleNotFoundError: No module named 'loguru'
infra-host_a-1 exited with code 1

Docker-compose content:

version: "2.2"


services:
  host_a:
    build: build
    command: ${COMMAND}
    hostname: hostA
    ports:
    - "2222:22"
    volumes:
    - ./code:/code
    networks:
      net1:
        ipv4_address: 10.10.100.1
  host_b:
    build: build
    hostname: hostB
    networks:
      net1:
        ipv4_address: 10.10.100.2
      net2:
        ipv4_address: 10.10.200.1
  host_c:
    build: build
    hostname: hostC
    networks:
      net1:
        ipv4_address: 10.10.100.3
      net3:
        ipv4_address: 172.29.100.1
networks:
  net1:
    driver: bridge
    enable_ipv6: false
    ipam:
      config:
      - subnet: 10.10.100.0/24
        gateway: 10.10.100.32

This only happens with docker-compose, but when running the project in a container alone it works with docker build/run.
How do I solve this?

2

Answers


  1. Chosen as BEST ANSWER

    I solved the issue by using --build with docker compose like this:

    docker-compose up --build
    

    This made the libraries installation valid


  2. Wrong Python Command?

    On Unix Like Systems, the Paython command is usually python3 rather than just python. I suspect that you installed all the dependencies for Python3 but then tried to run it with Python2

    Virtual Enviroment?

    If the above’s solution of chaning the command to python3 did not work, you may need to create a virtual enviroment inside the docker container, install packages into that virtual enviroment and run the python file in the Virtual Enviroment as well.

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