调试Vagrant+Docker+Flask的最佳实践
我的目标是通过Docker容器运行一个Flask网页服务器。在Windows电脑上,这需要使用Vagrant来创建一个虚拟机。运行命令vagrant up --provider=docker时,出现了以下错误:
INFO interface: error: The container started either never left the "stopped" state or
very quickly reverted to the "stopped" state. This is usually
because the container didn't execute a command that kept it running,
and usually indicates a misconfiguration.
If you meant for this container to not remain running, please
set the Docker provider configuration "remains_running" to "false":
config.vm.provider "docker" do |d|
d.remains_running = false
end
这是我的Dockerfile
FROM mrmrcoleman/python_webapp
EXPOSE 5000
# Install Python
RUN apt-get install -y python python-dev python-distribute python-pip
# Add and install Python modules
RUN pip install Flask
#copy the working directory to the container
ADD . /
CMD python run.py
这是我的Vagrantfile
Vagrant.configure("2") do |config|
config.vm.provider "docker" do |d|
d.build_dir = "." #searches for a local dockerfile
end
config.vm.synced_folder ".", "/vagrant", type: "rsync"
rsync__chown = false
end
因为Vagrantfile和run.py独立运行时没有问题,所以我怀疑我的Dockerfile可能出错了。我的问题有两个:
- Dockerfile或Vagrantfile中有没有明显的错误?
- 有没有办法让vagrant/docker提供更具体的错误信息?
2 个回答
0
你的Dockerfile和Vagrantfile看起来没什么问题,但我觉得你需要修改一下run.py的权限,让它可以执行:
... #copy the working directory to the container ADD . / RUN chmod +x run.py CMD python run.py
这样可以吗?
有没有办法让vagrant/docker给出更具体的错误信息呢?
你可以看看这个vagrant调试页面。我还会用另一种方法,就是登录到容器里,手动运行脚本。
# log onto the vm running docker
vagrant ssh
# start your container in bash, assuming its already built.
docker run -it my/container /bin/bash
# now from inside your container try to start your app
python run.py
另外,如果你想在本地查看你的应用,你需要在Vagrantfile里添加端口转发。
2
我觉得我想要的答案是使用这个命令:
vagrant docker-logs
我把Dockerfile搞坏了,因为我没有意识到正常的运行状态就是好的表现。其实如果应用程序正常运行的话,表面上看似乎没什么变化。通过docker-logs可以确认flask应用正在监听请求。