简体   繁体   中英

python path in dockerfile

I am trying to dockerize my java app with a python script that will execute at some point from java code. I am newbie to docker. So, i am facing issue with execute python script because it doesn't find module: import psycopg2 ImportError: No module named psycopg2 . this is my Dockerfile.

FROM python:2.7-alpine

RUN apk update && \
    apk add --virtual build-deps gcc python-dev musl-dev && \
    apk add postgresql-dev

RUN mkdir /usr/src/app
WORKDIR /usr/src/app    

RUN pip install psycopg2
RUN pip install -U "pip==1.5.4"
RUN pip install cql
RUN pip install chardet

ENV PYTHONPATH "${PYTHONPATH}:/usr/src/app"

FROM openjdk:8
EXPOSE 9000
ADD target/my_jar.jar my_jar.jar
ENTRYPOINT ["java","-jar","/my_jar.jar"]

I think i miss python path to install modules correctly.

you need to run:

pip --target=/usr/src/app install psycopg2

to allow pip to install the files in that folder

so your dockerfile schould be something like:

FROM python:2.7-alpine

RUN apk update && \
    apk add --virtual build-deps gcc python-dev musl-dev && \
    apk add postgresql-dev

RUN mkdir /usr/src/app
WORKDIR /usr/src/app    

RUN pip --target=/usr/src/app install psycopg2
RUN pip --target=/usr/src/app install -U "pip==1.5.4"
RUN pip --target=/usr/src/app install cql
RUN pip --target=/usr/src/app install chardet


FROM openjdk:8
EXPOSE 9000
ENV PYTHONPATH "${PYTHONPATH}:/usr/src/app"
ADD target/my_jar.jar my_jar.jar
COPY --from=0 /usr/src/app /usr/src/app
ENTRYPOINT ["java","-jar","/my_jar.jar"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM