我一直在努力使用 Docker Toolbox 在 Windows 10 Home 上启动并运行 Docker 映像。这是我在 Kitematic 中看到的错误:
python: can't open file 'src/__main__.py': [Errno 2]
No such file or directory
这个失败的 Python 命令是“notes”Docker 容器的入口点。
Docker/up.sh 执行完毕后,没有任何错误。
Successfully built f59c275721cf
Successfully tagged docker_mysql:latest
Creating mysql ... done
Creating notes ... done
此外,这是一个小组项目,其中 Mac 上的其他用户无法复制此错误。将 Dockerfile 更新为以下内容并运行后:
Successfully built 42a95cd419f2
Successfully tagged docker_mysql:latest
Creating mysql ... done
Creating notes ... done
User@DESKTOP-EBUI9GE MINGW64 /c/users/user/Desktop/6440/Procedure-Notes-Application (master)
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
68f60fdba6f5 docker_mysql "docker-entrypoint.s…" 15 seconds ago Up 9 seconds 0.0.0.0:23306->3306/tcp mysql
User@DESKTOP-EBUI9GE MINGW64 /c/users/user/Desktop/6440/Procedure-Notes-Application (master)
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7fbd5b73189 docker_notes "python /app/src/__m…" 23 seconds ago Exited (2) 17 seconds ago notes
68f60fdba6f5 docker_mysql "docker-entrypoint.s…" 23 seconds ago Up 17 seconds 0.0.0.0:23306->3306/tcp mysql
更新的 Dockerfile:
### Use an official Python runtime as a parent image
FROM python:2.7-slim
### set working directory to the code location
WORKDIR /app
### copy the code base over (redone by the compose file)
COPY . .
### Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
### Make port available to the world outside this container
EXPOSE 5000
### start the application
CMD ["python", "/app/src/__main__.py"]
我现在收到另一个错误:
* Serving Flask app "__main__" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
Traceback (most recent call last):
File "/app/src/__main__.py", line 57, in <module>
app.run(debug=True, host='0.0.0.0')
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 943, in run
run_simple(host, port, self, **options)
File "/usr/local/lib/python2.7/site-packages/werkzeug/serving.py", line 988, in run_simple
run_with_reloader(inner, extra_files, reloader_interval, reloader_type)
File "/usr/local/lib/python2.7/site-packages/werkzeug/_reloader.py", line 332, in run_with_reloader
sys.exit(reloader.restart_with_reloader())
File "/usr/local/lib/python2.7/site-packages/werkzeug/_reloader.py", line 176, in restart_with_reloader
exit_code = subprocess.call(args, env=new_environ, close_fds=False)
File "/usr/local/lib/python2.7/subprocess.py", line 172, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/local/lib/python2.7/subprocess.py", line 394, in __init__
errread, errwrite)
File "/usr/local/lib/python2.7/subprocess.py", line 1047, in _execute_child
raise child_exception
OSError: [Errno 8] Exec format error
答案1
我开始这个过程时说的第一句话是:
您不需要 mkdir,因为 WORKDIR 会创建它。最好使用绝对引用,例如
WORKDIR /app
和CMD ["python", "/app/src/__main__.py"]
。
Python脚本现在启动和停止时出现错误:
File "/usr/local/lib/python2.7/subprocess.py", line 1047, in _execute_child
raise child_exception
这通常是 Python 脚本中的错误。帖子中描述了一种可能性 python subprocess.call() 未按预期工作:
默认情况下
subprocess.call
不使用 shell 来运行我们的命令,因此您无法运行类似的 shell 命令cd
。要使用 shell 运行命令,请使用
shell=True
作为参数。在这种情况下,建议将命令作为单个字符串而不是列表传递。由于它由 shell 运行,因此您也可以~/
在路径中使用:subprocess.call("(cd ~/catkin_ws/src && catkin_make)", shell=True)