Dockerfile for Python3 and OpenCV

huangapple go评论83阅读模式
英文:

Dockerfile for Python3 and OpenCV

问题

我需要一个带有Python3和最新版本OpenCV的Dockerfile。我编写的Dockerfile如下所述:

  1. FROM ubuntu
  2. #安装OpenCV
  3. RUN apt-get update &&\
  4. apt-get install -y cmake
  5. RUN apt-get install -y gcc g++
  6. RUN apt-get install -y python3-dev python3-numpy
  7. # 支持GUI功能
  8. RUN apt-get install -y libavcodec-dev libavformat-dev libswscale-dev
  9. # 支持GTK 2
  10. RUN apt-get install -y libgtk2.0-dev
  11. # 支持GTK 3
  12. RUN apt-get install -y libgtk-3-dev
  13. #可选依赖项
  14. RUN apt-get install -y libpng-dev
  15. RUN apt-get install -y libjpeg-dev
  16. RUN apt-get install -y libopenexr-dev
  17. RUN apt-get install -y libtiff-dev
  18. RUN apt-get install -y libwebp-dev
  19. # 克隆OpenCV仓库
  20. RUN apt-get install -y git
  21. RUN git clone https://github.com/opencv/opencv.git
  22. #编译
  23. RUN mkdir /opencv/build && \
  24. cd /opencv/build
  25. RUN cmake ..
  26. RUN make

然而,当我运行它时,出现了与 cmake 相关的以下错误:

  1. Step 17/27 : RUN cmake ..
  2. ---> Running in 3dca32df2036
  3. CMake Error: The source directory "/" does not appear to contain CMakeLists.txt.
  4. Specify --help for usage, or press the help button on the CMake GUI.
  5. 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:

  1. FROM ubuntu
  2. #Install OpenCV
  3. RUN apt-get update &&\
  4. apt-get install -y cmake
  5. RUN apt-get install -y gcc g++
  6. RUN apt-get install -y python3-dev python3-numpy
  7. # To support GUI features
  8. RUN apt-get install -y libavcodec-dev libavformat-dev libswscale-dev
  9. # To support GTK 2
  10. RUN apt-get install -y libgtk2.0-dev
  11. # To support GTK 3
  12. RUN apt-get install -y libgtk-3-dev
  13. #Optional dependencies
  14. RUN apt-get install -y libpng-dev
  15. RUN apt-get install -y libjpeg-dev
  16. RUN apt-get install -y libopenexr-dev
  17. RUN apt-get install -y libtiff-dev
  18. RUN apt-get install -y libwebp-dev
  19. # Clone OpenCV repo
  20. RUN apt-get install -y git
  21. RUN git clone https://github.com/opencv/opencv.git
  22. #Compile
  23. RUN mkdir /opencv/build && \
  24. cd /opencv/build
  25. RUN cmake ..
  26. RUN make

However, when i run it, it gives me the following error with cmake.

  1. Step 17/27 : RUN cmake ..
  2. ---> Running in 3dca32df2036
  3. CMake Error: The source directory "/" does not appear to contain CMakeLists.txt.
  4. Specify --help for usage, or press the help button on the CMake GUI.
  5. The command '/bin/sh -c cmake ..' returned a non-zero code: 1

Do you know what am i doing wrong?

答案1

得分: 0

你只需要链式执行最后的 RUN 指令:

  1. #编译
  2. RUN mkdir /opencv/build && \
  3. cd /opencv/build && \
  4. cmake .. && \
  5. make

解释:

RUN 语句会创建一个新的镜像层,并执行指定的 shell 命令。这意味着每个 RUN 语句将在单独的 shell 中运行命令,因此前一个 RUN 的当前目录不会被保留(通过仔细查看错误消息,您可以看到 CMake 的当前目录是 /)。
您可以在 Docker 文档 中找到有关 RUN 语句的更多信息,我还建议阅读 Dockerfile 最佳实践

英文:

You just need to chain the last RUN instructions:

  1. #Compile
  2. RUN mkdir /opencv/build && \
  3. cd /opencv/build && \
  4. cmake .. && \
  5. 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.

huangapple
  • 本文由 发表于 2023年2月14日 00:14:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定