Home > Software design >  Run PIP installed library in same Dockerfile during image build
Run PIP installed library in same Dockerfile during image build

Time:10-12

I need to install a PIP library in my docker image and run it from the very same Dockerfile.

Example:

from python

COPY requirements.txt /app/requirements.txt
RUN pip3 install -U -r ete3

RUN 'python3 -c "from ete3 import NCBITaxa;NCBITaxa();"'


CMD ["gunicorn", "-k", "uvicorn.workers.UvicornWorker", "-c", "gunicorn_conf.py", "api.main:app"]

running this fails with

/bin/sh: 1: python3 -c "import ete3": not found
The command '/bin/sh -c 'python3 -c "from ete3 import NCBITaxa;NCBITaxa();"'' returned a non-zero code: 127

however, if I remove the RUN directive, finish the build, attach to the running container python3 -c "from ete3 import NCBITaxa;NCBITaxa();" will work.

I assume since its pip and installed into /usr/local/lib/python3.8/dist-packages/ete3 it should be in the PYTHONPATH? Or is it that because this is at image build time the system is not yet available? Any tricks?

I need this functionality to pull some NCBI database updates at build time into the image so that this ete3 library is up2date for deployment. ete3 would pull the files at runtime and that is slow and terrible.

One alternative could be to prebuild an image with the PIP library installed from Dockerfile1 and run the update in Dockerfile2 ?

CodePudding user response:

You need to put the commands you want to run into a python script like something.py.

Your something.py file can look like,

import eta3
...
...
...

Then change the last line to this,

CMD python /home/username/something.py

Based on your comment, you can do the same thing with a bash script and run it using the CMD command in the dockerfile like this.

Your script will have the commands you want to run like this.

#!/bin/bash
gunicorn -k uvicorn.workers.UvicornWorker -c gunicorn_conf.py 

And you can add this line to your docker file.

CMD /bash_script.sh
  • Related