英文:
Dockerfile for Python3 and OpenCV
问题
我需要一个带有Python3和最新版本OpenCV的Dockerfile。我编写的Dockerfile如下所述:
FROM ubuntu
#安装OpenCV
RUN apt-get update &&\
apt-get install -y cmake
RUN apt-get install -y gcc g++
RUN apt-get install -y python3-dev python3-numpy
# 支持GUI功能
RUN apt-get install -y libavcodec-dev libavformat-dev libswscale-dev
# 支持GTK 2
RUN apt-get install -y libgtk2.0-dev
# 支持GTK 3
RUN apt-get install -y libgtk-3-dev
#可选依赖项
RUN apt-get install -y libpng-dev
RUN apt-get install -y libjpeg-dev
RUN apt-get install -y libopenexr-dev
RUN apt-get install -y libtiff-dev
RUN apt-get install -y libwebp-dev
# 克隆OpenCV仓库
RUN apt-get install -y git
RUN git clone https://github.com/opencv/opencv.git
#编译
RUN mkdir /opencv/build && \
cd /opencv/build
RUN cmake ..
RUN make
然而,当我运行它时,出现了与 cmake
相关的以下错误:
Step 17/27 : RUN cmake ..
---> Running in 3dca32df2036
CMake Error: The source directory "/" does not appear to contain CMakeLists.txt.
Specify --help for usage, or press the help button on the CMake GUI.
The command '/bin/sh -c cmake ..' returned a non-zero code: 1
你知道我做错了什么吗?
英文:
I need to have a Dockerfile with Python3 and the latest version of OpenCV. The Dockerfile I have written is described below:
FROM ubuntu
#Install OpenCV
RUN apt-get update &&\
apt-get install -y cmake
RUN apt-get install -y gcc g++
RUN apt-get install -y python3-dev python3-numpy
# To support GUI features
RUN apt-get install -y libavcodec-dev libavformat-dev libswscale-dev
# To support GTK 2
RUN apt-get install -y libgtk2.0-dev
# To support GTK 3
RUN apt-get install -y libgtk-3-dev
#Optional dependencies
RUN apt-get install -y libpng-dev
RUN apt-get install -y libjpeg-dev
RUN apt-get install -y libopenexr-dev
RUN apt-get install -y libtiff-dev
RUN apt-get install -y libwebp-dev
# Clone OpenCV repo
RUN apt-get install -y git
RUN git clone https://github.com/opencv/opencv.git
#Compile
RUN mkdir /opencv/build && \
cd /opencv/build
RUN cmake ..
RUN make
However, when i run it, it gives me the following error with cmake
.
Step 17/27 : RUN cmake ..
---> Running in 3dca32df2036
CMake Error: The source directory "/" does not appear to contain CMakeLists.txt.
Specify --help for usage, or press the help button on the CMake GUI.
The command '/bin/sh -c cmake ..' returned a non-zero code: 1
Do you know what am i doing wrong?
答案1
得分: 0
你只需要链式执行最后的 RUN
指令:
#编译
RUN mkdir /opencv/build && \
cd /opencv/build && \
cmake .. && \
make
解释:
RUN
语句会创建一个新的镜像层,并执行指定的 shell 命令。这意味着每个 RUN
语句将在单独的 shell 中运行命令,因此前一个 RUN
的当前目录不会被保留(通过仔细查看错误消息,您可以看到 CMake 的当前目录是 /
)。
您可以在 Docker 文档 中找到有关 RUN
语句的更多信息,我还建议阅读 Dockerfile 最佳实践。
英文:
You just need to chain the last RUN
instructions:
#Compile
RUN mkdir /opencv/build && \
cd /opencv/build && \
cmake .. && \
make
Explanation:
A RUN
statement creates a new image layer and executes the specified shell command. This means that each RUN
statement will run the command in a separate shell, so the current directory from the previous RUN
will not be preserved (you can see that the CMake current directory is /
by looking closely at the error message).
You can find more info about RUN
statement in the Docker documentation and I also recommend reading Best practices for writing Dockerfiles.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论