我有一个Dockerfile
FROM python:3.7.12-slim-buster
FROM aaftio/face_recognition
RUN pip install redis
RUN pip3 install glob2
RUN pip install face_recognition
COPY ./worker.py /worker.py
COPY ./rediswq.py /rediswq.py
CMD python3 worker.py
我构建映像并使用docker run -it {image_id}
运行映像,但是我看到了python应用程序的错误-它抱怨f字符串语法,因为它从python 3.6开始支持I地址,它使用了一些旧的python。
我调试并得到python版本,它打印1.19.0
错误:
File "worker.py", line 14
for filename in glob.iglob(f"/root/divisions/{division_number}/" + '**/*.*', recursive=True):
^
SyntaxError: invalid syntax
我调试并执行whereis python3
,输出为
python3: /usr/bin/python3.5m-config /usr/bin/python3.5 /usr/bin/python3 /usr/bin/python3.5-config /usr/bin/python3.5m /usr/lib/python3 /usr/lib/python3.5 /etc/python3.5 /etc/python3 /usr/local/bin/python3.4m-config /usr/local/bin/python3.4m /usr/local/bin/python3.4 /usr/local/bin/python3 /usr/local/bin/python3.4-config /usr/local/lib/python3.4 /usr/local/lib/python3.5 /usr/include/python3.5 /usr/include/python3.5m /usr/share/python3
错误在于您定义Dockerfile的方式
在创建docker多阶段构建时,您的最终容器将始终基于您引用的最后一个docker容器
在你的例子中,aaftio/face_recognition
使用Python 3.4.9
而不是python:3.7.12-slim-buster
使用Python 3.7.12
docker多阶段构建参考-这里
你可以试试这样
FROM aaftio/face_recognition as intermediate
RUN pip install face_recognition
FROM python:3.7.12-slim-buster
#Copy the python installed libraries from intermediate container
#COPY --from=intermediate face_recognition
RUN pip install redis
RUN pip3 install glob2
COPY ./worker.py /worker.py
COPY ./rediswq.py /rediswq.py
CMD python3 worker.py